|
16 | 16 | end
|
17 | 17 | end
|
18 | 18 |
|
19 |
| -describe MockServer::MockServerClient do |
20 |
| - |
| 19 | +shared_context 'setup expectation' do |
21 | 20 | let(:client) { MockServer::MockServerClient.new('localhost', 8098) }
|
22 | 21 |
|
23 |
| - before do |
24 |
| - # To suppress logging output to standard output, write to a temporary file |
25 |
| - client.logger = LoggingFactory::DEFAULT_FACTORY.log('test', output: 'tmp.log', truncate: true) |
| 22 | + let(:response_body) { 'responseBody' } |
| 23 | + let(:response_code) { '201' } |
| 24 | + let(:request_path) { '/somePath' } |
| 25 | + let(:param) { nil } |
| 26 | + |
| 27 | + let(:expectation_body) { exact('someBody') } |
| 28 | + let(:request_body) { 'someBody' } |
| 29 | + |
| 30 | + def setup_expectation(request) |
| 31 | + # additional expectation setup |
26 | 32 | end
|
27 | 33 |
|
28 |
| - def create_agent |
29 |
| - uri = URI('http://api.nsa.gov:1337/agent') |
30 |
| - http = Net::HTTP.new(uri.host, uri.port) |
31 |
| - req = Net::HTTP::Post.new(uri.path, 'Content-Type' => 'application/json') |
32 |
| - req.body = { name: 'John Doe', role: 'agent' }.to_json |
33 |
| - res = http.request(req) |
34 |
| - puts "response #{res.body}" |
35 |
| - rescue => e |
36 |
| - puts "failed #{e}" |
| 34 | + def setup_response(response) |
| 35 | + # additional response setup |
37 | 36 | end
|
38 | 37 |
|
39 |
| - it 'setup complex expectation' do |
| 38 | + def setup_request(request) |
| 39 | + # additional request setup |
| 40 | + end |
| 41 | + |
| 42 | + before do |
40 | 43 | WebMock.allow_net_connect!
|
41 | 44 |
|
42 |
| - # given |
| 45 | + # To suppress logging output to standard output, write to a temporary file |
| 46 | + client.logger = LoggingFactory::DEFAULT_FACTORY.log('test', output: 'tmp.log', truncate: true) |
| 47 | + |
43 | 48 | mock_expectation = expectation do |expectation|
|
44 | 49 | expectation.request do |request|
|
| 50 | + request.body = expectation_body |
45 | 51 | request.method = 'POST'
|
46 |
| - request.path = '/somePath' |
47 |
| - request.query_string_parameters << parameter('param', 'someQueryStringValue') |
48 |
| - request.headers << header('Header', 'someHeaderValue') |
49 |
| - request.cookies << cookie('cookie', 'someCookieValue') |
50 |
| - request.body = exact('someBody') |
| 52 | + request.path = request_path |
| 53 | + request.query_string_parameters << parameter('param', param) if param |
| 54 | + |
| 55 | + setup_expectation request |
51 | 56 | end
|
52 | 57 |
|
53 | 58 | expectation.response do |response|
|
54 |
| - response.status_code = 201 |
55 |
| - response.headers << header('header', 'someHeaderValue') |
56 |
| - response.body = body('someBody') |
57 |
| - response.delay = delay_by(:SECONDS, 1) |
| 59 | + response.status_code = response_code |
| 60 | + response.body = body(response_body) |
| 61 | + |
| 62 | + setup_response response |
58 | 63 | end
|
59 | 64 |
|
60 | 65 | expectation.times = exactly(1)
|
61 | 66 | end
|
62 | 67 |
|
63 |
| - # and |
64 |
| - expect(client.register(mock_expectation).code).to eq(201) |
| 68 | + @mock_expectation_code = client.register(mock_expectation).code |
| 69 | + |
| 70 | + url_params = %(?param=#{param}) if param |
65 | 71 |
|
66 | 72 | # when
|
67 |
| - uri = URI('http://localhost:8098/somePath') |
| 73 | + uri = URI('http://localhost:8098') |
68 | 74 | http = Net::HTTP.new(uri.host, uri.port)
|
69 |
| - req = Net::HTTP::Post.new('/somePath?param=someQueryStringValue') |
70 |
| - req['Header'] = 'someHeaderValue' |
71 |
| - req['Cookie'] = 'cookie=someCookieValue' |
72 |
| - req.body = 'someBody' |
73 |
| - res = http.request(req) |
74 |
| - |
75 |
| - # then |
76 |
| - expect(res.code).to eq('201') |
77 |
| - expect(res.body).to eq('someBody') |
| 75 | + req = Net::HTTP::Post.new(%(#{request_path}#{url_params})) |
| 76 | + setup_request req |
| 77 | + req.body = request_body |
| 78 | + @res = http.request(req) |
78 | 79 |
|
79 | 80 | WebMock.disable_net_connect!
|
80 | 81 | end
|
| 82 | +end |
| 83 | + |
| 84 | +shared_examples 'a successful mock expectation' do |
| 85 | + it 'sets the expectation successfully' do |
| 86 | + expect(@mock_expectation_code).to eq(201) |
| 87 | + end |
| 88 | +end |
| 89 | + |
| 90 | +shared_examples 'a successful mock response' do |
| 91 | + it 'returns the expectations response' do |
| 92 | + expect(@res.code).to eq(response_code) |
| 93 | + expect(@res.body).to eq(response_body) |
| 94 | + end |
| 95 | +end |
81 | 96 |
|
| 97 | + |
| 98 | +describe MockServer::MockServerClient do |
| 99 | + context 'when a complex expectation is set up' do |
| 100 | + include_context 'setup expectation' do |
| 101 | + let(:param) { 'someQueryStringValue' } |
| 102 | + |
| 103 | + def setup_expectation(request) |
| 104 | + request.headers << header('Header', 'someHeaderValue') |
| 105 | + request.cookies << cookie('cookie', 'someCookieValue') |
| 106 | + end |
| 107 | + |
| 108 | + def setup_response(response) |
| 109 | + response.headers << header('header', 'someHeaderValue') |
| 110 | + response.delay = delay_by(:SECONDS, 1) |
| 111 | + end |
| 112 | + |
| 113 | + def setup_request(request) |
| 114 | + request['Header'] = 'someHeaderValue' |
| 115 | + request['Cookie'] = 'cookie=someCookieValue' |
| 116 | + end |
| 117 | + end |
| 118 | + |
| 119 | + it_behaves_like 'a successful mock expectation' |
| 120 | + it_behaves_like 'a successful mock response' |
| 121 | + end |
| 122 | + |
| 123 | + context 'when the mock body type is json' do |
| 124 | + include_context 'setup expectation' do |
| 125 | + let(:expectation_body) { json({ param1: 'param1', param2: 'param2' }.to_json) } |
| 126 | + let(:request_body) { { param2: 'param2', param1: 'param1' }.to_json } |
| 127 | + end |
| 128 | + |
| 129 | + it_behaves_like 'a successful mock expectation' |
| 130 | + it_behaves_like 'a successful mock response' |
| 131 | + end |
| 132 | + |
| 133 | + context 'when the mock body type is json schema' do |
| 134 | + include_context 'setup expectation' do |
| 135 | + let(:expectation_body) { |
| 136 | + json_schema({ |
| 137 | + type: 'object', |
| 138 | + properties: { |
| 139 | + id: { type: 'integer' }, |
| 140 | + name: { type: 'string' } |
| 141 | + }, |
| 142 | + required: ['id', 'name'] |
| 143 | + }.to_json) |
| 144 | + } |
| 145 | + let(:request_body) { { id: 123, name: 'bob' }.to_json } |
| 146 | + end |
| 147 | + |
| 148 | + it_behaves_like 'a successful mock expectation' |
| 149 | + it_behaves_like 'a successful mock response' |
| 150 | + end |
| 151 | + |
| 152 | + context 'when the mock body type is xml schema' do |
| 153 | + include_context 'setup expectation' do |
| 154 | + let(:expectation_body) { xml_schema(' |
| 155 | + <xs:element name="id" type="xs:integer"/> |
| 156 | + <xs:element name="name" type="xs:string"/> |
| 157 | + ') } |
| 158 | + let(:request_body) { { id: 123, name: 'bob' }.to_json } |
| 159 | + end |
| 160 | + |
| 161 | + it_behaves_like 'a successful mock expectation' |
| 162 | + it_behaves_like 'a successful mock response' |
| 163 | + end |
82 | 164 | end
|
0 commit comments