Record & Replay
To analyse the requests that a system makes the proxy can be used to record all requests and write them into the mockserver_request.log. This can be particularly helpful when refactoring an existing or legacy system.
The requests can be written to the log in JSON or as Java code. The Java code can then be used to setup expectations in MockServer and exactly replay all interactions.
View Recorded Request As Java
The request and responses recorded by the MockServer Proxy can be written to the request log as Java code using the ProxyClient.dumpToLogAsJava(...) method, as follows:
new ProxyClient("localhost", 1090)
.dumpToLogAsJava(
request()
.withMethod("POST")
.withPath("/login")
);
This would result in entries in the mockserver_request.log file of the following format:
new MockServerClient()
.when(
request()
.withMethod("GET")
.withPath("/get_book")
.withQueryString("id=1")
.withHeaders(
new Header("Accept-Encoding", "gzip"),
new Header("Host", "localhost:1090"),
new Header("User-Agent", "Jetty/8.1.13.v20130916")
),
Times.once()
)
.respond(
response()
.withStatusCode(200)
.withHeaders(
new Header("Server", "Jetty(8.1.13.v20130916)")
)
.withBody("{\n \"id\": \"1\",\n \"title\": \"Xenophon's imperial fiction : on the education of Cyrus\",\n \"author\": \"James Tatum\",\n \"isbn\": \"0691067570\",\n \"publicationDate\": \"1989\"\n }")
);
View Recorded Request As JSON
The request and responses recorded by the MockServer Proxy can be written to the request log as JSON, as follows:
new ProxyClient("localhost", 1090)
.dumpToLogAsJSON(
request()
.withMethod("POST")
.withPath("/login")
);
This would result in entries in the mockserver_request.log file of the following format:
{
"httpRequest": {
"method": "POST",
"path": "/login",
"body": {
"type": "STRING",
"value": "{username: 'foo', password: 'bar'}",
},
"cookies": [
{
"name": "sid",
"values": [ "05F1A3D2F2143C49727" ]
}
],
"headers": [
{
"name": "Accept-Language",
"values": [ "en-US,en;q=0.8,fa;q=0.6" ]
},
{
"name": "Cookie",
"values": [ "sid=05F1A3D2F2143C49727" ]
},
{
"name": "Host",
"values": [ "localhost:2021" ]
},
{
"name": "Content-Length",
"values": [ "34" ]
},
{
"name": "Accept-Encoding",
"values": [ "gzip,deflate,sdch" ]
},
{
"name": "User-Agent",
"values": [ "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36" ]
},
{
"name": "Accept",
"values": [ "*/*" ]
},
{
"name": "Content-Type",
"values": [ "text/plain" ]
}
]
},
"httpResponse": {
"statusCode": 200,
"body": "{ message: 'success' }",
"headers": [
{
"name": "Content-Type",
"values": [ "application/json; charset=utf-8" ]
},
{
"name": "Cache-Control",
"values": [ "no-cache, no-store" ]
}
]
},
"times": {
"remainingTimes": 1,
"unlimited": false
}
}
Replay Recorded Requests
Once the recorded requests have been dumped to the log as Java or JSON these can be copied directly into a test setup method.
The request recordings are in Java or JSON for the following reasons:
- request recordings are clear and fully transparent
- request recordings are easily modifiable
- request recordings are code and so can easily be encapsulated in each test setup method, avoiding the anti-pattern of sharing recordings between tests