private boolean bodyMatches(HttpRequest context, HttpRequest request) { boolean bodyMatches = true; String bodyAsString = request.getBody() != null ? new String(request.getBody().getRawBytes(), request.getBody().getCharset(StandardCharsets.UTF_8)) : ""; if (!bodyAsString.isEmpty()) { if (bodyMatcher instanceof BinaryMatcher) { bodyMatches = matches(context, bodyMatcher, request.getBodyAsRawBytes()); } else { if (bodyMatcher instanceof ExactStringMatcher || bodyMatcher instanceof SubStringMatcher || bodyMatcher instanceof RegexStringMatcher || bodyMatcher instanceof XmlStringMatcher) { bodyMatches = matches(context, bodyMatcher, string(bodyAsString)); } else { bodyMatches = matches(context, bodyMatcher, bodyAsString); } } if (!bodyMatches) { try { bodyMatches = bodyDTOMatcher.equals(objectMapper.readValue(bodyAsString, BodyDTO.class)); } catch (Throwable e) { // ignore this exception as this exception would typically get thrown for "normal" HTTP requests (i.e. not clear or retrieve) } } } return bodyMatches; }
private ByteBuf getBody(HttpRequest httpRequest) { ByteBuf content = Unpooled.buffer(0, 0); Body body = httpRequest.getBody(); if (body != null) { Object bodyContents = body.getValue(); Charset bodyCharset = body.getCharset(ContentTypeMapper.getCharsetFromContentTypeHeader(httpRequest.getFirstHeader(CONTENT_TYPE.toString()))); if (bodyContents instanceof byte[]) { content = Unpooled.copiedBuffer((byte[]) bodyContents); } else if (bodyContents instanceof String) { content = Unpooled.copiedBuffer(((String) bodyContents).getBytes(bodyCharset)); } else if (body.toString() != null) { content = Unpooled.copiedBuffer(body.toString().getBytes(bodyCharset)); } } return content; }
if (request.getBody() != null) { if (request.getBody() instanceof JsonBody) { appendNewLineAndIndent((numberOfSpacesToIndent + 1) * INDENT_SIZE, output); output.append(".withBody("); JsonBody jsonBody = (JsonBody) request.getBody(); output.append("new JsonBody(\"").append(StringEscapeUtils.escapeJava(jsonBody.getValue())).append("\", JsonBodyMatchType.").append(jsonBody.getMatchType()).append(")"); output.append(")"); } else if (request.getBody() instanceof JsonPathBody) { appendNewLineAndIndent((numberOfSpacesToIndent + 1) * INDENT_SIZE, output); output.append(".withBody("); JsonPathBody jsonPathBody = (JsonPathBody) request.getBody(); output.append("new JsonPathBody(\"").append(StringEscapeUtils.escapeJava(jsonPathBody.getValue())).append("\")"); output.append(")"); } else if (request.getBody() instanceof JsonSchemaBody) { appendNewLineAndIndent((numberOfSpacesToIndent + 1) * INDENT_SIZE, output); output.append(".withBody("); JsonSchemaBody jsonSchemaBody = (JsonSchemaBody) request.getBody(); output.append("new JsonSchemaBody(\"").append(StringEscapeUtils.escapeJava(jsonSchemaBody.getValue())).append("\")"); output.append(")"); } else if (request.getBody() instanceof XmlBody) { appendNewLineAndIndent((numberOfSpacesToIndent + 1) * INDENT_SIZE, output); output.append(".withBody("); XmlBody xmlBody = (XmlBody) request.getBody(); output.append("new XmlBody(\"").append(StringEscapeUtils.escapeJava(xmlBody.getValue())).append("\")"); output.append(")"); } else if (request.getBody() instanceof XPathBody) { appendNewLineAndIndent((numberOfSpacesToIndent + 1) * INDENT_SIZE, output); output.append(".withBody("); XPathBody xPathBody = (XPathBody) request.getBody();
private boolean bodyMatches(HttpRequest context, HttpRequest request) { boolean bodyMatches = true; String bodyAsString = request.getBody() != null ? new String(request.getBody().getRawBytes(), request.getBody().getCharset(StandardCharsets.UTF_8)) : ""; if (!bodyAsString.isEmpty()) { if (bodyMatcher instanceof BinaryMatcher) { bodyMatches = matches(context, bodyMatcher, request.getBodyAsRawBytes()); } else { if (bodyMatcher instanceof ExactStringMatcher || bodyMatcher instanceof SubStringMatcher || bodyMatcher instanceof RegexStringMatcher || bodyMatcher instanceof XmlStringMatcher) { bodyMatches = matches(context, bodyMatcher, string(bodyAsString)); } else { bodyMatches = matches(context, bodyMatcher, bodyAsString); } } if (!bodyMatches) { try { bodyMatches = bodyDTOMatcher.equals(objectMapper.readValue(bodyAsString, BodyDTO.class)); } catch (Throwable e) { // ignore this exception as this exception would typically get thrown for "normal" HTTP requests (i.e. not clear or retrieve) } } } return bodyMatches; }
public HttpRequestDTO(HttpRequest httpRequest, Boolean not) { super(not); if (httpRequest != null) { method = httpRequest.getMethod(); path = httpRequest.getPath(); headers = httpRequest.getHeaders(); cookies = httpRequest.getCookies(); queryStringParameters = httpRequest.getQueryStringParameters(); body = BodyDTO.createDTO(httpRequest.getBody()); keepAlive = httpRequest.isKeepAlive(); secure = httpRequest.isSecure(); } }
public HttpRequestTemplateObject(HttpRequest httpRequest) { if (httpRequest != null) { method = httpRequest.getMethod().getValue(); path = httpRequest.getPath().getValue(); for (Header header : httpRequest.getHeaderList()) { headers.put(header.getName().getValue(), Lists.transform(header.getValues(), new Function<NottableString, String>() { public String apply(NottableString input) { return input.getValue(); } })); } for (Cookie cookie : httpRequest.getCookieList()) { cookies.put(cookie.getName().getValue(), cookie.getValue().getValue()); } for (Parameter parameter : httpRequest.getQueryStringParameterList()) { queryStringParameters.put(parameter.getName().getValue(), Lists.transform(parameter.getValues(), new Function<NottableString, String>() { public String apply(NottableString input) { return input.getValue(); } })); } body = BodyDTO.createDTO(httpRequest.getBody()); keepAlive = httpRequest.isKeepAlive(); secure = httpRequest.isSecure(); } }
public HttpRequest update(HttpRequest replaceRequest) { if (!Strings.isNullOrEmpty(replaceRequest.getMethod().getValue())) { withMethod(replaceRequest.getMethod()); } if (!Strings.isNullOrEmpty(replaceRequest.getPath().getValue())) { withPath(replaceRequest.getPath()); } for (Header header : replaceRequest.getHeaderList()) { getHeaders().replaceEntry(header); } for (Cookie cookie : replaceRequest.getCookieList()) { withCookie(cookie); } for (Parameter parameter : replaceRequest.getQueryStringParameterList()) { getQueryStringParameters().replaceEntry(parameter); } if (replaceRequest.getBody() != null) { withBody(replaceRequest.getBody()); } if (replaceRequest.isSecure() != null) { withSecure(replaceRequest.isSecure()); } if (replaceRequest.isKeepAlive() != null) { withKeepAlive(replaceRequest.isKeepAlive()); } return this; } }
public HttpRequestMatcher(HttpRequest httpRequest, MockServerLogger mockServerLogger) { this.httpRequest = httpRequest; this.mockServerLogger = mockServerLogger; if (httpRequest != null) { withMethod(httpRequest.getMethod()); withPath(httpRequest.getPath()); withQueryStringParameters(httpRequest.getQueryStringParameters()); withBody(httpRequest.getBody()); withHeaders(httpRequest.getHeaders()); withCookies(httpRequest.getCookies()); withKeepAlive(httpRequest.isKeepAlive()); withSsl(httpRequest.isSecure()); } }
public HttpRequestMatcher(Expectation expectation, MockServerLogger mockServerLogger) { this.expectation = expectation; this.httpRequest = expectation.getHttpRequest(); this.mockServerLogger = mockServerLogger; if (httpRequest != null) { withMethod(httpRequest.getMethod()); withPath(httpRequest.getPath()); withQueryStringParameters(httpRequest.getQueryStringParameters()); withBody(httpRequest.getBody()); withHeaders(httpRequest.getHeaders()); withCookies(httpRequest.getCookies()); withKeepAlive(httpRequest.isKeepAlive()); withSsl(httpRequest.isSecure()); } }
headersToIgnore) ); assertEquals(StaticTestExpectationResponseCallback.httpRequests.get(0).getBody().getValue(), "an_example_body_http"); assertEquals(StaticTestExpectationResponseCallback.httpRequests.get(0).getPath().getValue(), calculatePath("callback")); assertEquals(StaticTestExpectationResponseCallback.httpRequests.get(1).getBody().getValue(), "an_example_body_https"); assertEquals(StaticTestExpectationResponseCallback.httpRequests.get(1).getPath().getValue(), calculatePath("callback"));
private ByteBuf getBody(HttpRequest httpRequest) { ByteBuf content = Unpooled.buffer(0, 0); Body body = httpRequest.getBody(); if (body != null) { Object bodyContents = body.getValue(); Charset bodyCharset = body.getCharset(ContentTypeMapper.getCharsetFromContentTypeHeader(httpRequest.getFirstHeader(CONTENT_TYPE.toString()))); if (bodyContents instanceof byte[]) { content = Unpooled.copiedBuffer((byte[]) bodyContents); } else if (bodyContents instanceof String) { content = Unpooled.copiedBuffer(((String) bodyContents).getBytes(bodyCharset)); } else if (body.toString() != null) { content = Unpooled.copiedBuffer(body.toString().getBytes(bodyCharset)); } } return content; }
public HttpRequestDTO(HttpRequest httpRequest, Boolean not) { super(not); if (httpRequest != null) { method = httpRequest.getMethod(); path = httpRequest.getPath(); headers = httpRequest.getHeaders(); cookies = httpRequest.getCookies(); queryStringParameters = httpRequest.getQueryStringParameters(); body = BodyDTO.createDTO(httpRequest.getBody()); keepAlive = httpRequest.isKeepAlive(); secure = httpRequest.isSecure(); } }
public HttpRequestTemplateObject(HttpRequest httpRequest) { if (httpRequest != null) { method = httpRequest.getMethod().getValue(); path = httpRequest.getPath().getValue(); for (Header header : httpRequest.getHeaderList()) { headers.put(header.getName().getValue(), Lists.transform(header.getValues(), new Function<NottableString, String>() { public String apply(NottableString input) { return input.getValue(); } })); } for (Cookie cookie : httpRequest.getCookieList()) { cookies.put(cookie.getName().getValue(), cookie.getValue().getValue()); } for (Parameter parameter : httpRequest.getQueryStringParameterList()) { queryStringParameters.put(parameter.getName().getValue(), Lists.transform(parameter.getValues(), new Function<NottableString, String>() { public String apply(NottableString input) { return input.getValue(); } })); } body = BodyDTO.createDTO(httpRequest.getBody()); keepAlive = httpRequest.isKeepAlive(); secure = httpRequest.isSecure(); } }
public HttpRequest update(HttpRequest replaceRequest) { if (!Strings.isNullOrEmpty(replaceRequest.getMethod().getValue())) { withMethod(replaceRequest.getMethod()); } if (!Strings.isNullOrEmpty(replaceRequest.getPath().getValue())) { withPath(replaceRequest.getPath()); } for (Header header : replaceRequest.getHeaderList()) { getHeaders().replaceEntry(header); } for (Cookie cookie : replaceRequest.getCookieList()) { withCookie(cookie); } for (Parameter parameter : replaceRequest.getQueryStringParameterList()) { getQueryStringParameters().replaceEntry(parameter); } if (replaceRequest.getBody() != null) { withBody(replaceRequest.getBody()); } if (replaceRequest.isSecure() != null) { withSecure(replaceRequest.isSecure()); } if (replaceRequest.isKeepAlive() != null) { withKeepAlive(replaceRequest.isKeepAlive()); } return this; } }
public HttpRequestMatcher(HttpRequest httpRequest, MockServerLogger mockServerLogger) { this.httpRequest = httpRequest; this.mockServerLogger = mockServerLogger; if (httpRequest != null) { withMethod(httpRequest.getMethod()); withPath(httpRequest.getPath()); withQueryStringParameters(httpRequest.getQueryStringParameters()); withBody(httpRequest.getBody()); withHeaders(httpRequest.getHeaders()); withCookies(httpRequest.getCookies()); withKeepAlive(httpRequest.isKeepAlive()); withSsl(httpRequest.isSecure()); } }
public HttpRequestMatcher(Expectation expectation, MockServerLogger mockServerLogger) { this.expectation = expectation; this.httpRequest = expectation.getHttpRequest(); this.mockServerLogger = mockServerLogger; if (httpRequest != null) { withMethod(httpRequest.getMethod()); withPath(httpRequest.getPath()); withQueryStringParameters(httpRequest.getQueryStringParameters()); withBody(httpRequest.getBody()); withHeaders(httpRequest.getHeaders()); withCookies(httpRequest.getCookies()); withKeepAlive(httpRequest.isKeepAlive()); withSsl(httpRequest.isSecure()); } }