Skip to content

Commit 18532af

Browse files
committed
Only transform the supplied request body type if type is BINARY
- There were two bugs with the transformation. - Firstly it always altered the body type to String causing issues for other types such as REGEX. - Secondly the transformation for BINARY didn't actually happen because we was comparing BodyType with a symbol which was failing miserably and not actually transforming the body value.
1 parent 90ac447 commit 18532af

File tree

2 files changed

+52
-3
lines changed

2 files changed

+52
-3
lines changed

lib/mockserver/model/request.rb

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,12 @@ class Request < Hashie::Trash
3232
property :cookies, default: Cookies.new([])
3333
property :headers, default: Headers.new([])
3434
property :body, transform_with: (lambda do |body|
35-
is_base_64_body = body && body.type == :BINARY
36-
body_value = is_base_64_body ? Base64.decode64(body.value) : body.value
37-
Body.new(type: :STRING, value: body_value)
35+
if body && body.type.to_s == 'BINARY'
36+
body.type = :STRING
37+
body.value = Base64.decode64(body.value)
38+
end
39+
40+
body
3841
end)
3942

4043
coerce_key :method, HTTPMethod

spec/mockserver/builder_spec.rb

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,4 +87,50 @@
8787
expect(to_camelized_hash(at_least(2))).to eq('unlimited' => 'true', 'remainingTimes' => 2)
8888
end
8989

90+
91+
describe 'transforming request body' do
92+
let(:expectation) { MockServer::Model::Expectation.new }
93+
let(:request) { MockServer::Model::Request.new }
94+
95+
[{type: :XPATH, value: '/mock/instance'},
96+
{type: :REGEX, value: '\d+.\d+'},
97+
{type: :STRING, value: 'Mockserver is great'}].each do |request_hash|
98+
99+
it "generates a request body of type #{request_hash[:type]}" do
100+
request.body = MockServer::Model::Body.new(request_hash)
101+
expectation.request = request
102+
103+
expect(to_camelized_hash(expectation)).to eq({
104+
"httpRequest" => {
105+
"method" => "GET",
106+
"body" => {
107+
"type" => request_hash[:type].to_s,
108+
"value" => request_hash[:value].to_s
109+
}
110+
}
111+
})
112+
end
113+
end
114+
115+
context 'when request body type binary' do
116+
let(:body) do
117+
MockServer::Model::Body.new(type: :BINARY, value: 'TW9ja3NlcnZlciBpcyBncmVhdAo=')
118+
end
119+
120+
it 'correctly transforms to a string and updates the object' do
121+
request.body = body
122+
expectation.request = request
123+
124+
expect(to_camelized_hash(expectation)).to eq({
125+
"httpRequest" => {
126+
"method" => "GET",
127+
"body" => {
128+
"type" => "STRING",
129+
"value" => "Mockserver is great\n"
130+
}
131+
}
132+
})
133+
end
134+
end
135+
end
90136
end

0 commit comments

Comments
 (0)