Skip to content

Commit

Permalink
Merge pull request #32 from auth0/client-headers
Browse files Browse the repository at this point in the history
Use client header spec as per documentation
  • Loading branch information
hzalaz committed May 29, 2015
2 parents 715c634 + 639b254 commit 718a9d4
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 21 deletions.
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

0 comments on commit 718a9d4

Please sign in to comment.