Close http exchange at the end of http handler
This commit is contained in:
parent
ab2b3f9481
commit
5a3af95f0b
2 changed files with 48 additions and 18 deletions
|
@ -1114,4 +1114,23 @@ class MockServerIntegrationTest extends Specification {
|
|||
remoteMockServer.removeMock('testRest')?.size() == 0
|
||||
}
|
||||
|
||||
def "should handle empty post"() {
|
||||
expect:
|
||||
remoteMockServer.addMock(new AddMock(
|
||||
name: 'testRest',
|
||||
path: 'testEndpoint',
|
||||
port: 9999,
|
||||
statusCode: 201,
|
||||
soap: false
|
||||
))
|
||||
when:
|
||||
HttpPost restPost = new HttpPost('http://localhost:9999/testEndpoint')
|
||||
CloseableHttpResponse response = client.execute(restPost)
|
||||
then:
|
||||
response.statusLine.statusCode == 201
|
||||
Util.consumeResponse(response)
|
||||
expect:
|
||||
remoteMockServer.removeMock('testRest')?.size() == 1
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -20,28 +20,39 @@ class ContextExecutor {
|
|||
this.mocks = new CopyOnWriteArrayList<>([initialMock])
|
||||
httpServerWraper.createContext(path) {
|
||||
HttpExchange ex ->
|
||||
MockRequest request = new MockRequest(ex.requestBody.text, ex.requestHeaders, ex.requestURI)
|
||||
log.info('Mock received input')
|
||||
log.debug("Request: ${request.text}")
|
||||
for (Mock mock : mocks) {
|
||||
try {
|
||||
if (mock.match(Method.valueOf(ex.requestMethod), request)) {
|
||||
log.debug("Mock ${mock.name} match request ${request.text}")
|
||||
MockResponse httpResponse = mock.apply(request)
|
||||
fillExchange(ex, httpResponse)
|
||||
log.trace("Mock ${mock.name} response with body ${httpResponse.text}")
|
||||
return
|
||||
}
|
||||
log.debug("Mock ${mock.name} does not match request")
|
||||
} catch (Exception e) {
|
||||
log.warn("An exception occured when matching or applying mock ${mock.name}", e)
|
||||
}
|
||||
try {
|
||||
applyMocks(ex)
|
||||
} catch (Exception e) {
|
||||
log.error("Exceptiony occured handling request", e)
|
||||
throw e
|
||||
} finally {
|
||||
ex.close()
|
||||
}
|
||||
log.warn("Any mock does not match request ${request.text}")
|
||||
Util.createResponse(ex, request.text, 404)
|
||||
}
|
||||
}
|
||||
|
||||
private void applyMocks(HttpExchange ex) {
|
||||
MockRequest request = new MockRequest(ex.requestBody.text, ex.requestHeaders, ex.requestURI)
|
||||
log.info('Mock received input')
|
||||
log.debug("Request: ${request.text}")
|
||||
for (Mock mock : mocks) {
|
||||
try {
|
||||
if (mock.match(Method.valueOf(ex.requestMethod), request)) {
|
||||
log.debug("Mock ${mock.name} match request ${request.text}")
|
||||
MockResponse httpResponse = mock.apply(request)
|
||||
fillExchange(ex, httpResponse)
|
||||
log.trace("Mock ${mock.name} response with body ${httpResponse.text}")
|
||||
return
|
||||
}
|
||||
log.debug("Mock ${mock.name} does not match request")
|
||||
} catch (Exception e) {
|
||||
log.warn("An exception occured when matching or applying mock ${mock.name}", e)
|
||||
}
|
||||
}
|
||||
log.warn("Any mock does not match request ${request.text}")
|
||||
Util.createResponse(ex, request.text, 404)
|
||||
}
|
||||
|
||||
String getPath() {
|
||||
return path.substring(1)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue