feat(user-agent): Send the plugin name and version in the UA

This commit is contained in:
Pixelastic 2015-07-28 19:49:20 +02:00
parent c0db6f4908
commit ecd4c67f06
3 changed files with 33 additions and 0 deletions

View File

@ -124,8 +124,15 @@ class AlgoliaSearchJekyllPush < Jekyll::Command
index.set_settings(settings)
end
# Change the User-Agent header to isolate calls from this plugin
def set_user_agent_header
version = Gem::Specification.load('algoliasearch-jekyll.gemspec').version
Algolia.set_extra_header('User-Agent', "Algolia for Jekyll #{version}")
end
# Create an index to push our data
def create_index(index_name)
set_user_agent_header
index = Algolia::Index.new(index_name)
configure_index(index) unless @is_dry_run
index

View File

@ -184,6 +184,29 @@ describe(AlgoliaSearchJekyllPush) do
end
end
describe 'set_user_agent_header' do
it 'should set a User-Agent with the plugin name and version' do
# Given
expected = '48.1516.2342'
spec = Gem::Specification.load('algoliasearch-jekyll.gemspec')
spec.version = expected
allow(Algolia).to receive(:set_extra_header)
# When
push.set_user_agent_header
# Then
expect(Algolia).to have_received(:set_extra_header).with(
'User-Agent',
/Jekyll/
)
expect(Algolia).to have_received(:set_extra_header).with(
'User-Agent',
/#{expected}/
)
end
end
describe 'push' do
let(:index_double) { double('Algolia Index').as_null_object }
let(:config) do
@ -199,6 +222,7 @@ describe(AlgoliaSearchJekyllPush) do
# Mock all calls to not send anything
allow_any_instance_of(AlgoliaSearchCredentialChecker)
.to receive(:assert_valid)
allow(Algolia).to receive(:set_extra_header)
allow(Algolia).to receive(:init)
allow(Algolia).to receive(:move_index)
allow(Algolia::Index).to receive(:new).and_return(index_double)

2
todo Normal file
View File

@ -0,0 +1,2 @@
Add User-Agent when indexing