Skip to content

Commit

Permalink
FI-3487: Fix response content types (#616)
Browse files Browse the repository at this point in the history
* set json content format for all json api responses

* fix content types for other requests
  • Loading branch information
Jammjammjamm authored Feb 14, 2025
1 parent 3074d02 commit 9571dff
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
4 changes: 3 additions & 1 deletion lib/inferno/apps/web/controllers/controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ def self.inherited(subclass)
# Hanami Controller 2.0.0 removes the ability to set a default
# Content-Type response header, so set it manually if it hasn't been
# set.
subclass.after { |_req, res| res.format = :json if res.format == :all && res.body&.first&.first == '{' }
subclass.after do |_req, res|
res.format = :json if res.format == :all
end
end

def self.resource_name
Expand Down
18 changes: 12 additions & 6 deletions lib/inferno/apps/web/router.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@

module Inferno
module Web
client_page = ERB.new(File.read(File.join(Inferno::Application.root, 'lib', 'inferno', 'apps', 'web',
'index.html.erb'))).result
client_page = ERB.new(
File.read(
File.join(Inferno::Application.root, 'lib', 'inferno', 'apps', 'web', 'index.html.erb')
)
).result
CLIENT_PAGE_RESPONSE = ->(_env) { [200, { 'Content-Type' => 'text/html' }, [client_page]] }

base_path = Application['base_path']&.delete_prefix('/')

Expand Down Expand Up @@ -47,12 +51,14 @@ module Web

get '/requests/:id', to: Inferno::Web::Controllers::Requests::Show, as: :requests_show

get '/version', to: ->(_env) { [200, {}, [{ 'version' => Inferno::VERSION.to_s }.to_json]] }, as: :version
get '/version', to: lambda { |_env|
[200, { 'Content-Type' => 'application/json' }, [{ 'version' => Inferno::VERSION.to_s }.to_json]]
}, as: :version
end

# Should not need Content-Type header but GitHub Codespaces will not work without them.
# This could be investigated and likely removed if addressed properly elsewhere.
get '/', to: ->(_env) { [200, { 'Content-Type' => 'text/html' }, [client_page]] }
get '/', to: CLIENT_PAGE_RESPONSE
get '/jwks.json', to: lambda { |_env|
[200, { 'Content-Type' => 'application/json' }, [Inferno::JWKS.jwks_json]]
}, as: :jwks
Expand All @@ -70,7 +76,7 @@ module Web

Inferno::Repositories::TestSuites.all.map { |suite| "/#{suite.id}" }.each do |suite_path|
Application['logger'].info("Registering suite route: #{suite_path}")
get suite_path, to: ->(_env) { [200, {}, [client_page]] }
get suite_path, to: CLIENT_PAGE_RESPONSE
end

get '/test_sessions/:id', to: Inferno::Web::Controllers::TestSessions::ClientShow, as: :client_session_show
Expand All @@ -83,7 +89,7 @@ module Web
if base_path.present?
Hanami::Router.new do
scope("#{base_path}/") do
get '/', to: ->(_env) { [200, { 'Content-Type' => 'text/html' }, [client_page]] }
get '/', to: CLIENT_PAGE_RESPONSE
end
scope(base_path, &route_block)
end
Expand Down

0 comments on commit 9571dff

Please # to comment.