Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New feature: Acceptable #791

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/http/chainable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ def nodelay
end

# Turn on given features. Available features are:
# * acceptable
# * auto_inflate
# * auto_deflate
# * instrumentation
Expand Down
1 change: 1 addition & 0 deletions lib/http/feature.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ def on_error(_request, _error); end

require "http/features/auto_inflate"
require "http/features/auto_deflate"
require "http/features/acceptable"
require "http/features/logging"
require "http/features/instrumentation"
require "http/features/normalize_uri"
43 changes: 43 additions & 0 deletions lib/http/features/acceptable.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# frozen_string_literal: true

module HTTP
module Features
class Acceptable < Feature
def wrap_response(response)
return response if accepted?(response)

Response.new(
status: 406,
version: response.version,
headers: response.headers,
proxy_headers: response.proxy_headers,
connection: response.connection,
body: response.body,
request: response.request
)
end

private

def accepted?(response)
accept = response.request[Headers::ACCEPT]

return true unless accept

ranges = accept.split(/\s*,\s*/).map { |r| r.gsub(/\s*;.*/, "") }
ranges.any? { |range| match?(response.mime_type, range) }
end

def match?(mime_type, range)
return true if range == "*/*"

m1, m2 = mime_type.split("/", 2)
r1, r2 = range.split("/", 2)

m1 == r1 && ["*", m2].include?(r2)
end

HTTP::Options.register_feature(:acceptable, self)
end
end
end
115 changes: 115 additions & 0 deletions spec/lib/http/features/acceptable_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
# frozen_string_literal: true

RSpec.describe HTTP::Features::Acceptable do
subject(:feature) { described_class.new }

let(:connection) { double }
let(:headers) { {} }

describe "#wrap_response" do
subject(:result) { feature.wrap_response(response) }

let(:request) do
HTTP::Request.new(
verb: :get,
uri: "https://example.com/",
headers: headers
)
end
let(:response) do
HTTP::Response.new(
version: "1.1",
status: 200,
headers: { "content-type": "text/html; charset=utf-8" },
connection: connection,
request: request
)
end

context "when there is no Accept header" do
it "returns original request" do
expect(result).to be response
end
end

context "when MIME type matches single range" do
let(:headers) { { accept: "text/html" } }

it "returns original request" do
expect(result).to be response
end
end

context "when MIME type matches range with parameter" do
let(:headers) { { accept: "text/html; q=1" } }

it "returns original request" do
expect(result).to be response
end
end

context "when MIME type matches one of multiple ranges" do
let(:headers) { { accept: "text/plain, text/html, image/gif" } }

it "returns original request" do
expect(result).to be response
end
end

context "when type matches and subtype does not" do
let(:headers) { { accept: "text/plain" } }

it "returns synthetic 406 status" do
expect(result.code).to be 406
end

it "returns original version" do
expect(result.version).to be response.version
end

it "returns original headers" do
expect(result.headers).to eq response.headers
end

it "returns original connection" do
expect(result.connection).to be response.connection
end

it "returns original request" do
expect(result.request).to be request
end
end

context "when both type and subtype do not match" do
let(:headers) { { accept: "image/gif" } }

it "returns original request" do
expect(result.code).to be 406
end
end

context "when range is */*" do
let(:headers) { { accept: "*/*" } }

it "returns original request" do
expect(result).to be response
end
end

context "when type matches and subtype is wildcard" do
let(:headers) { { accept: "text/*" } }

it "returns original request" do
expect(result).to be response
end
end

context "when type does not match and subtype is wildcard" do
let(:headers) { { accept: "image/*" } }

it "returns original request" do
expect(result.code).to be 406
end
end
end
end
Loading