From d042ce973ca96f40ada52567c5e45d41756bf89f Mon Sep 17 00:00:00 2001 From: Bruno Casali Date: Tue, 25 Jan 2022 18:40:34 -0300 Subject: [PATCH] Add User-Agent inside the pre-defined headers This commit also exposes a way to add any other header to the request --- lib/meilisearch/http_request.rb | 3 ++- lib/meilisearch/version.rb | 4 ++++ spec/meilisearch/index/base_spec.rb | 3 ++- spec/meilisearch_spec.rb | 11 +++++++++++ 4 files changed, 19 insertions(+), 2 deletions(-) diff --git a/lib/meilisearch/http_request.rb b/lib/meilisearch/http_request.rb index e6e016d2..5b724e5a 100644 --- a/lib/meilisearch/http_request.rb +++ b/lib/meilisearch/http_request.rb @@ -89,7 +89,8 @@ def http_delete(relative_path = '') def build_default_options_headers { 'Content-Type' => 'application/json', - 'Authorization' => ("Bearer #{@api_key}" unless @api_key.nil?) + 'Authorization' => ("Bearer #{@api_key}" unless @api_key.nil?), + 'User-Agent' => MeiliSearch.qualified_version }.compact end diff --git a/lib/meilisearch/version.rb b/lib/meilisearch/version.rb index 84faca7c..932268da 100644 --- a/lib/meilisearch/version.rb +++ b/lib/meilisearch/version.rb @@ -2,4 +2,8 @@ module MeiliSearch VERSION = '0.18.0' + + def self.qualified_version + "Meilisearch Ruby (v#{VERSION})" + end end diff --git a/spec/meilisearch/index/base_spec.rb b/spec/meilisearch/index/base_spec.rb index 6bd41691..cd0869db 100644 --- a/spec/meilisearch/index/base_spec.rb +++ b/spec/meilisearch/index/base_spec.rb @@ -102,7 +102,8 @@ it 'supports options' do options = { timeout: 2, max_retries: 1 } expected_headers = { - 'Authorization' => "Bearer #{MASTER_KEY}" + 'Authorization' => "Bearer #{MASTER_KEY}", + 'User-Agent' => MeiliSearch.qualified_version } new_client = MeiliSearch::Client.new(URL, MASTER_KEY, options) diff --git a/spec/meilisearch_spec.rb b/spec/meilisearch_spec.rb index a86202d0..18500e87 100644 --- a/spec/meilisearch_spec.rb +++ b/spec/meilisearch_spec.rb @@ -5,6 +5,10 @@ expect(MeiliSearch::VERSION).not_to be nil end + it 'has a qualified version number' do + expect(MeiliSearch.qualified_version).to eq("Meilisearch Ruby (v#{MeiliSearch::VERSION})") + end + it 'raises an exception when it is impossible to connect' do new_client = MeiliSearch::Client.new('http://127.0.0.1:8800', 'masterKey') expect do @@ -24,4 +28,11 @@ new_client.indexes end.to raise_error(Timeout::Error) end + + it 'has a pre-defined header with current version' do + new_client = MeiliSearch::Client.new(URL, MASTER_KEY) + + expect(new_client.headers).to have_key('User-Agent') + expect(new_client.headers['User-Agent']).to eq(MeiliSearch.qualified_version) + end end