For IntelliJ IDEA and
Android Studio


private void setHeaders(HttpRequest httpRequest, FullHttpRequest fullHttpResponse) { Headers headers = new Headers(); HttpHeaders httpHeaders = fullHttpResponse.headers(); for (String headerName : httpHeaders.names()) { headers.withEntry(new Header(headerName, httpHeaders.getAll(headerName))); } httpRequest.withHeaders(headers); }
public void retrieveRecordedLogMessages() { String[] logMessages = new ProxyClient("localhost", 1080) .retrieveLogMessagesArray( request() .withPath("/some/path") .withMethod("POST") ); }
private String getCookieHeader(HttpRequest request) { List<Cookie> cookies = new ArrayList<Cookie>(); for (org.mockserver.model.Cookie cookie : request.getCookieList()) { cookies.add(new DefaultCookie(cookie.getName().getValue(), cookie.getValue().getValue())); } if (cookies.size() > 0) { return " -H '" + COOKIE + ": " + ClientCookieEncoder.LAX.encode(cookies) + "'"; } else { return ""; } } }
private String getHostAndPort(HttpRequest request, InetSocketAddress remoteAddress) { String host = request.getFirstHeader("Host"); if (Strings.isNullOrEmpty(host)) { host = remoteAddress.getHostName() + ":" + remoteAddress.getPort(); } return host; }
private void setQueryString(HttpRequest httpRequest, HttpServletRequest httpServletRequest) { Parameters parameters = new Parameters(); if (StringUtils.isNotEmpty(httpServletRequest.getQueryString())) { parameters.withEntries(new QueryStringDecoder("?" + httpServletRequest.getQueryString()).parameters()); } httpRequest.withQueryStringParameters(parameters); }
public void retrieveAllRecordedRequests() { HttpRequest[] recordedRequests = new MockServerClient("localhost", 1080) .retrieveRecordedRequests( request() ); }
/** * Clear expectations, logs or both that match the http * * @param httpRequest the http request that is matched against when deciding whether to clear each expectation if null all expectations are cleared * @param type the type to clear, EXPECTATION, LOG or BOTH */ public T clear(HttpRequest httpRequest, ClearType type) { sendRequest(request().withMethod("PUT").withPath(calculatePath("clear")).withQueryStringParameter("type", type.name().toLowerCase()).withBody(httpRequest != null ? httpRequestSerializer.serialize(httpRequest) : "", Charsets.UTF_8)); return clientClass.cast(this); }
void sendExpectation(Expectation expectation) { HttpResponse httpResponse = sendRequest(request().withMethod("PUT").withPath(calculatePath("expectation")).withBody(expectation != null ? expectationSerializer.serialize(expectation) : "", Charsets.UTF_8)); if (httpResponse != null && httpResponse.getStatusCode() != 201) { throw new ClientException(formatErrorMessage(NEW_LINE + "error:%s" + NEW_LINE + "while submitted expectation:%s", httpResponse.getBody(), expectation)); } }
public InetSocketAddress socketAddressFromHostHeader() { if (!Strings.isNullOrEmpty(getFirstHeader(HOST.toString()))) { boolean isSsl = isSecure() != null && isSecure(); String[] hostHeaderParts = getFirstHeader(HOST.toString()).split(":"); return new InetSocketAddress(hostHeaderParts[0], hostHeaderParts.length > 1 ? Integer.parseInt(hostHeaderParts[1]) : isSsl ? 443 : 80); } else { throw new IllegalArgumentException("Host header must be provided to determine remote socket address, the request does not include the \"Host\" header:" + NEW_LINE + this); } }
public void reset() { mockServerMatcher.reset(); mockServerLog.reset(); mockServerLogger.info(request(), "resetting all expectations and request logs" + NEW_LINE); }
public void retrieveRecordedLogMessages() { String[] logMessages = new MockServerClient("localhost", 1080) .retrieveLogMessagesArray( request() .withPath("/some/path") .withMethod("POST") ); }
public void clear() { new ProxyClient("localhost", 1080).clear( request() .withPath("/some/path") .withMethod("POST") ); }