diff --git a/lib/pact/provider/request.rb b/lib/pact/provider/request.rb index 79b34ca2..d9779055 100644 --- a/lib/pact/provider/request.rb +++ b/lib/pact/provider/request.rb @@ -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 @@ -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 diff --git a/spec/lib/pact/provider/request_spec.rb b/spec/lib/pact/provider/request_spec.rb index fb34692d..d00e5312 100644 --- a/spec/lib/pact/provider/request_spec.rb +++ b/spec/lib/pact/provider/request_spec.rb @@ -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 @@ -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 @@ -127,4 +138,4 @@ end end end -end \ No newline at end of file +end