Closed mangatmodi closed 5 years ago
You could use the following code:
byte[] pdfBytes = IOUtils.toByteArray(getClass().getClassLoader().getResourceAsStream("test.pdf"));
mockServerClient
.when(
request().withBody(binary(pdfBytes))
)
.respond(
response()
.withStatusCode(OK_200.code())
.withReasonPhrase(OK_200.reasonPhrase())
.withHeaders(
header(CONTENT_TYPE.toString(), MediaType.PDF.toString()),
header(CONTENT_DISPOSITION.toString(), "form-data; name=\"test.pdf\"; filename=\"test.pdf\""),
header(CACHE_CONTROL.toString(), "must-revalidate, post-check=0, pre-check=0")
)
.withBody(binary(pdfBytes))
);
This code is taken from one of the integration tests: https://github.com/jamesdbloom/mockserver/blob/e2b0be7257cf88e5d624fbafcd2a3a844df2ffd3/mockserver-integration-testing/src/main/java/org/mockserver/integration/server/AbstractExtendedMockingIntegrationTest.java#L1180
I managed to call the rest API to configure an expectation that returns a binary response like this
curl -X 'PUT'
'http://localhost:1080/mockserver/expectation'
-H 'accept: application/json'
-H 'Content-Type: application/json'
-d '{ "id": "getZip", "priority": 0, "httpRequest": { "method": "GET", "path": "/api/v1/*" }, "httpResponse": { "headers": { "content-type": ["application/zip"], "content-disposition": ["form-data; name="result.zip"; filename="result.zip""] }, "body": { "type": "BINARY", "base64Bytes":"XXXXXXXXX....." } } }'
I created the base64bytes with this java code, with the help of jamesdbloom
public static void main(String[] args) throws IOException {
MockServerTest cls = new MockServerTest();
byte[] bytes = org.apache.commons.io.IOUtils.toByteArray(cls.getClass().getClassLoader().getResourceAsStream("response.zip"));
System.out.println(new String(Base64.getEncoder().encode(bytes)));
}
I need to mock a http file server, where hitting a url will return a binary file placed in the class path.
I am unable to find any example in the documentation.
Until now I have been setting expectations as:-