-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest
executable file
·227 lines (183 loc) · 5.74 KB
/
test
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#!/usr/bin/env ruby
require 'http'
def describe(name, &body)
puts
puts name
begin
body[]
rescue Exception => e
bad e
end
end
def bad(msg)
puts "\033[31m#{msg}\033[0m"
end
def good(msg)
puts "\033[32m#{msg}\033[0m"
end
def test(name, cond)
cond ? good(' ' + name) : bad(' ' + name)
end
def conf
_, body = get '/mock/conf'
puts body.to_json
end
def debug
@debug = true
end
def host
ENV['HOST'] || 'localhost'
end
def port
ENV['PORT'] || '1080'
end
def base_url
"http://#{host}:#{port}"
end
%w[head options delete post put patch get].each do |m|
define_singleton_method m do |path, **opts|
begin
rep = HTTP.send(m, base_url + path, **opts)
puts rep.inspect if @debug
puts rep.body.to_s.inspect if @debug
begin
[rep.code, JSON.parse(rep.body.to_s)]
rescue Exception => e
bad "Exception from json: " + e.to_s if @error
[rep.code, rep.body]
end
rescue Exception => e
bad "Exception from request: " + e.to_s if @error
[nil, nil]
end
end
end
code, _ = get '/mock/conf'
unless code == 200
`thin start -a #{host} -p #{port} -P .testserver -d`
@thin = true
sleep 1
end
@debug = false
@error = true
puts "\n---- Starting Test Suite ----"
describe 'get config' do
put '/mock/reset'
code, body = get '/mock/conf'
test 'responds with 200', code && code == 200
test 'responds with default config', body.dig('default', 'code') == '200'
test 'responds with default config', body.dig('default', 'mode') == 'mock'
end
describe 'dump default mode' do
put '/mock/reset'
put '/mock/conf',
json: { default: { mode: 'dump' } }
code, body = get '/route'
puts body['message']
test 'respondeds with 200', code && code == 200
test 'method captured and returned', body['method'] == 'GET'
test 'path captured and returned', body['path'] == '/route'
end
describe 'mock default mode' do
put '/mock/reset'
put '/mock/conf',
json: { default: { body: { message: 'hello world' }, mode: 'mock' } }
code, body = get '/route'
test 'responds with configured body', body['message'] == 'hello world'
put '/mock/conf',
json: { default: { code: 503, mode: 'mock' } }
code, body = get '/route'
test 'responds with configured status', code && code == 503
test 'retains configured status', body['message'] == 'hello world'
end
describe 'echo default mode' do
put '/mock/reset'
put '/mock/conf', json: { default: { mode: 'echo' } }
code, body = patch '/tenant1', json: { message: 'hello' }
test 'responds with 200', code && code == 200
test 'responds with body', body['message'] == 'hello'
end
describe 'mock requests' do
put '/mock/reset'
put '/mock/conf',
json: { default: { code: 404, mode: 'mock', body: { message: 'hello' } } }
put '/mock/routes',
json: { method: 'POST', body: { message: 'route1' }, code: 503, path: '/route1' }
put '/mock/routes',
json: { method: 'DELETE', body: { message: 'route2' }, code: 201, path: '/route2' }
code, body = put '/route1'
test 'only mocks given method', code && code == 404
code, body = post '/route1'
test 'responds with given code', code && code == 503
test 'responds with given code', body['message'] == 'route1'
code, body = delete '/route3'
test 'only mocks given route', code && code == 404
delete '/mock/routes'
code, body = post '/route1'
test 'responds with default code after deletion', code && code == 404
test 'responds with given code after deletion', body['message'] == 'hello'
put '/mock/routes',
json: { method: 'POST', body: { message: 'route1' }, code: 503, path: '/route1' }
put '/mock/routes',
json: { method: 'DELETE', body: { message: 'route2' }, code: 201, path: '/route2' }
put '/mock/reset'
code, body = delete '/route2'
test 'reset deletes all routes (code)', code && code == 200
test 'reset deletes all routes (body)', body['message'] == 'mock better'
end
describe 'mock request deduplication' do
put '/mock/reset'
put '/mock/conf',
json: { default: { code: 404, mode: 'mock', body: { message: 'hello' } } }
2.times do
put '/mock/routes',
json: { method: 'POST', body: { message: 'route1' }, code: 503, path: '/route1' }
end
_, body = get '/mock/conf'
test 'routes are unique method-path pairs', body['routes'].count == 1
end
describe 'request history' do
put '/mock/reset'
get '/a', json: { message: 1 }
post '/b', json: { message: 2 }
patch '/c', json: { message: 3 }
_, body = get '/mock/history'
test 'records history bodies',
body.map { |r| r['body']['message'] } == [1, 2, 3]
test 'records history methods',
body.map { |r| r['method'] } == %w[GET POST PATCH]
test 'records history paths',
body.map { |r| r['path'] } == %w[/a /b /c]
delete '/mock/history'
_, body = get '/mock/history'
test 'deletes history', body == []
post '/route1', json: { message: 1 }
post '/route2', json: { message: 2 }
post '/route3', json: { message: 3 }
put '/mock/reset'
_, body = get '/mock/history'
test 'reset deletes all history', body == []
end
describe 'reset history ignores routes' do
put '/mock/reset'
put '/mock/routes', json: { method: 'POST', path: 'path' }
delete '/mock/history'
_, body = get '/mock/conf'
test 'reset history removes all requests',
body['history'].count.zero?
test 'reset history leaves routes intact',
body['routes'].count == 1
end
describe 'reset routes ignores history' do
put '/mock/reset'
post '/route1', json: { message: 1 }
delete '/mock/routes'
_, body = get '/mock/conf'
test 'reset routes removes all routes',
body['routes'].count.zero?
test 'reset routes leaves history intact',
body['history'].count == 1
end
puts "\n---- Test Suite Complete ----"
puts
`thin stop -P .testserver` if @thin