erithmetic-mountebank 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,72 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe 'Examples' do
4
+ before:each do
5
+ reset_mountebank
6
+ end
7
+
8
+ describe 'get all imposters' do
9
+ it 'should be empty' do
10
+ expect(Mountebank.imposters).to be_empty
11
+ end
12
+ end
13
+
14
+ describe 'create imposter' do
15
+ it 'should create' do
16
+ port = 4545
17
+ protocol = Mountebank::Imposter::PROTOCOL_HTTP
18
+ imposter = Mountebank::Imposter.create(port, protocol, record_requests: true)
19
+
20
+ expect(imposter.reload.requests).to be_empty
21
+ test_url('http://127.0.0.1:4545')
22
+ expect(imposter.reload.requests).to_not be_empty
23
+ end
24
+ end
25
+
26
+ describe 'create imposter with stub' do
27
+ it 'should have stub' do
28
+ port = 4545
29
+ protocol = Mountebank::Imposter::PROTOCOL_HTTP
30
+ imposter = Mountebank::Imposter.build(port, protocol, record_requests: true)
31
+
32
+ # Create a response
33
+ status_code = 200
34
+ headers = {"Content-Type" => "application/json"}
35
+ body = {foo:"bar"}.to_json
36
+ response = Mountebank::Stub::HttpResponse.create(status_code, headers, body)
37
+
38
+ imposter.add_stub(response)
39
+ imposter.save!
40
+
41
+ expect(imposter.reload.requests).to be_empty
42
+ expect(test_url('http://127.0.0.1:4545')).to eq('{"foo":"bar"}')
43
+ expect(imposter.reload.requests).to_not be_empty
44
+ end
45
+ end
46
+
47
+ describe 'create imposter with stub & predicate' do
48
+ it 'should have stub & predicate' do
49
+ port = 4545
50
+ protocol = Mountebank::Imposter::PROTOCOL_HTTP
51
+ imposter = Mountebank::Imposter.build(port, protocol, record_requests: true)
52
+
53
+ # Create a response
54
+ status_code = 200
55
+ headers = {"Content-Type" => "application/json"}
56
+ body = {foo:"bar2"}.to_json
57
+ response = Mountebank::Stub::HttpResponse.create(status_code, headers, body)
58
+
59
+ # Create a predicate
60
+ data = {equals: {path:"/test"}}
61
+ predicate = Mountebank::Stub::Predicate.new(data)
62
+
63
+ imposter.add_stub(response, predicate)
64
+ imposter.save!
65
+
66
+ expect(imposter.reload.requests).to be_empty
67
+ expect(test_url('http://127.0.0.1:4545/test')).to eq('{"foo":"bar2"}')
68
+ expect(test_url('http://127.0.0.1:4545')).to eq('')
69
+ expect(imposter.reload.requests).to_not be_empty
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,20 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe Mountebank::Helper do
4
+ describe '.symbolize' do
5
+ let(:hash) { {
6
+ "foo" => {"bar" => "Baz"},
7
+ "goo" => "balls"
8
+ }
9
+ }
10
+ let(:expected_hash) { {
11
+ :foo => {:bar => "Baz"},
12
+ :goo => "balls"
13
+ }
14
+ }
15
+
16
+ it 'symbolizes' do
17
+ expect(Mountebank::Helper.symbolize(hash)).to eq expected_hash
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,272 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe Mountebank::Imposter do
4
+ before:each do
5
+ reset_mountebank
6
+ end
7
+
8
+ let(:port) { 4545 }
9
+ let(:protocol) { Mountebank::Imposter::PROTOCOL_HTTP }
10
+
11
+ shared_examples 'blank imposter' do
12
+ it 'valid imposter' do
13
+ expect(imposter).to be_a Mountebank::Imposter
14
+ expect(imposter.port).to eq port
15
+ expect(imposter.protocol).to eq protocol
16
+ expect(imposter.name).to eq "imposter_#{port}"
17
+ expect(imposter.stubs).to be_empty
18
+ expect(imposter.requests).to be_empty
19
+ expect(imposter.mode).to be_nil
20
+ end
21
+ end
22
+
23
+ shared_examples 'persists imposter' do
24
+ it 'persist to server' do
25
+ imposter
26
+ expect(Mountebank.imposters).to_not be_empty
27
+ end
28
+ end
29
+
30
+ describe '.build' do
31
+ let(:imposter) { Mountebank::Imposter.build(port, protocol) }
32
+
33
+ it_should_behave_like 'blank imposter'
34
+ end
35
+
36
+ describe '.create' do
37
+ context 'new imposter' do
38
+ let(:imposter) { Mountebank::Imposter.create(port, protocol) }
39
+
40
+ it_should_behave_like 'blank imposter'
41
+ it_should_behave_like 'persists imposter'
42
+ end
43
+
44
+ context 'assumes 2nd argument to be `http`' do
45
+ let(:imposter) { Mountebank::Imposter.create(port) }
46
+
47
+ it_should_behave_like 'blank imposter'
48
+ it_should_behave_like 'persists imposter'
49
+ end
50
+
51
+ context 'other creation options' do
52
+ let(:imposter) { Mountebank::Imposter.create(port, protocol, name:'meow_server') }
53
+
54
+ it 'uses a different name' do
55
+ expect(imposter.name).to eq 'meow_server'
56
+ end
57
+ end
58
+
59
+ context 'invalid arguments' do
60
+ it 'raises invalid port' do
61
+ expect{ Mountebank::Imposter.create('abcd') }.to raise_error 'Invalid port number'
62
+ end
63
+
64
+ it 'raises invalid protocol' do
65
+ expect{ Mountebank::Imposter.create(port, 'seattle') }.to raise_error 'Invalid protocol'
66
+ end
67
+ end
68
+
69
+ context 'creates stub response' do
70
+ let(:responses) { [
71
+ {is: {statusCode: 200, body:"ohai"}}
72
+ ]
73
+ }
74
+ let(:predicates) { [] }
75
+ let(:stubs) { [
76
+ Mountebank::Stub.create(responses, predicates)
77
+ ]
78
+ }
79
+ let!(:imposter) { Mountebank::Imposter.create(port, protocol, stubs: stubs, record_requests: true) }
80
+
81
+ it 'is valid' do
82
+ expect(test_url('http://127.0.0.1:4545')).to eq 'ohai'
83
+ expect(imposter.reload.requests).to_not be_empty
84
+ expect(imposter.stubs.first).to be_a Mountebank::Stub
85
+ end
86
+
87
+ context 'with predicates' do
88
+ let(:response_body) { "Its a real test" }
89
+ let(:responses) { [
90
+ {is: {statusCode: 200, body:response_body}}
91
+ ]
92
+ }
93
+ let(:predicates) { [
94
+ {equals: {path:'/test'}}
95
+ ]
96
+ }
97
+
98
+ it 'is valid' do
99
+ expect(test_url('http://127.0.0.1:4545/test')).to eq response_body
100
+ end
101
+ end
102
+ end
103
+ end
104
+
105
+ describe '.get' do
106
+ before do
107
+ Mountebank::Imposter.create(port)
108
+ end
109
+
110
+ context 'valid imposter' do
111
+ let(:imposter) { Mountebank::Imposter.find(port) }
112
+
113
+ it_should_behave_like 'blank imposter'
114
+ it_should_behave_like 'persists imposter'
115
+ end
116
+
117
+ context 'unknown imposter' do
118
+ it 'returns false' do
119
+ expect(Mountebank::Imposter.find(4546)).to_not be
120
+ end
121
+ end
122
+ end
123
+
124
+ describe '.delete' do
125
+ before do
126
+ Mountebank::Imposter.create(port)
127
+ end
128
+
129
+ context 'has imposter' do
130
+ it 'returns true' do
131
+ expect(Mountebank::Imposter.delete(port)).to be
132
+ end
133
+ end
134
+
135
+ context 'no imposter' do
136
+ it 'returns false' do
137
+ expect(Mountebank::Imposter.delete(4546)).to_not be
138
+ end
139
+ end
140
+ end
141
+
142
+ describe '#reload' do
143
+ before do
144
+ Mountebank::Imposter.create(port, Mountebank::Imposter::PROTOCOL_HTTP, recordRequests: true)
145
+ end
146
+
147
+ let!(:imposter) { Mountebank::Imposter.find(port) }
148
+
149
+ context 'no change' do
150
+ it 'returns imposter' do
151
+ expect(imposter.reload).to be_a Mountebank::Imposter
152
+ end
153
+
154
+ it_should_behave_like 'blank imposter'
155
+ it_should_behave_like 'persists imposter'
156
+ end
157
+
158
+ context 'has requests' do
159
+ it 'returns imposter with requests' do
160
+ test_url('http://127.0.0.1:4545')
161
+ expect(imposter.reload.requests).to_not be_empty
162
+ end
163
+ end
164
+ end
165
+
166
+ describe '#add_stub' do
167
+ let(:imposter) { Mountebank::Imposter.build(port, protocol) }
168
+
169
+ context 'with response' do
170
+ before do
171
+ response = Mountebank::Stub::HttpResponse.create(200, {}, 'ohai you')
172
+ imposter.add_stub(response)
173
+ end
174
+
175
+ it 'adds new stub' do
176
+ expect(imposter.to_json).to eq({
177
+ "port": port,
178
+ "protocol": protocol,
179
+ "name": "imposter_#{port}",
180
+ "stubs": [{
181
+ "responses": [{"is":{"statusCode":200, "body":"ohai you"}}]
182
+ }],
183
+ "recordRequests": false
184
+ }.to_json)
185
+ end
186
+
187
+ it 'is valid imposter' do
188
+ imposter.save!
189
+ expect(test_url('http://127.0.0.1:4545')).to eq('ohai you')
190
+ end
191
+ end
192
+
193
+ context 'with predicate' do
194
+ before do
195
+ response = Mountebank::Stub::HttpResponse.create(200, {}, 'ohai test2')
196
+ data = {equals: {path:'/test2'}}
197
+ predicate = Mountebank::Stub::Predicate.new(data)
198
+ imposter.add_stub(response, predicate)
199
+ end
200
+
201
+ it 'is valid imposter' do
202
+ imposter.save!
203
+ expect(test_url('http://127.0.0.1:4545/test2')).to eq('ohai test2')
204
+ end
205
+ end
206
+
207
+ context 'with array of responses' do
208
+ before do
209
+ response1 = Mountebank::Stub::HttpResponse.create(200, {}, 'hello mother')
210
+ response2 = Mountebank::Stub::HttpResponse.create(200, {}, 'hello father')
211
+ data = {equals: {path:'/test3'}}
212
+ predicate = Mountebank::Stub::Predicate.new(data)
213
+ imposter.add_stub([response1, response2], predicate)
214
+ imposter.save!
215
+ end
216
+
217
+ it 'should save stub in memory with 2 responses' do
218
+ expect(imposter.stubs.first.responses.length).to eq(2)
219
+ end
220
+
221
+ it 'should save stub to server with 2 responses' do
222
+ stub = Mountebank::Imposter.get_imposter_config(port)[:stubs].first
223
+ expect(stub[:responses].length).to eq(2)
224
+ end
225
+
226
+ it 'is a valid imposter with 2 responses' do
227
+ expect(test_url('http://127.0.0.1:4545/test3')).to eq('hello mother')
228
+ expect(test_url('http://127.0.0.1:4545/test3')).to eq('hello father')
229
+ end
230
+ end
231
+
232
+ context 'with array of predicates' do
233
+ before do
234
+ response1 = Mountebank::Stub::HttpResponse.create(200, {}, 'hello mother')
235
+ data1 = {equals: {path:'/test3'}}
236
+ data2 = {equals: {method:'GET'}}
237
+ predicate1 = Mountebank::Stub::Predicate.new(data1)
238
+ predicate2 = Mountebank::Stub::Predicate.new(data2)
239
+ imposter.add_stub(response1, [predicate1, predicate2])
240
+ imposter.save!
241
+ end
242
+
243
+ it 'should save stub in memory with 2 predicates' do
244
+ expect(imposter.stubs.first.predicates.length).to eq(2)
245
+ end
246
+
247
+ it 'should save stub to server with 2 predicates' do
248
+ stub = Mountebank::Imposter.get_imposter_config(port)[:stubs].first
249
+ expect(stub[:predicates].length).to eq(2)
250
+ end
251
+
252
+ it 'is a valid imposter' do
253
+ expect(test_url('http://127.0.0.1:4545/test3')).to eq('hello mother')
254
+ end
255
+
256
+ end
257
+
258
+ end
259
+
260
+ describe '#replayable_data' do
261
+ let(:imposter) { Mountebank::Imposter.build(port, protocol) }
262
+
263
+ it 'returns valid data' do
264
+ expect(imposter.replayable_data).to eq({
265
+ port: port,
266
+ protocol: protocol,
267
+ name: "imposter_#{port}",
268
+ recordRequests: false
269
+ })
270
+ end
271
+ end
272
+ end
@@ -0,0 +1,23 @@
1
+ require 'spec_helper'
2
+ require 'mountebank/network'
3
+
4
+ RSpec.describe Mountebank::Network do
5
+ subject{ Mountebank::Network }
6
+ describe '.mountebank_server_uri' do
7
+ it 'returns correct uri' do
8
+ expect(subject.mountebank_server_uri).to eq 'http://127.0.0.1:2525'
9
+ end
10
+ end
11
+
12
+ describe '.connection' do
13
+ it 'returns a Faraday instance' do
14
+ expect(subject.connection).to be_a(::Faraday::Connection)
15
+ end
16
+ end
17
+
18
+ describe '.get' do
19
+ it 'calls a URL with GET' do
20
+ expect(subject.get('/').status).to eq 200
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,21 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe Mountebank::Stub::HttpResponse do
4
+ let(:statusCode) { 200 }
5
+ let(:headers) { {"Content-Type" => "application/json"} }
6
+ let(:body) { {foo:"bar"}.to_json }
7
+ let!(:response) { Mountebank::Stub::HttpResponse.create(statusCode, headers, body) }
8
+ let!(:response_with_behaviors) { Mountebank::Stub::HttpResponse.create(statusCode, headers, body, {wait: 10000}) }
9
+
10
+ describe '.create' do
11
+ it 'returns response object' do
12
+ expect(response).to be_a Mountebank::Stub::HttpResponse
13
+ expect(response.to_json).to eq '{"is":{"statusCode":200,"headers":{"Content-Type":"application/json"},"body":"{\"foo\":\"bar\"}"}}'
14
+ end
15
+
16
+ it 'returns a response object' do
17
+ expect(response_with_behaviors).to be_a Mountebank::Stub::HttpResponse
18
+ expect(response_with_behaviors.to_json).to eq '{"is":{"statusCode":200,"headers":{"Content-Type":"application/json"},"body":"{\"foo\":\"bar\"}"},"_behaviors":{"wait":10000}}'
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,15 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe Mountebank::Stub::HttpsResponse do
4
+ let(:statusCode) { 200 }
5
+ let(:headers) { {"Content-Type" => "application/json"} }
6
+ let(:body) { {foo:"bar"}.to_json }
7
+ let!(:response) { Mountebank::Stub::HttpsResponse.create(statusCode, headers, body) }
8
+
9
+ describe '.create' do
10
+ it 'returns response object' do
11
+ expect(response).to be_a Mountebank::Stub::HttpsResponse
12
+ expect(response.to_json).to eq '{"is":{"statusCode":200,"headers":{"Content-Type":"application/json"},"body":"{\"foo\":\"bar\"}"}}'
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,24 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe Mountebank::Stub::Predicate do
4
+ let(:data) { {} }
5
+ let(:predicate) { Mountebank::Stub::Predicate.new(data) }
6
+
7
+ describe '#initialize' do
8
+ it 'creates a new object' do
9
+ expect(predicate.equals).to be_nil
10
+ expect(predicate.caseSensitive).to be_nil
11
+ expect(predicate.except).to be_nil
12
+ expect(predicate.to_json).to eq '{}'
13
+ end
14
+
15
+ context 'sets value' do
16
+ let(:data) { {equals: {path:'/test'}} }
17
+
18
+ it 'has path' do
19
+ expect(predicate.equals).to eq({path:'/test'})
20
+ expect(predicate.to_json).to eq '{"equals":{"path":"/test"}}'
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,23 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe Mountebank::Stub::ProxyResponse do
4
+ let(:to) { 'http://example.com' }
5
+ let(:mode) { Mountebank::Stub::ProxyResponse::PROXY_MODE_ONCE }
6
+ let(:predicateGenerators) { [
7
+ { matches: {
8
+ method:true,
9
+ path:true,
10
+ query:true
11
+ }
12
+ }
13
+ ]
14
+ }
15
+ let!(:response) { Mountebank::Stub::ProxyResponse.create(to, mode, predicateGenerators) }
16
+
17
+ describe '.create' do
18
+ it 'returns response object' do
19
+ expect(response).to be_a Mountebank::Stub::ProxyResponse
20
+ expect(response.to_json).to eq '{"proxy":{"to":"http://example.com","mode":"proxyOnce","predicateGenerators":[{"matches":{"method":true,"path":true,"query":true}}]}}'
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,34 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe Mountebank::Stub::Response do
4
+ let(:data) { {} }
5
+ let(:response) { Mountebank::Stub::Response.new(data) }
6
+
7
+ describe '#initialize' do
8
+ it 'creates a new object' do
9
+ expect(response.is).to be_nil
10
+ expect(response.proxy).to be_nil
11
+ expect(response.inject).to be_nil
12
+ expect(response.to_json).to eq '{}'
13
+ end
14
+ end
15
+
16
+ context 'add dummy response' do
17
+ let(:data) { {
18
+ :is => {statusCode: 200, body:"ohai"}
19
+ }
20
+ }
21
+ it 'is able to response' do
22
+ expect(response.to_json).to eq '{"is":{"statusCode":200,"body":"ohai"}}'
23
+ end
24
+ end
25
+
26
+ context '.with_injection' do
27
+ let(:data) { 'function (request, state, logger){}' }
28
+ let(:response) { Mountebank::Stub::Response.with_injection(data) }
29
+
30
+ it 'is valid' do
31
+ expect(response.inject).to eq(data)
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,13 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe Mountebank::Stub::TcpResponse do
4
+ let(:data) { 'blah' }
5
+ let!(:response) { Mountebank::Stub::TcpResponse.create(data) }
6
+
7
+ describe '.create' do
8
+ it 'returns response object' do
9
+ expect(response).to be_a Mountebank::Stub::TcpResponse
10
+ expect(response.to_json).to eq '{"is":{"data":"blah"}}'
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,61 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe Mountebank::Stub do
4
+ let(:responses) { [] }
5
+ let(:predicates) { [] }
6
+ let(:stub) { Mountebank::Stub.create(responses, predicates) }
7
+
8
+ describe '#initialize' do
9
+ it 'creates a new object' do
10
+ expect(stub).to be_a Mountebank::Stub
11
+ expect(stub.responses).to eq []
12
+ expect(stub.predicates).to eq []
13
+ expect(stub.to_json).to eq '{}'
14
+ end
15
+ end
16
+
17
+ context 'has responses' do
18
+ let(:responses) { [
19
+ {
20
+ is: {statusCode: 200, body:"ohai"}
21
+ }
22
+ ]
23
+ }
24
+
25
+ it 'is not empty' do
26
+ expect(stub.responses).to_not be_empty
27
+ end
28
+
29
+ it 'is a response' do
30
+ expect(stub.responses.first).to be_a Mountebank::Stub::Response
31
+ expect(stub.responses.first.is[:statusCode]).to eq 200
32
+ end
33
+
34
+ it 'renders correct JSON' do
35
+ expect(stub.to_json).to eq '{"responses":[{"is":{"statusCode":200,"body":"ohai"}}]}'
36
+ end
37
+ end
38
+
39
+ context 'has predicates' do
40
+ let(:predicates) { [
41
+ {
42
+ equals: {path:'/test'}
43
+ }
44
+ ]
45
+ }
46
+
47
+ it 'is not empty' do
48
+ expect(stub.predicates).to_not be_empty
49
+ end
50
+
51
+ it 'is a predicate' do
52
+ expect(stub.predicates.first).to be_a Mountebank::Stub::Predicate
53
+ expect(stub.predicates.first.equals[:path]).to eq('/test')
54
+ end
55
+
56
+ it 'renders correct JSON' do
57
+ expect(stub.to_json).to eq '{"predicates":[{"equals":{"path":"/test"}}]}'
58
+ end
59
+ end
60
+ end
61
+
@@ -0,0 +1,35 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe Mountebank do
4
+ before:each do
5
+ reset_mountebank
6
+ end
7
+
8
+ describe '.reset' do
9
+ it 'returns' do
10
+ expect(Mountebank.reset).to be
11
+ end
12
+ end
13
+
14
+ describe '.imposters' do
15
+ context 'no imposters' do
16
+ it 'blank' do
17
+ expect(Mountebank.imposters).to be_empty
18
+ end
19
+ end
20
+
21
+ context 'has imposters' do
22
+ before do
23
+ Mountebank::Imposter.create(4545)
24
+ end
25
+
26
+ it 'not empty' do
27
+ expect(Mountebank.imposters).to_not be_empty
28
+ end
29
+
30
+ it 'returns valid imposter' do
31
+ expect(Mountebank.imposters.first).to be_a Mountebank::Imposter
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,17 @@
1
+ require 'mountebank'
2
+ require 'dotenv'
3
+ require 'pry'
4
+ require 'open-uri'
5
+ Dotenv.load
6
+
7
+ def reset_mountebank
8
+ Mountebank.reset
9
+ end
10
+
11
+ def test_url(uri)
12
+ open(uri).read
13
+ end
14
+
15
+ RSpec.configure do |config|
16
+ end
17
+