|
| 1 | +require 'spec_helper' |
| 2 | + |
| 3 | +describe ApiAuth::RequestDrivers::TyphoeusRequest do |
| 4 | + let(:timestamp) { Time.now.utc.httpdate } |
| 5 | + |
| 6 | + let(:request_url) { 'https://example.com/resource.xml?foo=bar&bar=foo' } |
| 7 | + let(:upload_file) { File.open('spec/fixtures/upload.png', 'r') } |
| 8 | + |
| 9 | + let(:request_headers) do |
| 10 | + { |
| 11 | + 'Authorization' => 'APIAuth 1044:12345', |
| 12 | + 'content-md5' => '1B2M2Y8AsgTpgAmY7PhCfg==', |
| 13 | + 'content-type' => 'text/plain', |
| 14 | + 'date' => timestamp |
| 15 | + } |
| 16 | + end |
| 17 | + |
| 18 | + let(:upload_request) do |
| 19 | + Typhoeus::Request.new(request_url, method: :put, headers: request_headers, body: { file: upload_file }) |
| 20 | + end |
| 21 | + |
| 22 | + let(:request) do |
| 23 | + Typhoeus::Request.new(request_url, method: :put, headers: request_headers, body: "hello\nworld") |
| 24 | + end |
| 25 | + |
| 26 | + subject(:driven_request) { ApiAuth::RequestDrivers::TyphoeusRequest.new(request) } |
| 27 | + |
| 28 | + describe 'getting headers correctly' do |
| 29 | + describe '#content_type' do |
| 30 | + it 'gets the content_type' do |
| 31 | + expect(driven_request.content_type).to eq('text/plain') |
| 32 | + end |
| 33 | + end |
| 34 | + |
| 35 | + it 'gets the content_md5' do |
| 36 | + expect(driven_request.content_md5).to eq('1B2M2Y8AsgTpgAmY7PhCfg==') |
| 37 | + end |
| 38 | + |
| 39 | + it 'gets the request_uri' do |
| 40 | + expect(driven_request.request_uri).to eq('/resource.xml?foo=bar&bar=foo') |
| 41 | + end |
| 42 | + |
| 43 | + it 'gets the timestamp' do |
| 44 | + expect(driven_request.timestamp).to eq(timestamp) |
| 45 | + end |
| 46 | + |
| 47 | + it 'gets the authorization_header' do |
| 48 | + expect(driven_request.authorization_header).to eq('APIAuth 1044:12345') |
| 49 | + end |
| 50 | + |
| 51 | + describe '#calculated_md5' do |
| 52 | + it 'calculates md5 from the body' do |
| 53 | + expect(driven_request.calculated_md5).to eq('kZXQvrKoieG+Be1rsZVINw==') |
| 54 | + end |
| 55 | + |
| 56 | + it 'treats no body as empty string' do |
| 57 | + request.options[:body] = nil |
| 58 | + expect(driven_request.calculated_md5).to eq('1B2M2Y8AsgTpgAmY7PhCfg==') |
| 59 | + end |
| 60 | + |
| 61 | + context 'file upload' do |
| 62 | + let(:request) { upload_request } |
| 63 | + |
| 64 | + it 'calculates correctly for multipart content' do |
| 65 | + expect(driven_request.calculated_md5).to eq('3wNtEzQ9ZdXZnkyg/glH1g==') |
| 66 | + end |
| 67 | + end |
| 68 | + end |
| 69 | + |
| 70 | + describe 'http_method' do |
| 71 | + context 'when put request' do |
| 72 | + let(:request) { Typhoeus::Request.new(request_url, method: :put, headers: request_headers) } |
| 73 | + |
| 74 | + it 'returns upcased put' do |
| 75 | + expect(driven_request.http_method).to eq('PUT') |
| 76 | + end |
| 77 | + end |
| 78 | + |
| 79 | + context 'when get request' do |
| 80 | + let(:request) { Typhoeus::Request.new(request_url, method: :get, headers: request_headers) } |
| 81 | + |
| 82 | + it 'returns upcased get' do |
| 83 | + expect(driven_request.http_method).to eq('GET') |
| 84 | + end |
| 85 | + end |
| 86 | + end |
| 87 | + end |
| 88 | + |
| 89 | + describe 'setting headers correctly' do |
| 90 | + let(:request_headers) do |
| 91 | + { |
| 92 | + 'content-type' => 'text/plain' |
| 93 | + } |
| 94 | + end |
| 95 | + |
| 96 | + let(:request) do |
| 97 | + Typhoeus::Request.new(request_url, method: :put, headers: request_headers) |
| 98 | + end |
| 99 | + |
| 100 | + describe '#populate_content_md5' do |
| 101 | + context 'when request type has no body' do |
| 102 | + let(:request) do |
| 103 | + Typhoeus::Request.new(request_url, method: :get, headers: request_headers) |
| 104 | + end |
| 105 | + |
| 106 | + it "doesn't populate content-md5" do |
| 107 | + driven_request.populate_content_md5 |
| 108 | + expect(request.options[:headers]['Content-MD5']).to be_nil |
| 109 | + end |
| 110 | + end |
| 111 | + |
| 112 | + context 'when request type has a body' do |
| 113 | + let(:request) do |
| 114 | + Typhoeus::Request.new(request_url, method: :put, headers: request_headers, body: "hello\nworld") |
| 115 | + end |
| 116 | + |
| 117 | + it 'populates content-md5' do |
| 118 | + driven_request.populate_content_md5 |
| 119 | + expect(request.options[:headers]['Content-MD5']).to eq('kZXQvrKoieG+Be1rsZVINw==') |
| 120 | + end |
| 121 | + |
| 122 | + it 'refreshes the cached headers' do |
| 123 | + driven_request.populate_content_md5 |
| 124 | + expect(driven_request.content_md5).to eq('kZXQvrKoieG+Be1rsZVINw==') |
| 125 | + end |
| 126 | + end |
| 127 | + end |
| 128 | + |
| 129 | + describe '#set_date' do |
| 130 | + before do |
| 131 | + allow(Time).to receive_message_chain(:now, :utc, :httpdate).and_return(timestamp) |
| 132 | + end |
| 133 | + |
| 134 | + it 'sets the date header of the request' do |
| 135 | + driven_request.set_date |
| 136 | + expect(request.options[:headers]['Date']).to eq(timestamp) |
| 137 | + end |
| 138 | + |
| 139 | + it 'refreshes the cached headers' do |
| 140 | + driven_request.set_date |
| 141 | + expect(driven_request.timestamp).to eq(timestamp) |
| 142 | + end |
| 143 | + end |
| 144 | + |
| 145 | + describe '#set_auth_header' do |
| 146 | + it 'sets the auth header' do |
| 147 | + driven_request.set_auth_header('APIAuth 1044:54321') |
| 148 | + expect(request.options[:headers]['Authorization']).to eq('APIAuth 1044:54321') |
| 149 | + end |
| 150 | + end |
| 151 | + end |
| 152 | + |
| 153 | + describe 'md5_mismatch?' do |
| 154 | + context 'when request type has no body' do |
| 155 | + let(:request) do |
| 156 | + Typhoeus::Request.new(request_url, method: :get, headers: request_headers) |
| 157 | + end |
| 158 | + |
| 159 | + it 'is false' do |
| 160 | + expect(driven_request.md5_mismatch?).to be false |
| 161 | + end |
| 162 | + end |
| 163 | + |
| 164 | + context 'when request type has a body' do |
| 165 | + let(:request) do |
| 166 | + Typhoeus::Request.new(request_url, method: :put, headers: request_headers, body: "hello\world") |
| 167 | + end |
| 168 | + |
| 169 | + context 'when calculated matches sent' do |
| 170 | + before do |
| 171 | + request.options[:headers]['Content-MD5'] = '/F4DjTilcDIIVEHn/nAQsA==' |
| 172 | + end |
| 173 | + |
| 174 | + it 'is false' do |
| 175 | + expect(driven_request.md5_mismatch?).to be false |
| 176 | + end |
| 177 | + end |
| 178 | + |
| 179 | + context "when calculated doesn't match sent" do |
| 180 | + before do |
| 181 | + request.options[:headers]['Content-MD5'] = '3' |
| 182 | + end |
| 183 | + |
| 184 | + it 'is true' do |
| 185 | + expect(driven_request.md5_mismatch?).to be true |
| 186 | + end |
| 187 | + end |
| 188 | + end |
| 189 | + end |
| 190 | + |
| 191 | + describe 'fetch_headers' do |
| 192 | + it 'returns request headers' do |
| 193 | + expect(driven_request.fetch_headers).to include('CONTENT-TYPE' => 'text/plain') |
| 194 | + end |
| 195 | + end |
| 196 | +end |
0 commit comments