From 768e6bbaa04b2b3fee176b82024d91fe673bad2c Mon Sep 17 00:00:00 2001 From: Ryo Nakamura Date: Fri, 18 Nov 2022 11:41:49 +0900 Subject: [PATCH] Fix `BetterErrors::Middleware` for Content-Type that includes non MIME-Type parts --- lib/better_errors/middleware.rb | 2 +- spec/better_errors/middleware_spec.rb | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/lib/better_errors/middleware.rb b/lib/better_errors/middleware.rb index 34b5efa7..99e3fa4f 100644 --- a/lib/better_errors/middleware.rb +++ b/lib/better_errors/middleware.rb @@ -173,7 +173,7 @@ def internal_call(env, id, method) body = JSON.parse(request.body.read) return invalid_csrf_token_json_response unless request.cookies[CSRF_TOKEN_COOKIE_NAME] == body['csrfToken'] - return not_acceptable_json_response unless request.content_type == 'application/json' + return not_acceptable_json_response unless request.media_type == 'application/json' response = @error_page.send("do_#{method}", body) [200, { "Content-Type" => "application/json; charset=utf-8" }, [JSON.dump(response)]] diff --git a/spec/better_errors/middleware_spec.rb b/spec/better_errors/middleware_spec.rb index 0cdd8216..7b47904f 100644 --- a/spec/better_errors/middleware_spec.rb +++ b/spec/better_errors/middleware_spec.rb @@ -493,6 +493,20 @@ def initialize(message, original_exception = nil) end end + context 'when the Content-Type of the request is application/json; charset=utf-8' do + before do + request_env['CONTENT_TYPE'] = 'application/json; charset=utf-8' + end + + it 'returns JSON containing the eval result' do + expect(error_page).to receive(:do_eval).and_return(prompt: '#', result: "much_stuff_here") + expect(json_body).to match( + 'prompt' => '#', + 'result' => 'much_stuff_here', + ) + end + end + context 'when the Content-Type of the request is application/json' do before do request_env['HTTP_CONTENT_TYPE'] = 'application/json'