Skip to content

Commit 4cd48aa

Browse files
authored
Add spec coverage for all content hashes (#202)
1 parent e96c16f commit 4cd48aa

2 files changed

Lines changed: 83 additions & 5 deletions

File tree

spec/request_drivers/action_controller_spec.rb

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,43 @@
44

55
describe ApiAuth::RequestDrivers::ActionControllerRequest do
66
let(:timestamp) { Time.now.utc.httpdate }
7+
let(:content_sha256) { '47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU=' }
78

89
let(:request) do
910
ActionController::Request.new(
1011
'AUTHORIZATION' => 'APIAuth 1044:12345',
1112
'PATH_INFO' => '/resource.xml',
1213
'QUERY_STRING' => 'foo=bar&bar=foo',
1314
'REQUEST_METHOD' => 'PUT',
14-
'HTTP_X_AUTHORIZATION_CONTENT_SHA256' => '47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU=',
15+
'HTTP_X_AUTHORIZATION_CONTENT_SHA256' => content_sha256,
16+
'CONTENT_TYPE' => 'text/plain',
17+
'CONTENT_LENGTH' => '11',
18+
'HTTP_DATE' => timestamp,
19+
'rack.input' => StringIO.new("hello\nworld")
20+
)
21+
end
22+
23+
let(:request2) do
24+
ActionController::Request.new(
25+
'AUTHORIZATION' => 'APIAuth 1044:12345',
26+
'PATH_INFO' => '/resource.xml',
27+
'QUERY_STRING' => 'foo=bar&bar=foo',
28+
'REQUEST_METHOD' => 'PUT',
29+
'X_AUTHORIZATION_CONTENT_SHA256' => content_sha256,
30+
'CONTENT_TYPE' => 'text/plain',
31+
'CONTENT_LENGTH' => '11',
32+
'HTTP_DATE' => timestamp,
33+
'rack.input' => StringIO.new("hello\nworld")
34+
)
35+
end
36+
37+
let(:request3) do
38+
ActionController::Request.new(
39+
'AUTHORIZATION' => 'APIAuth 1044:12345',
40+
'PATH_INFO' => '/resource.xml',
41+
'QUERY_STRING' => 'foo=bar&bar=foo',
42+
'REQUEST_METHOD' => 'PUT',
43+
'X-AUTHORIZATION-CONTENT-SHA256' => content_sha256,
1544
'CONTENT_TYPE' => 'text/plain',
1645
'CONTENT_LENGTH' => '11',
1746
'HTTP_DATE' => timestamp,
@@ -27,7 +56,17 @@
2756
end
2857

2958
it 'gets the content_hash' do
30-
expect(driven_request.content_hash).to eq('47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU=')
59+
expect(driven_request.content_hash).to eq(content_sha256)
60+
end
61+
62+
it 'gets the content_hash for request 2' do
63+
example_request = ApiAuth::RequestDrivers::ActionControllerRequest.new(request2)
64+
expect(example_request.content_hash).to eq(content_sha256)
65+
end
66+
67+
it 'gets the content_hash for request 3' do
68+
example_request = ApiAuth::RequestDrivers::ActionControllerRequest.new(request3)
69+
expect(example_request.content_hash).to eq(content_sha256)
3170
end
3271

3372
it 'gets the request_uri' do
@@ -50,7 +89,7 @@
5089
it 'treats no body as empty string' do
5190
request.env['rack.input'] = StringIO.new
5291
request.env['CONTENT_LENGTH'] = 0
53-
expect(driven_request.calculated_hash).to eq('47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU=')
92+
expect(driven_request.calculated_hash).to eq(content_sha256)
5493
end
5594
end
5695

spec/request_drivers/action_dispatch_spec.rb

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,43 @@
44

55
describe ApiAuth::RequestDrivers::ActionDispatchRequest do
66
let(:timestamp) { Time.now.utc.httpdate }
7+
let(:content_sha256) { '47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU=' }
78

89
let(:request) do
910
ActionDispatch::Request.new(
1011
'AUTHORIZATION' => 'APIAuth 1044:12345',
1112
'PATH_INFO' => '/resource.xml',
1213
'QUERY_STRING' => 'foo=bar&bar=foo',
1314
'REQUEST_METHOD' => 'PUT',
14-
'HTTP_X_AUTHORIZATION_CONTENT_SHA256' => '47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU=',
15+
'HTTP_X_AUTHORIZATION_CONTENT_SHA256' => content_sha256,
16+
'CONTENT_TYPE' => 'text/plain',
17+
'CONTENT_LENGTH' => '11',
18+
'HTTP_DATE' => timestamp,
19+
'rack.input' => StringIO.new("hello\nworld")
20+
)
21+
end
22+
23+
let(:request2) do
24+
ActionDispatch::Request.new(
25+
'AUTHORIZATION' => 'APIAuth 1044:12345',
26+
'PATH_INFO' => '/resource.xml',
27+
'QUERY_STRING' => 'foo=bar&bar=foo',
28+
'REQUEST_METHOD' => 'PUT',
29+
'X_AUTHORIZATION_CONTENT_SHA256' => content_sha256,
30+
'CONTENT_TYPE' => 'text/plain',
31+
'CONTENT_LENGTH' => '11',
32+
'HTTP_DATE' => timestamp,
33+
'rack.input' => StringIO.new("hello\nworld")
34+
)
35+
end
36+
37+
let(:request3) do
38+
ActionDispatch::Request.new(
39+
'AUTHORIZATION' => 'APIAuth 1044:12345',
40+
'PATH_INFO' => '/resource.xml',
41+
'QUERY_STRING' => 'foo=bar&bar=foo',
42+
'REQUEST_METHOD' => 'PUT',
43+
'X-AUTHORIZATION-CONTENT-SHA256' => content_sha256,
1544
'CONTENT_TYPE' => 'text/plain',
1645
'CONTENT_LENGTH' => '11',
1746
'HTTP_DATE' => timestamp,
@@ -27,7 +56,17 @@
2756
end
2857

2958
it 'gets the content_hash' do
30-
expect(driven_request.content_hash).to eq('47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU=')
59+
expect(driven_request.content_hash).to eq(content_sha256)
60+
end
61+
62+
it 'gets the content_hash for request 2' do
63+
example_request = ApiAuth::RequestDrivers::ActionDispatchRequest.new(request2)
64+
expect(example_request.content_hash).to eq(content_sha256)
65+
end
66+
67+
it 'gets the content_hash for request 3' do
68+
example_request = ApiAuth::RequestDrivers::ActionDispatchRequest.new(request3)
69+
expect(example_request.content_hash).to eq(content_sha256)
3170
end
3271

3372
it 'gets the request_uri' do

0 commit comments

Comments
 (0)