|
| 1 | +require 'spec_helper' |
| 2 | +require 'api_auth/middleware/excon' |
| 3 | + |
| 4 | +describe ApiAuth::RequestDrivers::ExconRequest do |
| 5 | + let(:timestamp) { Time.now.utc.httpdate } |
| 6 | + let(:body) { "hello\nworld" } |
| 7 | + let(:method) {'GET'} |
| 8 | + let(:headers) do |
| 9 | + { |
| 10 | + 'Authorization' => 'APIAuth 1044:12345', |
| 11 | + 'content-md5' => '1B2M2Y8AsgTpgAmY7PhCfg==', |
| 12 | + 'content-type' => 'text/plain', |
| 13 | + 'date' => timestamp |
| 14 | + } |
| 15 | + end |
| 16 | + |
| 17 | + let(:request) do |
| 18 | + datum = { path: '/resource.xml', |
| 19 | + method: method, |
| 20 | + headers: headers, |
| 21 | + body: body } |
| 22 | + query_string = '?foo=bar&bar=foo' |
| 23 | + |
| 24 | + ApiAuth::Middleware::ExconRequestWrapper.new(datum, query_string) |
| 25 | + end |
| 26 | + |
| 27 | + subject(:driven_request) { described_class.new(request) } |
| 28 | + |
| 29 | + describe 'getting headers correctly' do |
| 30 | + it 'gets the content_type' do |
| 31 | + expect(driven_request.content_type).to eq('text/plain') |
| 32 | + end |
| 33 | + |
| 34 | + it 'gets the content_md5' do |
| 35 | + expect(driven_request.content_md5).to eq('1B2M2Y8AsgTpgAmY7PhCfg==') |
| 36 | + end |
| 37 | + |
| 38 | + it 'gets the request_uri' do |
| 39 | + expect(driven_request.request_uri).to eq('/resource.xml?foo=bar&bar=foo') |
| 40 | + end |
| 41 | + |
| 42 | + it 'gets the timestamp' do |
| 43 | + expect(driven_request.timestamp).to eq(timestamp) |
| 44 | + end |
| 45 | + |
| 46 | + it 'gets the authorization_header' do |
| 47 | + expect(driven_request.authorization_header).to eq('APIAuth 1044:12345') |
| 48 | + end |
| 49 | + |
| 50 | + describe '#calculated_md5' do |
| 51 | + it 'calculates md5 from the body' do |
| 52 | + expect(driven_request.calculated_md5).to eq('kZXQvrKoieG+Be1rsZVINw==') |
| 53 | + end |
| 54 | + |
| 55 | + context 'no body' do |
| 56 | + let(:body) { nil } |
| 57 | + |
| 58 | + it 'is treated as empty string' do |
| 59 | + expect(driven_request.calculated_md5).to eq('1B2M2Y8AsgTpgAmY7PhCfg==') |
| 60 | + end |
| 61 | + end |
| 62 | + end |
| 63 | + |
| 64 | + describe 'http_method' do |
| 65 | + let(:method) { 'PUT' } |
| 66 | + |
| 67 | + it 'is as passed' do |
| 68 | + expect(driven_request.http_method).to eq(method) |
| 69 | + end |
| 70 | + end |
| 71 | + end |
| 72 | + |
| 73 | + describe 'setting headers correctly' do |
| 74 | + let(:headers) { { 'content-type' => 'text/plain'} } |
| 75 | + |
| 76 | + describe '#populate_content_md5' do |
| 77 | + context 'when there is no content body' do |
| 78 | + let(:body) { nil } |
| 79 | + |
| 80 | + it "doesn't populate content-md5" do |
| 81 | + driven_request.populate_content_md5 |
| 82 | + expect(request.headers['Content-MD5']).to be_nil |
| 83 | + end |
| 84 | + end |
| 85 | + |
| 86 | + context 'when there is a content body' do |
| 87 | + let(:body) { "hello\nworld" } |
| 88 | + |
| 89 | + it 'populates content-md5' do |
| 90 | + driven_request.populate_content_md5 |
| 91 | + expect(request.headers['Content-MD5']).to eq('kZXQvrKoieG+Be1rsZVINw==') |
| 92 | + end |
| 93 | + |
| 94 | + it 'refreshes the cached headers' do |
| 95 | + driven_request.populate_content_md5 |
| 96 | + expect(driven_request.content_md5).to eq('kZXQvrKoieG+Be1rsZVINw==') |
| 97 | + end |
| 98 | + end |
| 99 | + end |
| 100 | + |
| 101 | + describe '#set_date' do |
| 102 | + before do |
| 103 | + allow(Time).to receive_message_chain(:now, :utc, :httpdate).and_return(timestamp) |
| 104 | + end |
| 105 | + |
| 106 | + it 'sets the date header of the request' do |
| 107 | + driven_request.set_date |
| 108 | + expect(request.headers['DATE']).to eq(timestamp) |
| 109 | + end |
| 110 | + |
| 111 | + it 'refreshes the cached headers' do |
| 112 | + driven_request.set_date |
| 113 | + expect(driven_request.timestamp).to eq(timestamp) |
| 114 | + end |
| 115 | + end |
| 116 | + |
| 117 | + describe '#set_auth_header' do |
| 118 | + it 'sets the auth header' do |
| 119 | + driven_request.set_auth_header('APIAuth 1044:54321') |
| 120 | + expect(request.headers['Authorization']).to eq('APIAuth 1044:54321') |
| 121 | + end |
| 122 | + end |
| 123 | + end |
| 124 | + |
| 125 | + describe 'md5_mismatch?' do |
| 126 | + context 'when there is no content body' do |
| 127 | + let(:body) { nil } |
| 128 | + |
| 129 | + it 'is false' do |
| 130 | + expect(driven_request.md5_mismatch?).to be false |
| 131 | + end |
| 132 | + end |
| 133 | + |
| 134 | + context 'when there is a content body' do |
| 135 | + let(:body) { "hello\nworld" } |
| 136 | + |
| 137 | + context 'when calculated matches sent' do |
| 138 | + before do |
| 139 | + request.headers['Content-MD5'] = 'kZXQvrKoieG+Be1rsZVINw==' |
| 140 | + end |
| 141 | + |
| 142 | + it 'is false' do |
| 143 | + expect(driven_request.md5_mismatch?).to be false |
| 144 | + end |
| 145 | + end |
| 146 | + |
| 147 | + context "when calculated doesn't match sent" do |
| 148 | + before do |
| 149 | + request.headers['Content-MD5'] = '3' |
| 150 | + end |
| 151 | + |
| 152 | + it 'is true' do |
| 153 | + expect(driven_request.md5_mismatch?).to be true |
| 154 | + end |
| 155 | + end |
| 156 | + end |
| 157 | + end |
| 158 | +end |
0 commit comments