Skip to content

Commit 6470d19

Browse files
committed
feat: correctly identify javascript and css content types
Ensure the correct rack app handles the request
1 parent eb1cd32 commit 6470d19

File tree

3 files changed

+9
-11
lines changed

3 files changed

+9
-11
lines changed

lib/rack/pact_broker/accepts_html_filter.rb

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,29 @@
1+
# Decides whether this is a browser request or a request for the API
12
module Rack
23
module PactBroker
34
class AcceptsHtmlFilter
4-
55
def initialize app
66
@app = app
77
end
88

99
def call env
10-
if accepts_html_and_not_json_or_csv env
10+
if accepts_web_content_types_and_not_api_media env
1111
@app.call(env)
1212
else
1313
[404, {},[]]
1414
end
1515
end
1616

17-
def accepts_html_and_not_json_or_csv env
17+
def accepts_web_content_types_and_not_api_media env
1818
accept = env['HTTP_ACCEPT'] || ''
19-
accepts_html(accept) && !accepts_json_or_csv(accept)
19+
accepts_web_content_types(accept) && !accepts_api_content_types(accept)
2020
end
2121

22-
def accepts_html(accept)
23-
accept.include?("html")
22+
def accepts_web_content_types(accept)
23+
accept.include?("*/*") || accept.include?("html") || accept.include?("text/css") || accept.include?("text/javascript")
2424
end
2525

26-
def accepts_json_or_csv accept
26+
def accepts_api_content_types accept
2727
accept.include?("json") || accept.include?("csv")
2828
end
2929
end

lib/rack/pact_broker/convert_404_to_hal.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ def initialize app
99
def call env
1010
response = @app.call(env)
1111

12-
if response.first == 404 && response[1]['Content-Type'] == 'text/html' && !(env['HTTP_ACCEPT'] =~ /html/)
12+
if response.first == 404 && response[1]['Content-Type'] == 'text/html' && !(env['HTTP_ACCEPT'] =~ /html|javascript|css/)
1313
[404, { 'Content-Type' => 'application/hal+json'},[]]
1414
else
1515
response

lib/rack/pact_broker/convert_file_extension_to_accept_header.rb

+1-3
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@ class ConvertFileExtensionToAcceptHeader
1111
".csv" => "text/csv",
1212
".svg" => "image/svg+xml",
1313
".json" => "application/hal+json",
14-
".yaml" => "application/yaml",
15-
".css" => "text/css",
16-
".js" => "text/javascript"
14+
".yaml" => "application/yaml"
1715
}
1816

1917
def initialize app

0 commit comments

Comments
 (0)