Skip to content

Commit

Permalink
allow protobuf mime type (#96)
Browse files Browse the repository at this point in the history
  • Loading branch information
David Elliott authored Nov 12, 2020
1 parent d3750b9 commit aefc5b2
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
15 changes: 11 additions & 4 deletions lib/stitches/valid_mime_type.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
require_relative 'allowlist_middleware'
module Stitches
# A middleware that requires all API calls to be for versioned JSON. This means that the Accept
# header (available to Rack apps as HTTP_ACCEPT) should be like so:
# A middleware that requires all API calls to be for versioned JSON or Protobuf.
#
# This means that the Accept header (available to Rack apps as HTTP_ACCEPT) should be like so:
#
# application/json; version=1
#
# This just checks that you've specified some numeric version. ApiVersionConstraint should be used
# to "lock down" the versions you accept.
#
# Or in the case of a protobuf encoded payload the header should be like so:
#
# application/protobuf
#
# There isn't an accepted standard for protobuf encoded payloads but this form is common.
class ValidMimeType < Stitches::AllowlistMiddleware

protected

def do_call(env)
accept = String(env["HTTP_ACCEPT"])
if accept =~ %r{application/json} && accept =~ %r{version=\d+}
if (accept =~ %r{application/json} && accept =~ %r{version=\d+}) || accept =~ %r{application/protobuf}
@app.call(env)
else
not_acceptable_response(accept)
Expand All @@ -24,7 +31,7 @@ def do_call(env)

def not_acceptable_response(accept_header)
status = 406
body = "Not Acceptable - '#{accept_header}' didn't have the right mime type or version number. We only accept application/json with a version"
body = "Not Acceptable - '#{accept_header}' didn't have the right mime type or version number. We only accept application/json with a version or application/protobuf"
header = { "WWW-Authenticate" => accept_header }
Rack::Response.new(body, status, header).finish
end
Expand Down
16 changes: 16 additions & 0 deletions spec/valid_mime_type_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,22 @@
end
end

context "protbuf mime type" do
let(:env) {
{
"PATH_INFO" => "/api/ping",
"HTTP_ACCEPT" => "application/protobuf",
}
}

before do
@response = middleware.call(env)
end
it "calls through to the rest of the chain" do
expect(app).to have_received(:call).with(env)
end
end

context "unacceptable responses" do
before do
@response = middleware.call(env)
Expand Down

0 comments on commit aefc5b2

Please sign in to comment.