Skip to content

Commit

Permalink
When passing lambda as body, to to_return_json, it is evaluated at th…
Browse files Browse the repository at this point in the history
…e time of returning the response and not at the time of declaration.
  • Loading branch information
bblimke committed Aug 29, 2023
1 parent d707719 commit 8401c6e
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 5 deletions.
19 changes: 16 additions & 3 deletions lib/webmock/request_stub.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,25 @@ 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)
json_body = if body.respond_to?(:call)
->(request_signature) {
b = if body.respond_to?(:arity) && body.arity == 1
body.call(request_signature)
else
body.call
end
b = b.to_json unless b.is_a?(String)
b
}
elsif !body.is_a?(String)
body.to_json
else
body
end

resp_h.merge(
headers: {content_type: 'application/json'}.merge(headers.to_h),
body: body
body: json_body
)
end

Expand Down
19 changes: 19 additions & 0 deletions spec/acceptance/shared/returning_declared_responses.rb
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,25 @@ def call(request)
end
end

describe "when response was declared via to_return_json" do
describe "and declared response body is lamba" do
it "should evaluate the response body at the time of returning the response" do
record = double("SomeRecord")
allow(record).to receive_messages(to_json: '{"what":"something callable"}.')

stub_request(:get, "www.example.com").to_return_json(body: -> {record})

allow(record).to receive_messages(to_json: '{"what":"something else callable"}.')
expect(http_request(:get, "http://www.example.com/").body).to eq('{"what":"something else callable"}.')
end

it "should evaluate the response body with request passed as argument to lambda" do
stub_request(:get, "www.example.com").to_return_json(body: ->(request) {"\"#{request.uri.to_s}\""})
expect(http_request(:get, "http://www.example.com/").body).to eq("\"http://www.example.com:80/\"")
end
end
end

describe "when response was declared as lambda" do
it "should return evaluated response body" do
stub_request(:post, "www.example.com").to_return(lambda {|request|
Expand Down
16 changes: 14 additions & 2 deletions spec/unit/request_stub_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,24 @@
expect(@request_stub.response.body).to eq('{"abc":"def"}')
end

it "should json-ify any callable proc or lambda to body" do
it "should evaluate a callable proc or lambda body at the time of response being returned and not at the time of stub declaration" do
record = double("SomeRecord")
allow(record).to receive_messages(to_json: '{"what":"something callable"}.')

@request_stub.to_return_json(body: (->(request) { record }))

allow(record).to receive_messages(to_json: '{"what":"something else callable"}.')
expect(@request_stub.response.body.call(double(WebMock::RequestSignature))).to eq('{"what":"something else callable"}.')
end

it "should evaluate a callable proc or lambda body, even if it takes no args" 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"}.')

allow(record).to receive_messages(to_json: '{"what":"something else callable"}.')
expect(@request_stub.response.body.call(double(WebMock::RequestSignature))).to eq('{"what":"something else callable"}.')
end

it "should apply the content_type header" do
Expand Down

0 comments on commit 8401c6e

Please sign in to comment.