For IntelliJ IDEA,
Android Studio or Eclipse



private void setHeaderIfNotAlreadyExists(HttpResponse response, String name, String value) { if (response.getFirstHeader(name).isEmpty()) { response.withHeader(name, value); } }
public void responseLiteralWithUTF16BodyResponse() { new MockServerClient("localhost", 1080) // this request matcher matches every request .when( request() ) .respond( response() .withHeader( CONTENT_TYPE.toString(), MediaType.create("text", "plain").withCharset(Charsets.UTF_16).toString() ) .withBody("我说中国话".getBytes(Charsets.UTF_16)) ); }
@Override public void writeResponse(HttpRequest request, HttpResponse response, boolean apiResponse) { if (response == null) { response = notFoundResponse(); } if (enableCORSForAllResponses()) { addCORSHeaders.addCORSHeaders(request, response); } else if (apiResponse && enableCORSForAPI()) { addCORSHeaders.addCORSHeaders(request, response); } if (apiResponse) { response.withHeader("version", org.mockserver.Version.getVersion()); } addConnectionHeader(request, response); writeAndCloseSocket(ctx, request, response); }
@Override public void writeResponse(HttpRequest request, HttpResponse response, boolean apiResponse) { if (response == null) { response = notFoundResponse(); } if (enableCORSForAllResponses()) { addCORSHeaders.addCORSHeaders(request, response); } else if (apiResponse && enableCORSForAPI()) { addCORSHeaders.addCORSHeaders(request, response); } if (apiResponse) { response.withHeader("version", org.mockserver.Version.getVersion()); } addConnectionHeader(request, response); mockServerResponseToHttpServletResponseEncoder.mapMockServerResponseToHttpServletResponse(response, httpServletResponse); }
.withHeader(CONTENT_TYPE.toString(), MediaType.create("text", "plain").withCharset(Charsets.UTF_16).toString()) .withStatusCode(OK_200.code()) .withReasonPhrase(OK_200.reasonPhrase()) .withHeader(CONTENT_TYPE.toString(), MediaType.create("text", "plain").withCharset(Charsets.UTF_16).toString()) .withStatusCode(OK_200.code()) .withReasonPhrase(OK_200.reasonPhrase())
.withHeader(CONTENT_TYPE.toString(), MediaType.create("text", "plain").withCharset(Charsets.UTF_8).toString()) .withStatusCode(OK_200.code()) .withReasonPhrase(OK_200.reasonPhrase()) .withHeader(CONTENT_TYPE.toString(), MediaType.create("text", "plain").withCharset(Charsets.UTF_8).toString()) .withStatusCode(OK_200.code()) .withReasonPhrase(OK_200.reasonPhrase())
public void addCORSHeaders(HttpResponse response) { String methods = "CONNECT, DELETE, GET, HEAD, OPTIONS, POST, PUT, TRACE"; String headers = "Allow, Content-Encoding, Content-Length, Content-Type, ETag, Expires, Last-Modified, Location, Server, Vary"; if (response.getFirstHeader("Access-Control-Allow-Origin").isEmpty()) { response.withHeader("Access-Control-Allow-Origin", "*"); } if (response.getFirstHeader("Access-Control-Allow-Methods").isEmpty()) { response.withHeader("Access-Control-Allow-Methods", methods); } if (response.getFirstHeader("Access-Control-Allow-Headers").isEmpty()) { response.withHeader("Access-Control-Allow-Headers", headers); } if (response.getFirstHeader("Access-Control-Expose-Headers").isEmpty()) { response.withHeader("Access-Control-Expose-Headers", headers); } if (response.getFirstHeader("Access-Control-Max-Age").isEmpty()) { response.withHeader("Access-Control-Max-Age", "300"); } if (response.getFirstHeader("X-CORS").isEmpty()) { response.withHeader("X-CORS", "MockServer CORS support enabled by default, to disable ConfigurationProperties.enableCORSForAPI(false) or -Dmockserver.disableCORS=false"); } } }
.withStatusCode(OK_200.code()) .withReasonPhrase(OK_200.reasonPhrase()) .withHeader("name", "value") .withBody("some_request_body"), makeRequest( .withStatusCode(OK_200.code()) .withReasonPhrase(OK_200.reasonPhrase()) .withHeader("name", "value") .withBody("some_request_body"), makeRequest(
assertEquals( response("some_body_one") .withHeader("some", "header") .withHeader("cookie", "some=parameter") .withHeader("set-cookie", "some=parameter") .withCookie("some", "parameter"), makeRequest(
.withHeader(CONTENT_TYPE.toString(), MediaType.create("text", "plain").withCharset(Charsets.UTF_8).toString()) .withBody(body) ); .withHeader(CONTENT_TYPE.toString(), MediaType.create("text", "plain").withCharset(Charsets.UTF_8).toString()) .withStatusCode(OK_200.code()) .withReasonPhrase(OK_200.reasonPhrase()) .withHeader(CONTENT_TYPE.toString(), MediaType.create("text", "plain").withCharset(Charsets.UTF_8).toString()) .withStatusCode(OK_200.code()) .withReasonPhrase(OK_200.reasonPhrase())
.withReasonPhrase(OK_200.reasonPhrase()) .withCookie("name", "value") .withHeader("set-cookie", "name=value") .withBody("{\"method\":\"GET\",\"path\":\"/some_path\",\"body\":\"some_request_body\"}"), makeRequest( .withReasonPhrase(OK_200.reasonPhrase()) .withCookie("name", "value") .withHeader("set-cookie", "name=value") .withBody("{\"method\":\"GET\",\"path\":\"/some_path\",\"body\":\"some_request_body\"}"), makeRequest(
.withHeader(CONTENT_TYPE.toString(), MediaType.create("text", "plain").withCharset(Charsets.UTF_8).toString()) .withStatusCode(OK_200.code()) .withReasonPhrase(OK_200.reasonPhrase()) .withHeader(CONTENT_TYPE.toString(), MediaType.create("text", "plain").withCharset(Charsets.UTF_8).toString()) .withStatusCode(OK_200.code()) .withReasonPhrase(OK_200.reasonPhrase())
public void addCORSHeaders(HttpResponse response) { String methods = "CONNECT, DELETE, GET, HEAD, OPTIONS, POST, PUT, TRACE"; String headers = "Allow, Content-Encoding, Content-Length, Content-Type, ETag, Expires, Last-Modified, Location, Server, Vary"; if (response.getFirstHeader("Access-Control-Allow-Origin").isEmpty()) { response.withHeader("Access-Control-Allow-Origin", "*"); } if (response.getFirstHeader("Access-Control-Allow-Methods").isEmpty()) { response.withHeader("Access-Control-Allow-Methods", methods); } if (response.getFirstHeader("Access-Control-Allow-Headers").isEmpty()) { response.withHeader("Access-Control-Allow-Headers", headers); } if (response.getFirstHeader("Access-Control-Expose-Headers").isEmpty()) { response.withHeader("Access-Control-Expose-Headers", headers); } if (response.getFirstHeader("Access-Control-Max-Age").isEmpty()) { response.withHeader("Access-Control-Max-Age", "300"); } if (response.getFirstHeader("X-CORS").isEmpty()) { response.withHeader("X-CORS", "MockServer CORS support enabled by default, to disable ConfigurationProperties.enableCORSForAPI(false) or -Dmockserver.disableCORS=false"); } } }