-
-
Notifications
You must be signed in to change notification settings - Fork 176
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: extract logic for determining if a request is for UI or API
- Loading branch information
Showing
3 changed files
with
86 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
# frozen_string_literal: true | ||
|
||
module Rack | ||
module PactBroker | ||
module RequestTarget | ||
extend self | ||
|
||
WEB_ASSET_EXTENSIONS = %w[.js .woff .woff2 .css .png .html .map .ttf .ico].freeze | ||
API_CONTENT_TYPES = %w[application/hal+json application/json text/csv application/yaml].freeze | ||
|
||
def request_for_ui?(env) | ||
!(request_for_api?(env)) | ||
end | ||
|
||
def request_for_api?(env) | ||
explicit_request_for_api(env) || no_accept_header(env) || (accept_all(env) && !is_web_extension(env)) | ||
end | ||
|
||
private | ||
|
||
def body_is_json(env) | ||
env['CONTENT_TYPE']&.include?("json") | ||
end | ||
|
||
def explicit_request_for_api(env) | ||
accepts_api_content_type(env) || body_is_api_content_type(env) | ||
end | ||
|
||
def accepts_api_content_type(env) | ||
is_api_content_type((env['HTTP_ACCEPT']&.downcase) || "") | ||
end | ||
|
||
def body_is_api_content_type(env) | ||
is_api_content_type((env['CONTENT_TYPE']&.downcase) || "") | ||
end | ||
|
||
def is_api_content_type(header) | ||
API_CONTENT_TYPES.any?{ |content_type| header.include?(content_type) } | ||
end | ||
|
||
# default curl Accept header | ||
# Also used by browsers to request various web assets like woff files | ||
def accept_all(env) | ||
env['HTTP_ACCEPT'] == "*/*" | ||
end | ||
|
||
# No browser ever makes a request without an accept header, so it must be an API | ||
# request if there is no Accept header | ||
def no_accept_header(env) | ||
env['HTTP_ACCEPT'] == nil || env['HTTP_ACCEPT'] == "" | ||
end | ||
|
||
def is_web_extension(env) | ||
env['PATH_INFO'].end_with?(*WEB_ASSET_EXTENSIONS) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 22 additions & 29 deletions
51
...ack/pact_broker/ui_request_filter_spec.rb → ...b/rack/pact_broker/request_target_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters