/
ErsatzServer.groovy
609 lines (537 loc) 路 22.7 KB
/
ErsatzServer.groovy
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
/*
* Copyright (C) 2018 Christopher J. Stehno
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.stehno.ersatz
import com.stehno.ersatz.auth.BasicAuthHandler
import com.stehno.ersatz.auth.DigestAuthHandler
import com.stehno.ersatz.auth.SimpleIdentityManager
import com.stehno.ersatz.impl.ErsatzRequest
import com.stehno.ersatz.impl.ExpectationsImpl
import com.stehno.ersatz.impl.ResponseChunker
import com.stehno.ersatz.impl.UndertowClientRequest
import com.stehno.ersatz.impl.UnmatchedRequestReport
import com.stehno.ersatz.impl.WebSocketsHandlerBuilder
import groovy.transform.CompileStatic
import groovy.util.logging.Slf4j
import io.undertow.Undertow
import io.undertow.server.HttpHandler
import io.undertow.server.HttpServerExchange
import io.undertow.server.handlers.BlockingHandler
import io.undertow.server.handlers.CookieImpl
import io.undertow.server.handlers.HttpTraceHandler
import io.undertow.server.handlers.encoding.ContentEncodingRepository
import io.undertow.server.handlers.encoding.DeflateEncodingProvider
import io.undertow.server.handlers.encoding.EncodingHandler
import io.undertow.server.handlers.encoding.GzipEncodingProvider
import org.xnio.Options
import space.jasan.support.groovy.closure.ConsumerWithDelegate
import javax.net.ssl.KeyManagerFactory
import javax.net.ssl.SSLContext
import java.security.KeyStore
import java.util.concurrent.TimeUnit
import java.util.function.BiFunction
import java.util.function.Consumer
import java.util.function.Function
import static com.stehno.ersatz.impl.ResponseChunker.prepareChunks
import static groovy.lang.Closure.DELEGATE_FIRST
import static groovy.transform.TypeCheckingMode.SKIP
import static io.undertow.UndertowOptions.IDLE_TIMEOUT
import static io.undertow.UndertowOptions.NO_REQUEST_TIMEOUT
import static io.undertow.UndertowOptions.REQUEST_PARSE_TIMEOUT
import static io.undertow.util.HttpString.tryFromString
import static java.util.concurrent.TimeUnit.MILLISECONDS
import static java.util.concurrent.TimeUnit.SECONDS
/**
* The main entry point for configuring an Ersatz server, which allows configuring of the expectations and management of the server itself. This is
* the class that should be instantiated in unit tests.
*
* The server will be started on an ephemeral port so as not to collide with itself or other server applications running in the test environment. In
* your tests, you can retrieve the server port or URL using the <code>getPort()</code> and <code>getServerUrl()</code> methods respectively.
*
* Using the <code>ErsatzServer</code> follows the workflow:
*
* <ol>
* <li>Create the <code>ErsatzServer</code> instance.</li>
* <li>Configure the expectations.</li>
* <li>Start the server</li>
* <li>Run your client tests against the server.</li>
* <li>Verify the expectations.</li>
* <li>Stop the server.</li>
* </ol>
*
* See the <a href="http://stehno.com/ersatz/asciidoc/html5/" target="_blank">User Guide</a> for more detailed information.
*/
@CompileStatic
@Slf4j
class ErsatzServer implements ServerConfig, Closeable {
/**
* The response body returned when no matching expectation could be found.
*/
static final String NOT_FOUND_BODY = '404: Not Found'
private static final String LOCALHOST = 'localhost'
private static final int EPHEMERAL_PORT = 0
private static final int UNSPECIFIED_PORT = -1
private static final String NO_HEADERS = '<no-headers>'
private static final String EMPTY = '<empty>'
private final RequestDecoders globalDecoders = new RequestDecoders()
private final ResponseEncoders globalEncoders = new ResponseEncoders()
private final ExpectationsImpl expectations = new ExpectationsImpl(globalDecoders, globalEncoders)
private Undertow server
private boolean httpsEnabled
private boolean autoStartEnabled = true
private boolean started
private boolean mismatchToConsole
private URL keystoreLocation
private String keystorePass = 'ersatz'
private int actualHttpPort = UNSPECIFIED_PORT
private int actualHttpsPort = UNSPECIFIED_PORT
private AuthenticationConfig authenticationConfig
private Closure<Void> timeoutConfig = { b -> }
/**
* Creates a new Ersatz server instance with either the default configuration or a configuration provided by the Groovy DSL closure.
*
* @param closure the configuration closure (delegated to <code>ServerConfig</code>)
*/
@SuppressWarnings('ThisReferenceEscapesConstructor')
ErsatzServer(@DelegatesTo(value = ServerConfig, strategy = DELEGATE_FIRST) final Closure closure = null) {
if (closure) {
ConsumerWithDelegate.create(closure).accept(this)
}
}
/**
* Creates a new Ersatz server instance configured by the provided <code>Consumer</code>, which will have an instance of <code>ServerConfig</code>
* passed into it for server configuration.
*
* @param consumer the configuration consumer
*/
@SuppressWarnings('ThisReferenceEscapesConstructor')
ErsatzServer(final Consumer<ServerConfig> consumer) {
consumer.accept(this)
}
/**
* Used to control the enabled/disabled state of HTTPS on the server. By default HTTPS is disabled.
*
* @param enabled optional toggle value (true if not specified)
* @return a reference to the server being configured
*/
ErsatzServer https(boolean enabled = true) {
httpsEnabled = enabled
this
}
/**
* Used to enable/disable the auto-start feature, which will start the server after any call to either of the <code>expectations</code>
* configuration methods. With this setting enabled, any other calls to the <code>start()</code> method are ignored. Further configuration is
* allowed.
*
* Auto-start is enabled by default.
*
* @param autoStart whether or not auto-start is enabled
* @return a reference to the server being configured
*/
ServerConfig autoStart(boolean autoStart) {
autoStartEnabled = autoStart
this
}
@Deprecated
ServerConfig autoStart() {
autoStart(true)
}
/**
* Used to specify the server request timeout property value on the server. If not specified, <code>SECONDS</code> will be used as the units.
*
* The IDLE_TIMEOUT, NO_REQUEST_TIMEOUT, REQUEST_PARSE_TIMEOUT, READ_TIMEOUT and WRITE_TIMEOUT are all configured to the same specified
* value.
*
* @param value the timeout value
* @param units the units the timeout is specified with (or <code>SECONDS</code>)
* @return a reference to the server being configured
*/
ServerConfig timeout(final int value, final TimeUnit units = SECONDS) {
timeoutConfig = { Undertow.Builder builder ->
Integer ms = MILLISECONDS.convert(value, units) as Integer
builder.setServerOption(IDLE_TIMEOUT, ms)
builder.setServerOption(NO_REQUEST_TIMEOUT, ms)
builder.setServerOption(REQUEST_PARSE_TIMEOUT, ms)
builder.setSocketOption(Options.READ_TIMEOUT, ms)
builder.setSocketOption(Options.WRITE_TIMEOUT, ms)
null
}
this
}
/**
* Used to toggle the console output of mismatched request reports. By default they are only rendered in the logging. A value of <code>true</code>
* will cause the report to be output on the console as well.
*
* @param toConsole whether or not the report should also be written to the console
* @return a reference to the server being configured
*/
@Override
ServerConfig reportToConsole(boolean toConsole = true) {
mismatchToConsole = toConsole
this
}
/**
* Allows configuration of an external HTTPS keystore with the given location and password. By default, if this is not specified an internally
* provided keystore will be used for HTTPS certification. See the User Guide for details about configuring your own keystore.
*
* @param location the URL of the keystore file
* @param password the keystore file password (defaults to "ersatz" if omitted)
* @return a reference to the server being configured
*/
ServerConfig keystore(final URL location, final String password = 'ersatz') {
keystoreLocation = location
keystorePass = password
this
}
/**
* Used to retrieve the port where the HTTP server is running.
*
* @return the HTTP port
*/
int getHttpPort() {
actualHttpPort
}
/**
* Used to retrieve the port where the HTTPS server is running.
*
* @return the HTTPS port
*/
int getHttpsPort() {
actualHttpsPort
}
/**
* Used to retrieve the full URL of the HTTP server.
*
* @return the full URL of the HTTP server
*/
String getHttpUrl() {
"http://localhost:$actualHttpPort"
}
/**
* Used to retrieve the Web Socket URL.
*
* @return the web socket URL
*/
String getWsUrl() {
"ws://localhost:$actualHttpPort"
}
/**
* Used to retrieve the full URL of the HTTPS server.
*
* @return the full URL of the HTTP server
*/
String getHttpsUrl() {
"https://localhost:$actualHttpsPort"
}
/**
* A helper method which may be used to append the given path to the server HTTP url.
*
* @param path the path to be applied
* @return the resulting URL
*/
String httpUrl(final String path) {
"$httpUrl$path"
}
/**
* A helper method which may be used to append the given path to the server HTTPS url.
*
* @param path the path to be applied
* @return the resulting URL
*/
String httpsUrl(final String path) {
"$httpsUrl$path"
}
/**
* Used to configure HTTP expectations on the server; the provided <code>Consumer<Expectations></code> implementation will have an active
* <code>Expectations</code> object passed into it for configuring server interaction expectations.
*
* Calling this method when auto-start is enabled will start the server.
*
* @param expects the <code>Consumer<Expectations></code> instance to perform the configuration
* @return a reference to this server
*/
@SuppressWarnings('ConfusingMethodName')
ErsatzServer expectations(final Consumer<Expectations> expects) {
expects.accept(expectations)
if (autoStartEnabled) {
start()
}
this
}
/**
* Used to configure HTTP expectations on the server; the provided Groovy <code>Closure</code> will delegate to an <code>Expectations</code>
* instance for configuring server interaction expectations using the Groovy DSL.
*
* Calling this method when auto-start is enabled will start the server.
*
* @param closure the Groovy <code>Closure</code> which will provide expectation configuration via DSL
* @return a reference to this server
*/
@SuppressWarnings('ConfusingMethodName')
ErsatzServer expectations(@DelegatesTo(value = Expectations, strategy = DELEGATE_FIRST) final Closure closure) {
expectations(ConsumerWithDelegate.create(closure))
}
/**
* An alternate means of starting the expectation chain.
*
* Calling this method when auto-start is enabled will <b>NOT</b> start the server. Use one of the other expectation configuration method if
* auto-start functionality is desired.
*
* @return the reference to the Expectation configuration object
*/
Expectations expects() {
expectations
}
/**
* Configures the given request content decoder for the specified request content-type.
*
* @param contentType the request content-type
* @param decoder the request content decoder
* @return the reference to the server configuration
*/
@Override
ErsatzServer decoder(String contentType, BiFunction<byte[], DecodingContext, Object> decoder) {
globalDecoders.register contentType, decoder
this
}
/**
* Configures the given request content decoder for the specified request content-type.
*
* @param contentType the request content-type
* @param decoder the request content decoder
* @return the reference to the server configuration
*/
@Override
ErsatzServer decoder(ContentType contentType, BiFunction<byte[], DecodingContext, Object> decoder) {
globalDecoders.register contentType, decoder
this
}
/**
* Registers a response body encoder.
*
* param contentType the response content-type to be encoded
* @param objectType the response object type to be encoded
* @param encoder the encoder function
* @return a reference to this server configuration
*/
@Override
ServerConfig encoder(String contentType, Class objectType, Function<Object, String> encoder) {
globalEncoders.register contentType, objectType, encoder
this
}
/**
* Registers a response body encoder.
*
* param contentType the response content-type to be encoded
* @param objectType the response object type to be encoded
* @param encoder the encoder function
* @return a reference to this server configuration
*/
@Override
ServerConfig encoder(ContentType contentType, Class objectType, Function<Object, String> encoder) {
globalEncoders.register contentType, objectType, encoder
this
}
/**
* Registers authentication configuration as a Groovy Closure.
*
* @param closure the configuration closure
* @return a reference to this server configuration
*/
@Override
ServerConfig authentication(@DelegatesTo(value = AuthenticationConfig, strategy = DELEGATE_FIRST) final Closure closure) {
authentication(ConsumerWithDelegate.create(closure))
}
/**
* Registers authentication configuration as a <code>Consumer<AuthenticationConfig></code>.
*
* @param config the configuration Consumer
* @return a reference to this server configuration
*/
@Override
ServerConfig authentication(final Consumer<AuthenticationConfig> config) {
authenticationConfig = new AuthenticationConfig()
config.accept(authenticationConfig)
return this
}
/**
* Used to start the HTTP server for test interactions. This method should be called after configuration of expectations and before the test
* interactions are executed against the server.
*/
@SuppressWarnings(['Println', 'DuplicateNumberLiteral'])
void start() {
if (!started) {
Undertow.Builder builder = Undertow.builder().addHttpListener(EPHEMERAL_PORT, LOCALHOST)
timeoutConfig.call(builder)
if (httpsEnabled) {
builder.addHttpsListener(EPHEMERAL_PORT, LOCALHOST, sslContext())
}
BlockingHandler blockingHandler = new BlockingHandler(new EncodingHandler(
applyAuthentication(
new HttpTraceHandler(
new HttpHandler() {
@Override
void handleRequest(final HttpServerExchange exchange) throws Exception {
ClientRequest clientRequest = new UndertowClientRequest(exchange)
log.debug 'Request: {}', clientRequest
ErsatzRequest request = expectations.findMatch(clientRequest) as ErsatzRequest
if (request) {
Response currentResponse = request.currentResponse
request.mark(clientRequest)
send(exchange, currentResponse)
} else {
UnmatchedRequestReport report = new UnmatchedRequestReport(
clientRequest,
expectations.requests as List<ErsatzRequest>
)
log.warn report.toString()
if (mismatchToConsole) {
println report
}
exchange.setStatusCode(404).responseSender.send(NOT_FOUND_BODY)
}
}
}
)
),
new ContentEncodingRepository()
.addEncodingHandler('gzip', new GzipEncodingProvider(), 50)
.addEncodingHandler('deflate', new DeflateEncodingProvider(), 50)
))
WebSocketsHandlerBuilder wsBuilder = new WebSocketsHandlerBuilder(expectations, blockingHandler, mismatchToConsole)
server = builder.setHandler(wsBuilder.build()).build()
server.start()
applyPorts()
started = true
}
}
/**
* Clears all configured expectations from the server. Does not affect global encoders or decoders.
*/
void clearExpectations() {
expectations.clear()
}
/**
* Used to stop the HTTP server. The server may be restarted after it has been stopped.
*/
void stop() {
if (started) {
actualHttpPort = UNSPECIFIED_PORT
actualHttpsPort = UNSPECIFIED_PORT
server?.stop()
started = false
}
}
/**
* An alias to the <code>stop()</code> method.
*/
@Override
void close() {
stop()
}
/**
* Used to verify that all of the expected request interactions were called the appropriate number of times. This method should be called after
* all test interactions have been performed. This is an optional step since generally you will also be receiving the expected response back
* from the server; however, this verification step can come in handy when simply needing to know that a request is actually called or not.
*
* If there are web socket expectations configured, this method will be blocking against the expected operations. Expectations involving web
* sockets should consider using the timeout parameters - the default is 1s.
*
* @param timeout the timeout value (defaults to 1)
* @param unit the timeout unit (defaults to SECONDS)
* @return <code>true</code> if all call criteria were met during test execution.
*/
boolean verify(final long timeout = 1, final TimeUnit unit = SECONDS) {
expectations.verify(timeout, unit)
}
private HttpHandler applyAuthentication(final HttpHandler handler) {
HttpHandler result = handler
if (authenticationConfig) {
SimpleIdentityManager identityManager = new SimpleIdentityManager(authenticationConfig.username, authenticationConfig.password)
switch (authenticationConfig.type) {
case Authentication.BASIC:
result = new BasicAuthHandler(identityManager).apply(result)
break
case Authentication.DIGEST:
result = new DigestAuthHandler(identityManager).apply(result)
break
default:
throw new IllegalArgumentException('Invalid authentication configuration.')
}
}
result
}
private static void send(final HttpServerExchange exchange, final Response response) {
if (response) {
if (response.delay) {
sleep response.delay
}
exchange.statusCode = response.code
response.headers.each { String k, List<String> v ->
v.each { String value ->
exchange.responseHeaders.add(tryFromString(k), value)
}
}
if (response.chunkingConfig) {
exchange.responseHeaders.add(tryFromString('Transfer-encoding'), 'chunked')
}
response.cookies.each { k, v ->
if (v instanceof Cookie) {
Cookie ersatzCookie = v as Cookie
exchange.responseCookies.put k, new CookieImpl(k, ersatzCookie.value).with {
path = ersatzCookie.path
domain = ersatzCookie.domain
maxAge = ersatzCookie.maxAge
secure = ersatzCookie.secure
version = ersatzCookie.version
httpOnly = ersatzCookie.httpOnly
setComment(ersatzCookie.comment)
} as CookieImpl
} else {
exchange.responseCookies.put(k, new CookieImpl(k, v as String))
}
}
}
String responseContent = response?.content
String responsePreview = responseContent?.take(1000) ?: EMPTY
ChunkingConfig chunking = response?.chunkingConfig
if (responseContent && chunking) {
log.debug 'Chunked-Response({}; {}): {}', exchange.responseHeaders ?: NO_HEADERS, chunking, responsePreview
List<String> chunks = prepareChunks(responseContent, chunking.chunks)
exchange.responseSender.send(chunks.remove(0), new ResponseChunker(chunks, chunking.delay))
} else {
log.debug 'Response({}): {}', exchange.responseHeaders ?: NO_HEADERS, responsePreview
exchange.responseSender.send(responseContent)
}
}
private void applyPorts() {
actualHttpPort = (server.listenerInfo[0].address as InetSocketAddress).port
if (httpsEnabled) {
actualHttpsPort = (server.listenerInfo[1].address as InetSocketAddress).port
}
}
@CompileStatic(SKIP)
private SSLContext sslContext() {
KeyStore keyStore = KeyStore.getInstance('JKS')
(keystoreLocation ?: ErsatzServer.getResource('/ersatz.keystore')).withInputStream { instr ->
keyStore.load(instr, keystorePass.toCharArray())
}
KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.defaultAlgorithm)
keyManagerFactory.init(keyStore, keystorePass.toCharArray())
SSLContext sslContext = SSLContext.getInstance('TLS')
sslContext.init(keyManagerFactory.keyManagers, null, null)
sslContext
}
}