From 0d673b02774c13499600ad521c6f17793cf54a5a Mon Sep 17 00:00:00 2001 From: Sebastien Savater Date: Mon, 12 Dec 2022 11:44:30 +0100 Subject: [PATCH 1/3] Json-ify any non-string object in #to_return_json --- lib/webmock/request_stub.rb | 2 +- spec/unit/request_stub_spec.rb | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/lib/webmock/request_stub.rb b/lib/webmock/request_stub.rb index 8c831c5c..bd085b80 100644 --- a/lib/webmock/request_stub.rb +++ b/lib/webmock/request_stub.rb @@ -31,7 +31,7 @@ def to_return_json(*response_hashes) headers, body = resp_h.values_at(:headers, :body) resp_h.merge( headers: {content_type: 'application/json'}.merge(headers.to_h), - body: body.is_a?(Hash) ? body.to_json : body + body: body.is_a?(String) ? body : body.to_json ) end diff --git a/spec/unit/request_stub_spec.rb b/spec/unit/request_stub_spec.rb index e7a8acfd..765027be 100644 --- a/spec/unit/request_stub_spec.rb +++ b/spec/unit/request_stub_spec.rb @@ -69,6 +69,24 @@ expect(@request_stub.response.status).to eq([500, ""]) end + it "should json-ify an Array body" do + @request_stub.to_return_json(body: [{abc: "def" }]) + expect(@request_stub.response.body).to eq('[{"abc":"def"}]') + end + + it "should json-ify any object responding to `to_json`" do + record = double("SomeRecord") + allow(record).to receive_messages(to_json: '{"what":"something"}.') + + @request_stub.to_return_json(body: record) + expect(@request_stub.response.body).to eq('{"what":"something"}.') + end + + it "should not over-json-ify a String body" do + @request_stub.to_return_json(body: '{"abc":"def"}') + expect(@request_stub.response.body).to eq('{"abc":"def"}') + end + it "should apply the content_type header" do @request_stub.to_return_json(body: {abc: "def"}, status: 500) expect(@request_stub.response.headers).to eq({"Content-Type"=>"application/json"}) From d1e55fa609989d1deff4f69a9e2eb248a98ba2d9 Mon Sep 17 00:00:00 2001 From: Sebastien Savater Date: Mon, 12 Dec 2022 11:52:37 +0100 Subject: [PATCH 2/3] Add #to_return_json to README --- README.md | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/README.md b/README.md index 9f1d2a22..7499b5ac 100644 --- a/README.md +++ b/README.md @@ -330,6 +330,16 @@ stub_request(:any, "www.example.com"). Net::HTTP.get('www.example.com', '/') # ===> "abc\n" ``` +### Response with JSON body + +```ruby + +stub_request(:any, "www.example.com"). + to_return_json(body: {foo: "bar"}) + +Net::HTTP.get('www.example.com', '/') # ===> "{\"foo\": \"bar\"}" +``` + ### Response with custom status message ```ruby From cc4644f23bedb4d5357c1e3caafb44af2f03fcee Mon Sep 17 00:00:00 2001 From: Sebastien Savater Date: Fri, 19 May 2023 13:07:32 +0200 Subject: [PATCH 3/3] Json-ify procs in #to_return_json --- lib/webmock/request_stub.rb | 6 +++++- spec/unit/request_stub_spec.rb | 8 ++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/lib/webmock/request_stub.rb b/lib/webmock/request_stub.rb index bd085b80..85d971a2 100644 --- a/lib/webmock/request_stub.rb +++ b/lib/webmock/request_stub.rb @@ -29,9 +29,13 @@ def to_return_json(*response_hashes) json_response_hashes = [*response_hashes].flatten.map do |resp_h| headers, body = resp_h.values_at(:headers, :body) + + body = body.call if body.respond_to?(:call) + body = body.to_json unless body.is_a?(String) + resp_h.merge( headers: {content_type: 'application/json'}.merge(headers.to_h), - body: body.is_a?(String) ? body : body.to_json + body: body ) end diff --git a/spec/unit/request_stub_spec.rb b/spec/unit/request_stub_spec.rb index 765027be..c8e3324d 100644 --- a/spec/unit/request_stub_spec.rb +++ b/spec/unit/request_stub_spec.rb @@ -87,6 +87,14 @@ expect(@request_stub.response.body).to eq('{"abc":"def"}') end + it "should json-ify any callable proc or lambda to body" do + record = double("SomeRecord") + allow(record).to receive_messages(to_json: '{"what":"something callable"}.') + + @request_stub.to_return_json(body: -> { record }) + expect(@request_stub.response.body).to eq('{"what":"something callable"}.') + end + it "should apply the content_type header" do @request_stub.to_return_json(body: {abc: "def"}, status: 500) expect(@request_stub.response.headers).to eq({"Content-Type"=>"application/json"})