Skip to content

Commit 28b77d3

Browse files
Merge pull request #238 from jnormington/bugfix/model_request_dsl
Convert the request body correctly when it is a String
2 parents b890000 + e476a97 commit 28b77d3

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

lib/mockserver/model/request.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,12 @@ def request(method, path, &_)
7474
end
7575

7676
def request_from_json(payload)
77+
body = payload['body']
78+
79+
if body && body.is_a?(String)
80+
payload.merge!('body' => { 'type' => :STRING, 'value' => body })
81+
end
82+
7783
request = Request.new(symbolize_keys(payload))
7884
yield request if block_given?
7985
request

spec/mockserver/model/dsl_spec.rb

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
require 'spec_helper'
2+
3+
RSpec.describe 'MockServer::Model::DSL' do
4+
let(:header) { MockServer::Model::Header }
5+
let(:headers) do
6+
[
7+
header.new(name: 'User-Agent', values: ['curl/7.22.0 (x86_64-pc-linux-gnu)']),
8+
header.new(name: 'Host', values: ['localhost:2000']),
9+
header.new(name: 'Accept', values: ['*/*']),
10+
header.new(name: 'Content-Length', values: ['26']),
11+
header.new(name: 'Content-Type', values: ['application/x-www-form-urlencoded'])
12+
]
13+
end
14+
15+
describe '#request_from_json' do
16+
let(:body_content) { 'Hello this is a message' }
17+
let(:request_json) do
18+
{
19+
"method"=>"POST", "path"=>"/message",
20+
"headers"=>[{ "name"=>"User-Agent", "values"=>["curl/7.22.0 (x86_64-pc-linux-gnu)"] },
21+
{"name"=>"Host", "values"=>["localhost:2000"]}, {"name"=>"Accept", "values"=>["*/*"]},
22+
{"name"=>"Content-Length", "values"=>["26"]},
23+
{"name"=>"Content-Type", "values"=>["application/x-www-form-urlencoded"]}],
24+
"keepAlive"=>true, "secure"=>false
25+
}
26+
end
27+
28+
it 'correctly builds the request with no body' do
29+
request = request_from_json(request_json)
30+
31+
expect(request.headers).to eq headers
32+
expect(request.body).to eq nil
33+
end
34+
35+
it 'correctly builds the request with a body' do
36+
request_with_body = request_json.merge('body' => body_content)
37+
request = request_from_json(request_with_body)
38+
39+
expect(request.headers).to eq headers
40+
expect(request.body.type.to_s).to eq 'STRING'
41+
expect(request.body.value).to eq body_content
42+
end
43+
end
44+
end

0 commit comments

Comments
 (0)