Skip to content

Commit

Permalink
Add User-Agent inside the pre-defined headers
Browse files Browse the repository at this point in the history
This commit also exposes a way to add any other header to the request
  • Loading branch information
brunoocasali committed Jan 25, 2022
1 parent aba0af3 commit 019b083
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/meilisearch/http_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ def http_delete(relative_path = '')
def build_default_options_headers
{
'Content-Type' => 'application/json',
'User-Agent' => [@options[:user_agent], MeiliSearch.qualified_version].compact.join(' ; '),
'Authorization' => ("Bearer #{@api_key}" unless @api_key.nil?)
}.compact
end
Expand Down
4 changes: 4 additions & 0 deletions lib/meilisearch/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,8 @@

module MeiliSearch
VERSION = '0.18.0'

def self.qualified_version
"MeiliSearch Ruby (v#{VERSION})"
end
end
1 change: 1 addition & 0 deletions spec/meilisearch/index/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@
options = { timeout: 2, max_retries: 1 }
expected_headers = {
'Authorization' => "Bearer #{MASTER_KEY}",
'User-Agent' => MeiliSearch.qualified_version
}

new_client = MeiliSearch::Client.new(URL, MASTER_KEY, options)
Expand Down
18 changes: 18 additions & 0 deletions spec/meilisearch_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -24,4 +28,18 @@
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

it 'has a way to add other headers along the pre-defined ones' do
new_client = MeiliSearch::Client.new(URL, MASTER_KEY, user_agent: 'custom client')

expect(new_client.headers['User-Agent']).to \
eq(['custom client', MeiliSearch.qualified_version].join(' ; '))
end
end

0 comments on commit 019b083

Please sign in to comment.