From f6b7652a011392c955654a5c873caa08d396f88d Mon Sep 17 00:00:00 2001 From: Eugene Burmakin Date: Mon, 11 Aug 2025 00:21:58 +0200 Subject: [PATCH] Return dawarich headers on all API responses --- CHANGELOG.md | 6 ++++++ app/controllers/api/v1/health_controller.rb | 8 -------- app/controllers/api_controller.rb | 8 ++++++++ spec/requests/api/v1/countries/borders_spec.rb | 14 ++++++++++++++ 4 files changed, 28 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 30f554a6..62a6aa37 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/). +# [0.30.9] - 2025-08-11 + +## Added + +- X-Dawarich-Response and X-Dawarich-Version headers are now returned for all API responses. + # [0.30.8] - 2025-08-01 ## Fixed diff --git a/app/controllers/api/v1/health_controller.rb b/app/controllers/api/v1/health_controller.rb index 8e13d165..1e5ab2f1 100644 --- a/app/controllers/api/v1/health_controller.rb +++ b/app/controllers/api/v1/health_controller.rb @@ -4,14 +4,6 @@ class Api::V1::HealthController < ApiController skip_before_action :authenticate_api_key def index - if current_api_user - response.set_header('X-Dawarich-Response', 'Hey, I\'m alive and authenticated!') - else - response.set_header('X-Dawarich-Response', 'Hey, I\'m alive!') - end - - response.set_header('X-Dawarich-Version', APP_VERSION) - render json: { status: 'ok' } end end diff --git a/app/controllers/api_controller.rb b/app/controllers/api_controller.rb index 4d13bdaf..d53f57ae 100644 --- a/app/controllers/api_controller.rb +++ b/app/controllers/api_controller.rb @@ -2,10 +2,18 @@ class ApiController < ApplicationController skip_before_action :verify_authenticity_token + before_action :set_version_header before_action :authenticate_api_key private + def set_version_header + message = "Hey, I\'m alive#{current_api_user ? ' and authenticated' : ''}!" + + response.set_header('X-Dawarich-Response', message) + response.set_header('X-Dawarich-Version', APP_VERSION) + end + def authenticate_api_key return head :unauthorized unless current_api_user diff --git a/spec/requests/api/v1/countries/borders_spec.rb b/spec/requests/api/v1/countries/borders_spec.rb index d0717dcf..b5922b73 100644 --- a/spec/requests/api/v1/countries/borders_spec.rb +++ b/spec/requests/api/v1/countries/borders_spec.rb @@ -12,6 +12,13 @@ RSpec.describe 'Api::V1::Countries::Borders', type: :request do expect(response).to have_http_status(:unauthorized) end + + it 'returns X-Dawarich-Response header' do + get '/api/v1/countries/borders' + + expect(response.headers['X-Dawarich-Response']).to eq('Hey, I\'m alive!') + expect(response.headers['X-Dawarich-Version']).to eq(APP_VERSION) + end end context 'when user is authenticated' do @@ -22,6 +29,13 @@ RSpec.describe 'Api::V1::Countries::Borders', type: :request do expect(response.body).to include('AF') expect(response.body).to include('ZW') end + + it 'returns X-Dawarich-Response header' do + get '/api/v1/countries/borders', headers: { 'Authorization' => "Bearer #{user.api_key}" } + + expect(response.headers['X-Dawarich-Response']).to eq('Hey, I\'m alive and authenticated!') + expect(response.headers['X-Dawarich-Version']).to eq(APP_VERSION) + end end end end