Skip to content

Commit

Permalink
feat: correctly identify javascript and css content types
Browse files Browse the repository at this point in the history
Ensure the correct rack app handles the request
  • Loading branch information
bethesque committed Mar 31, 2019
1 parent eb1cd32 commit 6470d19
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 11 deletions.
14 changes: 7 additions & 7 deletions lib/rack/pact_broker/accepts_html_filter.rb
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
# Decides whether this is a browser request or a request for the API
module Rack
module PactBroker
class AcceptsHtmlFilter

def initialize app
@app = app
end

def call env
if accepts_html_and_not_json_or_csv env
if accepts_web_content_types_and_not_api_media env
@app.call(env)
else
[404, {},[]]
end
end

def accepts_html_and_not_json_or_csv env
def accepts_web_content_types_and_not_api_media env
accept = env['HTTP_ACCEPT'] || ''
accepts_html(accept) && !accepts_json_or_csv(accept)
accepts_web_content_types(accept) && !accepts_api_content_types(accept)
end

def accepts_html(accept)
accept.include?("html")
def accepts_web_content_types(accept)
accept.include?("*/*") || accept.include?("html") || accept.include?("text/css") || accept.include?("text/javascript")
end

def accepts_json_or_csv accept
def accepts_api_content_types accept
accept.include?("json") || accept.include?("csv")
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/rack/pact_broker/convert_404_to_hal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def initialize app
def call env
response = @app.call(env)

if response.first == 404 && response[1]['Content-Type'] == 'text/html' && !(env['HTTP_ACCEPT'] =~ /html/)
if response.first == 404 && response[1]['Content-Type'] == 'text/html' && !(env['HTTP_ACCEPT'] =~ /html|javascript|css/)
[404, { 'Content-Type' => 'application/hal+json'},[]]
else
response
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ class ConvertFileExtensionToAcceptHeader
".csv" => "text/csv",
".svg" => "image/svg+xml",
".json" => "application/hal+json",
".yaml" => "application/yaml",
".css" => "text/css",
".js" => "text/javascript"
".yaml" => "application/yaml"
}

def initialize app
Expand Down

0 comments on commit 6470d19

Please sign in to comment.