Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Client information headers #31

Merged
merged 5 commits into from
May 8, 2015
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion lib/auth0/mixins/initializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ def initialize(config)
domain = api_domain options
raise InvalidApiNamespace, "Api namespace must supply an API domain" if domain.nil?
self.class.base_uri "https://#{domain}"
self.class.headers "Content-Type" => 'application/json'

self.class.headers client_headers

self.extend Auth0::Api::AuthenticationEndpoints
@client_id = options[:client_id]
initialize_v2(options) if api_v2?(options)
Expand All @@ -28,6 +30,14 @@ def self.included(klass)

private

def client_headers
{
'Content-Type' => 'application/json',
'User-Agent' => "Ruby/#{RUBY_VERSION}",
'X-Auth0-Client' => "Ruby/#{Auth0::VERSION}"
}
end

def api_domain(options)
options[:domain] || options[:namespace]
end
Expand Down
24 changes: 24 additions & 0 deletions spec/integration/lib/auth0/auth0_client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,28 @@
let(:credentials) { v2_credentials.merge({access_token: ENV["MASTER_JWT"]}) }
end

context "client headers" do
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it will better to test this in an unit tests but we can have this here till we re-do them

let(:client) { Auth0::Client.new(v2_credentials.merge({access_token: 'abc123', domain: 'myhost.auth0.com'})) }
let(:headers) { client.class.headers }

it "has the correct headers present" do
headers.keys.sort.should eql ['Authorization', 'Content-Type', 'User-Agent', 'X-Auth0-Client']
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rather use expect syntax instead of should.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unless we use implicit subject..

end

it "uses the correct access token" do
headers['Authorization'].should eql "Bearer abc123"
end

it "is always json" do
headers['Content-Type'] = 'application/json'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you forgot the eq 😄

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seriously funny 😄
… I think I did this when I was hanging with @eugeniop and @ntotten. I'll amend :-)

end

it "sets the ruby version" do
headers['User-Agent'].should eql "Ruby/#{RUBY_VERSION}"
end

it "sets the client version" do
headers['X-Auth0-Client'].should eql "Ruby/#{Auth0::VERSION}"
end
end
end