Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion lib/pact/provider/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def body
when String then expected_request.body
when NullExpectation then ''
else
Pact::Generators.apply_generators(expected_request, "body", reified_body, @state_params)
generated_body
end
end

Expand Down Expand Up @@ -60,6 +60,19 @@ def reified_body
end
end

def generated_body
result = Pact::Provider::Generators.apply_generators(expected_request, "body", reified_body, @state_params)

case result
when Hash
result.to_json
when String
result
else
raise "Expected body to be a String or Hash, but was #{result.class} with value #{result.inspect}"
end
end

def rack_request_header_for header
with_http_prefix(header.to_s.upcase).tr('-', '_')
end
Expand Down
15 changes: 13 additions & 2 deletions spec/lib/pact/provider/request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@
let(:path) { '/path?something' }
let(:body) { { a: 'body' } }
let(:headers) { {} }
let(:generators) { {} }
let(:expected_request) do
instance_double(
'Pact::Request::Expected',
method: 'post',
full_path: path,
body: body,
headers: headers
headers: headers,
generators: generators,
)
end

Expand Down Expand Up @@ -69,6 +71,15 @@
it "returns the object as a json string" do
expect(subject.body).to eq body.to_json
end

context "and it uses generators" do
let(:body) { { a: 'body', b: '2025-04-08' } }
let(:generators) { {"body"=>{"b"=>{"type"=>"Date"}}} }

it "returns the object as a json string" do
expect(subject.body).to eq body.to_json
end
end
end
end

Expand Down Expand Up @@ -127,4 +138,4 @@
end
end
end
end
end