mock-server / mockserver

MockServer enables easy mocking of any system you integrate with via HTTP or HTTPS with clients written in Java, JavaScript and Ruby. MockServer also includes a proxy that introspects all proxied traffic including encrypted SSL traffic and supports Port Forwarding, Web Proxying (i.e. HTTP proxy), HTTPS Tunneling Proxying (using HTTP CONNECT) and SOCKS Proxying (i.e. dynamic port forwarding).
http://mock-server.com
Apache License 2.0
4.54k stars 1.06k forks source link

Question: Configure mockserver to return file #554

Closed mangatmodi closed 5 years ago

mangatmodi commented 5 years ago

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:-

val body =   """
                {
                    "httpRequest" : {
                        "path" : "/users",
                        }
                    },
                    "httpResponse" : {
                        "statusCode": 200,
                        "body": //ADD FILE HERE
                    },
                    "times" : {
                        "unlimited" : true
                    },
                    "timeToLive" : {
                        "unlimited" : true
                    }
                }
            Unirest.put("http://localhost:1080/expectation")
                .body(body)
                .asBinary()
jamesdbloom commented 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

remus-sinorchian-idnow commented 1 year ago

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)));
}