Skip to content

Commit 3f20d6f

Browse files
authored
feat: Allow JSON request with a vendor specific content type (#34)
We found Apia was rejecting webhook requests from distribution as the content type was not `application/json` but `application/vnd.docker.distribution.events.v2+json` This change allows for either standard json content type (ie: application/json) or a vendor specific type with a "+json" suffix (eg: application/vnd.docker.distribution.events.v2+json)
1 parent cd44cb3 commit 3f20d6f

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

lib/apia/request.rb

+3-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,9 @@ def parse_json_from_string(body)
5858
end
5959

6060
def get_json_body_from_body
61-
return unless content_type =~ /\Aapplication\/json/
61+
# Allow for either standard json content type (ie: application/json)
62+
# or a vendor specific type with a json suffix (eg: application/vnd.docker.distribution.events.v2+json)
63+
return unless content_type =~ /\Aapplication\/(|.*\+)json/
6264
return unless body?
6365

6466
parse_json_from_string(body.read)

spec/specs/apia/request_spec.rb

+8-2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
end
1313

1414
context '#json_body' do
15-
it 'should return nil if the content type is not application/json' do
16-
request = Apia::Request.new(Rack::MockRequest.env_for('/'))
15+
it 'should return nil if the content type is not json' do
16+
request = Apia::Request.new(Rack::MockRequest.env_for('/', 'CONTENT_TYPE' => 'application/vnd.docker.distribution.events.v2-json', :input => '{"name":"Lauren"}'))
1717
expect(request.json_body).to be nil
1818
end
1919

@@ -23,6 +23,12 @@
2323
expect(request.json_body['name']).to eq 'Lauren'
2424
end
2525

26+
it 'should return a hash when valid JSON is provided with a vendor specific json content type' do
27+
request = Apia::Request.new(Rack::MockRequest.env_for('/', 'CONTENT_TYPE' => 'application/vnd.docker.distribution.events.v2+json', :input => '{"name":"Lauren"}'))
28+
expect(request.json_body).to be_a Hash
29+
expect(request.json_body['name']).to eq 'Lauren'
30+
end
31+
2632
it 'should return an empty hash when the body is missing but the content type is provided' do
2733
request = Apia::Request.new(Rack::MockRequest.env_for('/', 'CONTENT_TYPE' => 'application/json', :input => ''))
2834
expect(request.json_body).to be_a Hash

0 commit comments

Comments
 (0)