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

Use client header spec as per documentation #32

Merged
merged 3 commits into from
May 29, 2015
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 2 additions & 1 deletion .bundle/config
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
---
BUNDLE_JOBS: 4
BUNDLE_JOBS: '4'
BUNDLE_BIN: bin
16 changes: 11 additions & 5 deletions lib/auth0/mixins/initializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,18 @@ def self.included(klass)
private

def client_headers(config)
sdk_headers = {
'Content-Type' => 'application/json',
client_info = JSON.dump({name: 'ruby-auth0', version: Auth0::VERSION})

headers = {
'Content-Type' => 'application/json'
}
sdk_headers['User-Agent'] = "Ruby/#{RUBY_VERSION}" if not config[:opt_out_sdk_info]
sdk_headers['Auth0-Client'] = "ruby-auth0/#{Auth0::VERSION}" if not config[:opt_out_sdk_info]
sdk_headers

if !config[:opt_out_sdk_info]
headers['User-Agent'] = "Ruby/#{RUBY_VERSION}"
headers['Auth0-Client'] = Base64.urlsafe_encode64(client_info)
end

headers
end

def api_domain(options)
Expand Down
35 changes: 26 additions & 9 deletions spec/integration/lib/auth0/auth0_client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,32 @@
let(:credentials) { v2_credentials.merge({access_token: ENV["MASTER_JWT"]}) }
end

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

context "client headers" do
let(:client) { Auth0::Client.new(v2_credentials.merge({access_token: 'abc123', domain: 'myhost.auth0.com'})) }
let(:headers) { client.class.headers }
specify { expect(headers).to include("Auth0-Client" => "ruby-auth0/#{Auth0::VERSION}") }
specify { expect(headers).to include("User-Agent" => "Ruby/#{RUBY_VERSION}") }
specify { expect(headers).to include("Authorization" => "Bearer #{access_token}") }
specify { expect(headers).to include("Content-Type" => "application/json") }

let(:base64_token) {
Base64.urlsafe_encode64('{"name":"ruby-auth0","version":"'+Auth0::VERSION+'"}')
}

it "has the correct headers present" do
expect(headers.keys.sort).to eql ['Auth0-Client', 'Authorization', 'Content-Type', 'User-Agent']
end

it "uses the correct access token" do
expect(headers['Authorization']).to eql "Bearer abc123"
end

it "is always json" do
expect(headers['Content-Type']).to eql 'application/json'
end

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

it "sets the client version" do
expect(headers['Auth0-Client']).to eql base64_token
end
end
end
12 changes: 6 additions & 6 deletions spec/lib/auth0/mixins/initializer_spec.rb
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
require "spec_helper"

class MockClass
attr_reader :token
attr_reader :token
include Auth0::Mixins::Initializer
include HTTParty
end

describe Auth0::Mixins::Initializer do
let(:params) { { namespace: 'samples.auth0.com' } }
let(:instance) { DummyClassForProxy.include(described_class).new(params) }
let(:instance) { DummyClassForProxy.send(:include, described_class).new(params) }
context 'api v2' do
before do
params[:api_version] = 2
end

it 'sets token when access_token is passed' do
params[:access_token] = '123'

expect(instance.instance_variable_get('@token')).to eq('123')
end

it 'sets token when token is passed' do
params[:token] = '123'
expect(instance.instance_variable_get('@token')).to eq('123')

expect(instance.instance_variable_get('@token')).to eq('123')
end
end
end