Closed smainil closed 2 years ago
The response is not base64 encoded the logs you have provided don't show what response you are receiving.
The following code shows MockServer is working correct as expected:
ClientAndServer mockServer = ClientAndServer.startClientAndServer();
mockServer
.when(
request()
.withPath("/api/system/status")
.withMethod("GET")
)
.respond(
response()
.withBody("{\"status\":\"UP\"}")
.withHeaders(
new Header("content-type", "application/json; charset=utf-8")
)
);
CloseableHttpClient httpClient = HttpClientBuilder.create()
.build();
HttpGet getRequest = new HttpGet("http://localhost:" + mockServer.getPort() + "/api/system/status");
CloseableHttpResponse resp = httpClient.execute(getRequest);
try (InputStreamReader isr = new InputStreamReader(resp.getEntity().getContent()); BufferedReader br = new BufferedReader(isr);) {
String responseBody = br.lines().collect(Collectors.joining("\n"));
System.out.println("responseBody = " + responseBody);
assertThat(responseBody, is("{\"status\":\"UP\"}"));
}
mockServer.stop();
The logs output when that code is executed are as follows:
2022-02-09 12:49:16 5.11.3-SNAPSHOT INFO 56378 started on port: 56378
2022-02-09 12:49:16 5.11.3-SNAPSHOT INFO 56378 creating expectation:
{
"httpRequest" : {
"method" : "GET",
"path" : "/api/system/status"
},
"httpResponse" : {
"headers" : {
"content-type" : [ "application/json; charset=utf-8" ]
},
"body" : "{\"status\":\"UP\"}"
},
"id" : "17089d51-fb7c-4c50-8b9c-7c0256206497",
"priority" : 0,
"timeToLive" : {
"unlimited" : true
},
"times" : {
"unlimited" : true
}
}
with id:
17089d51-fb7c-4c50-8b9c-7c0256206497
2022-02-09 12:49:16 5.11.3-SNAPSHOT INFO 56378 received request:
{
"method" : "GET",
"path" : "/api/system/status",
"headers" : {
"content-length" : [ "0" ],
"User-Agent" : [ "Apache-HttpClient/4.5.13 (Java/11.0.2)" ],
"Host" : [ "localhost:56378" ],
"Connection" : [ "Keep-Alive" ],
"Accept-Encoding" : [ "gzip,deflate" ]
},
"keepAlive" : true,
"secure" : false
}
2022-02-09 12:49:16 5.11.3-SNAPSHOT INFO 56378 request:
{
"method" : "GET",
"path" : "/api/system/status",
"headers" : {
"content-length" : [ "0" ],
"User-Agent" : [ "Apache-HttpClient/4.5.13 (Java/11.0.2)" ],
"Host" : [ "localhost:56378" ],
"Connection" : [ "Keep-Alive" ],
"Accept-Encoding" : [ "gzip,deflate" ]
},
"keepAlive" : true,
"secure" : false
}
matched expectation:
{
"httpRequest" : {
"method" : "GET",
"path" : "/api/system/status"
},
"httpResponse" : {
"headers" : {
"content-type" : [ "application/json; charset=utf-8" ]
},
"body" : "{\"status\":\"UP\"}"
},
"id" : "17089d51-fb7c-4c50-8b9c-7c0256206497",
"priority" : 0,
"timeToLive" : {
"unlimited" : true
},
"times" : {
"unlimited" : true
}
}
2022-02-09 12:49:16 5.11.3-SNAPSHOT INFO 56378 returning response:
{
"headers" : {
"content-type" : [ "application/json; charset=utf-8" ]
},
"body" : "{\"status\":\"UP\"}"
}
for request:
{
"method" : "GET",
"path" : "/api/system/status",
"headers" : {
"content-length" : [ "0" ],
"User-Agent" : [ "Apache-HttpClient/4.5.13 (Java/11.0.2)" ],
"Host" : [ "localhost:56378" ],
"Connection" : [ "Keep-Alive" ],
"Accept-Encoding" : [ "gzip,deflate" ]
},
"keepAlive" : true,
"secure" : false
}
for action:
{
"headers" : {
"content-type" : [ "application/json; charset=utf-8" ]
},
"body" : "{\"status\":\"UP\"}"
}
from expectation:
17089d51-fb7c-4c50-8b9c-7c0256206497
responseBody = {"status":"UP"}
2022-02-09 12:49:16 5.11.3-SNAPSHOT INFO 56378 stopped for port: 56378
Probably stumbled upon this issue or something rather close. In my case it was the received request body which appeared base64 encoded when trying to check it in an ExpectationResponseCallback.
def makeRequest(suffix, body) {
def post = new URL("http://localhost:5000/${suffix}").openConnection()
post.setRequestMethod("POST")
post.setDoOutput(true)
post.setRequestProperty("Content-Type", "multipart/related")
post.getOutputStream().write(body.getBytes("UTF-8"))
post.getInputStream().getText()
}
def testFoo() {
setup:
mockServer.when(HttpRequest.request()
.withPath("/foo")
).respond(
HttpClassCallback.callback(VerifyFoo.name)
)
expect:
'FOO' == makeRequest('foo', 'FOO')
}
static class VerifyFoo implements ExpectationResponseCallback {
@Override
HttpResponse handle(HttpRequest httpRequest) throws Exception {
// THIS FAILS
String body = httpRequest.getBodyAsString()
// THIS WORKS
String body = new String(httpRequest.getBody().getRawBytes())
println 'CHECK /foo: ' + body
return HttpResponse.response()
.withBody(body)
.withStatusCode(200)
}
}
When the request Content-Type is set (and not "text/*") the content is mapped to a BinaryBody: https://github.com/mock-server/mockserver/blob/master/mockserver-core/src/main/java/org/mockserver/model/BinaryBody.java#L46
Describe the issue Response body is always base 64 encoded.
What you are trying to do When adding an expectation in Java with a body containing plain text or json, the body after the expectation creation is always base 64 encoded
MockServer version 5.11.1
To Reproduce Steps to reproduce the issue:
How you are running MockServer (i.e maven plugin, docker, etc) Runningserver using maven plugin (result is same when running the server from command line)
Code you used to create expectations
What error you saw No error but in the log we can see that the value is encoded
Expected behaviour Response body not base 64 encoded but the exact value as the one we defined
MockServer Log