1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
|
require 'spec_helper.rb'
describe Rack::OAuth2::Util do
let :util do
Rack::OAuth2::Util
end
let :uri do
'http://client.example.com/callback'
end
describe '.www_form_url_encode' do
subject { util.www_form_url_encode '=+ .-/' }
it { should == '%3D%2B+.-%2F' }
end
describe '.www_form_urldecode' do
subject { util.www_form_url_decode '%3D%2B+.-%2F' }
it { should == '=+ .-/' }
end
describe '.base64_encode' do
subject { util.base64_encode '=+ .-/' }
it { should == 'PSsgLi0v' }
end
describe '.compact_hash' do
subject { util.compact_hash k1: 'v1', k2: '', k3: nil }
it { should == {k1: 'v1'} }
end
describe '.parse_uri' do
context 'when String is given' do
it { util.parse_uri(uri).should be_a URI::Generic }
end
context 'when URI is given' do
it 'should be itself' do
_uri_ = URI.parse uri
util.parse_uri(_uri_).should be _uri_
end
end
context 'when invalid URI is given' do
it do
expect do
util.parse_uri '::'
end.to raise_error URI::InvalidURIError
end
end
context 'otherwise' do
it do
expect { util.parse_uri nil }.to raise_error StandardError
expect { util.parse_uri 123 }.to raise_error StandardError
end
end
end
describe '.redirect_uri' do
let(:base_uri) { 'http://client.example.com' }
let(:params) do
{k1: :v1, k2: ''}
end
subject { util.redirect_uri base_uri, location, params }
context 'when location = :fragment' do
let(:location) { :fragment }
it { should == "#{base_uri}##{util.compact_hash(params).to_query}" }
end
context 'when location = :query' do
let(:location) { :query }
it { should == "#{base_uri}?#{util.compact_hash(params).to_query}" }
end
end
describe '.uri_match?' do
context 'when invalid URI is given' do
it do
util.uri_match?('::', '::').should == false
util.uri_match?(123, 'http://client.example.com/other').should == false
util.uri_match?('http://client.example.com/other', nil).should == false
end
end
context 'when exactly same' do
it { util.uri_match?(uri, uri).should == true }
end
context 'when path prefix matches' do
it { util.uri_match?(uri, "#{uri}/deep_path").should == true }
end
context 'otherwise' do
it do
util.uri_match?(uri, 'http://client.example.com/other').should == false
util.uri_match?(uri, 'http://attacker.example.com/callback').should == false
end
end
end
end
|