Skip to content

Commit 3519472

Browse files
committed
Merge branch 'karouf-fix-expected-body-when-using-generators'
2 parents 91a6ffc + 6b10fa3 commit 3519472

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

lib/pact/provider/request.rb

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def body
2929
when String then expected_request.body
3030
when NullExpectation then ''
3131
else
32-
Pact::Generators.apply_generators(expected_request, "body", reified_body, @state_params)
32+
generated_body
3333
end
3434
end
3535

@@ -60,6 +60,19 @@ def reified_body
6060
end
6161
end
6262

63+
def generated_body
64+
result = Pact::Provider::Generators.apply_generators(expected_request, "body", reified_body, @state_params)
65+
66+
case result
67+
when Hash
68+
result.to_json
69+
when String
70+
result
71+
else
72+
raise "Expected body to be a String or Hash, but was #{result.class} with value #{result.inspect}"
73+
end
74+
end
75+
6376
def rack_request_header_for header
6477
with_http_prefix(header.to_s.upcase).tr('-', '_')
6578
end

spec/lib/pact/provider/request_spec.rb

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@
66
let(:path) { '/path?something' }
77
let(:body) { { a: 'body' } }
88
let(:headers) { {} }
9+
let(:generators) { {} }
910
let(:expected_request) do
1011
instance_double(
1112
'Pact::Request::Expected',
1213
method: 'post',
1314
full_path: path,
1415
body: body,
15-
headers: headers
16+
headers: headers,
17+
generators: generators,
1618
)
1719
end
1820

@@ -69,6 +71,15 @@
6971
it "returns the object as a json string" do
7072
expect(subject.body).to eq body.to_json
7173
end
74+
75+
context "and it uses generators" do
76+
let(:body) { { a: 'body', b: '2025-04-08' } }
77+
let(:generators) { {"body"=>{"b"=>{"type"=>"Date"}}} }
78+
79+
it "returns the object as a json string" do
80+
expect(subject.body).to eq body.to_json
81+
end
82+
end
7283
end
7384
end
7485

@@ -127,4 +138,4 @@
127138
end
128139
end
129140
end
130-
end
141+
end

0 commit comments

Comments
 (0)