From a6c0b106530854fcff08678e3a8f36debbecb435 Mon Sep 17 00:00:00 2001 From: Benjamin Fleischer Date: Sun, 21 Jan 2024 21:32:00 -0600 Subject: [PATCH 1/2] feat: teach JR in tests to parse the response --- lib/jsonapi/resources/railtie.rb | 13 +++++++++++++ test/integration/requests/request_test.rb | 6 +++--- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/lib/jsonapi/resources/railtie.rb b/lib/jsonapi/resources/railtie.rb index 8e72eb01..43d9f5c3 100644 --- a/lib/jsonapi/resources/railtie.rb +++ b/lib/jsonapi/resources/railtie.rb @@ -6,6 +6,19 @@ class Railtie < ::Rails::Railtie rake_tasks do load 'tasks/check_upgrade.rake' end + + + initializer "jsonapi_resources.testing", after: :initialize do + next unless Rails.env.test? + # Make response.parsed_body work + ActionDispatch::IntegrationTest.register_encoder :api_json, + param_encoder: ->(params) { + params + }, + response_parser: ->(body) { + JSONAPI::MimeTypes.parser.call(body) + } + end end end end diff --git a/test/integration/requests/request_test.rb b/test/integration/requests/request_test.rb index defe3619..008bb2a6 100644 --- a/test/integration/requests/request_test.rb +++ b/test/integration/requests/request_test.rb @@ -73,13 +73,13 @@ def test_post_sessions 'Accept' => JSONAPI::MEDIA_TYPE } assert_jsonapi_response 201 - json_body = JSON.parse(response.body) + json_body = response.parsed_body session_id = json_body["data"]["id"] # Get what we just created get "/sessions/#{session_id}?include=responses" assert_jsonapi_response 200 - json_body = JSON.parse(response.body) + json_body = response.parsed_body assert(json_body.is_a?(Object)); assert(json_body["included"].is_a?(Array)); @@ -87,7 +87,7 @@ def test_post_sessions get "/sessions/#{session_id}?include=responses,responses.paragraph" assert_jsonapi_response 200 - json_body = JSON.parse(response.body) + json_body = response.parsed_body assert_equal("single_textbox", json_body["included"][0]["attributes"]["response_type"]["single_textbox"]); From 75113ed1fbfe885c568a4baa09da16d05cb06523 Mon Sep 17 00:00:00 2001 From: lgebhardt Date: Mon, 22 Jan 2024 14:51:30 -0500 Subject: [PATCH 2/2] use response.parsed_body instead of JSON.parse when evaluating responses in tests --- test/helpers/functional_helpers.rb | 4 ++-- test/integration/requests/request_test.rb | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/test/helpers/functional_helpers.rb b/test/helpers/functional_helpers.rb index 3d6dc9d3..b3ce85f8 100644 --- a/test/helpers/functional_helpers.rb +++ b/test/helpers/functional_helpers.rb @@ -53,7 +53,7 @@ module FunctionalHelpers # end # def json_response - JSON.parse(@response.body) + @response.parsed_body end end -end \ No newline at end of file +end diff --git a/test/integration/requests/request_test.rb b/test/integration/requests/request_test.rb index 008bb2a6..52531514 100644 --- a/test/integration/requests/request_test.rb +++ b/test/integration/requests/request_test.rb @@ -348,7 +348,7 @@ def test_post_polymorphic_with_has_many_relationship assert_jsonapi_response 201 - body = JSON.parse(response.body) + body = response.parsed_body person = Person.find(body.dig("data", "id")) assert_equal "Reo", person.name @@ -649,7 +649,7 @@ def test_patch_polymorphic_with_has_many_relationship assert_jsonapi_response 200 - body = JSON.parse(response.body) + body = response.parsed_body person = Person.find(body.dig("data", "id")) assert_equal "Reo", person.name