/** * The exact string body to match on such as "this is an exact string body" * * @param body the body on such as "this is an exact string body" */ public HttpRequest withBody(String body) { this.body = new StringBody(body); return this; }
public StringBody(String value, boolean subString, MediaType contentType) { super(Type.STRING, contentType); this.value = value; this.subString = subString; if (value != null) { this.rawBinaryData = value.getBytes(determineCharacterSet(contentType, DEFAULT_HTTP_CHARACTER_SET)); } else { this.rawBinaryData = new byte[0]; } }
public StringBodyDTO(StringBody stringBody, Boolean not) { super(stringBody.getType(), not, stringBody.getContentType()); string = stringBody.getValue(); subString = stringBody.isSubString(); }
StringBody stringBody = (StringBody) body; bodyDTOMatcher = new StringBodyDTO(stringBody); if (stringBody.isSubString()) { this.bodyMatcher = new SubStringMatcher(mockServerLogger, string(stringBody.getValue(), stringBody.getNot())); } else { this.bodyMatcher = new ExactStringMatcher(mockServerLogger, string(stringBody.getValue(), stringBody.getNot()));
@Test public void shouldReturnResponseByMatchingStringBody() throws IOException, URISyntaxException { // when getMockServerClient() .when( request() .withBody( exact("some_random_body") ), Times.exactly(2) ) .respond( response() .withBody("some_string_body_response") ); // then HttpClient httpClient = createHttpClient(); HttpPost request = new HttpPost( new URIBuilder() .setScheme("http") .setHost("localhost") .setPort(getServerPort()) .setPath(addContextToPath("some_path")) .build() ); request.setEntity(new StringEntity("some_random_body")); HttpResponse response = httpClient.execute(request); assertThat(new String(EntityUtils.toByteArray(response.getEntity()), UTF_8), is("some_string_body_response")); assertThat(response.getStatusLine().getStatusCode(), is(OK_200.code())); }
public StringBodyDTO(StringBody stringBody) { this(stringBody, stringBody.getNot()); }
if (body instanceof StringBody && !((StringBody) body).getValue().isEmpty()) { jgen.writeObjectField("body", body); } else if (body instanceof JsonBody && !((JsonBody) body).getValue().isEmpty()) {
StringBody stringBody = (StringBody) body; bodyDTOMatcher = new StringBodyDTO(stringBody); if (stringBody.isSubString()) { this.bodyMatcher = new SubStringMatcher(mockServerLogger, string(stringBody.getValue(), stringBody.getNot())); } else { this.bodyMatcher = new ExactStringMatcher(mockServerLogger, string(stringBody.getValue(), stringBody.getNot()));
param("queryStringParameterTwoName", "queryStringParameterTwoValue") .withBody(exact("some_body")) .withHeaders(header("headerName", "headerValue")) .withCookies(cookie("cookieName", "cookieValue")) param("queryStringParameterTwoName", "queryStringParameterTwoValue") .withBody(exact("some_body")) .withHeaders(header("headerName", "headerOtherValue")) .withCookies(cookie("cookieName", "cookieValue")), param("queryStringParameterTwoName", "queryStringParameterTwoValue") .withBody(exact("some_body")) .withHeaders(header("headerName", "headerOtherValue")) .withCookies(cookie("cookieName", "cookieValue")),
public static BodyWithContentTypeDTO createDTO(BodyWithContentType body) { BodyWithContentTypeDTO result = null; if (body instanceof BinaryBody) { BinaryBody binaryBody = (BinaryBody) body; result = new BinaryBodyDTO(binaryBody, binaryBody.getNot()); } else if (body instanceof JsonBody) { JsonBody jsonBody = (JsonBody) body; result = new JsonBodyDTO(jsonBody, jsonBody.getNot()); } else if (body instanceof ParameterBody) { ParameterBody parameterBody = (ParameterBody) body; result = new ParameterBodyDTO(parameterBody, parameterBody.getNot()); } else if (body instanceof StringBody) { StringBody stringBody = (StringBody) body; result = new StringBodyDTO(stringBody, stringBody.getNot()); } else if (body instanceof XmlBody) { XmlBody xmlBody = (XmlBody) body; result = new XmlBodyDTO(xmlBody, xmlBody.getNot()); } return result; }
output.append(".withBody("); StringBody stringBody = (StringBody) request.getBody(); output.append("new StringBody(\"").append(StringEscapeUtils.escapeJava(stringBody.getValue())).append("\")"); output.append(")"); } else if (request.getBody() instanceof ParameterBody) {
/** * The exact string body to match on such as "this is an exact string body" * * @param body the body on such as "this is an exact string body" * @param charset character set the string will be encoded in */ public HttpRequest withBody(String body, Charset charset) { if (body != null) { this.body = new StringBody(body, charset); } return this; }
@Override public void serialize(StringBody stringBody, JsonGenerator jgen, SerializerProvider provider) throws IOException { boolean notFieldSetAndNotDefault = stringBody.getNot() != null && stringBody.getNot(); boolean subStringFieldNotDefault = stringBody.isSubString(); boolean contentTypeFieldSet = stringBody.getContentType() != null; if (notFieldSetAndNotDefault || contentTypeFieldSet || subStringFieldNotDefault) { jgen.writeStartObject(); if (notFieldSetAndNotDefault) { jgen.writeBooleanField("not", true); } jgen.writeStringField("type", stringBody.getType().name()); jgen.writeStringField("string", stringBody.getValue()); if (subStringFieldNotDefault) { jgen.writeBooleanField("subString", true); } if (contentTypeFieldSet) { jgen.writeStringField("contentType", stringBody.getContentType()); } jgen.writeEndObject(); } else { jgen.writeString(stringBody.getValue()); } } }
param("queryStringParameterTwoName", "queryStringParameterTwoValue") .withBody(exact("some_body")) .withHeaders(header("headerName", "headerValue")) .withCookies(cookie("cookieName", "cookieValue")) param("queryStringParameterTwoName", "queryStringParameterTwoValue") .withBody(exact("some_body")) .withHeaders(header("headerOtherName", "headerValue")) .withCookies(cookie("cookieName", "cookieValue")), param("queryStringParameterTwoName", "queryStringParameterTwoValue") .withBody(exact("some_body")) .withHeaders(header("headerOtherName", "headerValue")) .withCookies(cookie("cookieName", "cookieValue")),
} else if (body instanceof StringBody) { StringBody stringBody = (StringBody) body; result = new StringBodyDTO(stringBody, stringBody.getNot()); } else if (body instanceof XmlBody) { XmlBody xmlBody = (XmlBody) body;
if (body instanceof StringBody && !((StringBody) body).getValue().isEmpty()) { jgen.writeObjectField("body", body); } else if (body instanceof JsonBody && !((JsonBody) body).getValue().isEmpty()) {
public StringBody(String value, boolean subString, MediaType contentType) { super(Type.STRING, contentType); this.value = value; this.subString = subString; if (value != null) { this.rawBinaryData = value.getBytes(determineCharacterSet(contentType, DEFAULT_HTTP_CHARACTER_SET)); } else { this.rawBinaryData = new byte[0]; } }
/** * Set response body to return as a string response body. The character set will be determined by the Content-Type header * on the response. To force the character set, use {@link #withBody(String, Charset)}. * * @param body a string */ public HttpResponse withBody(String body) { if (body != null) { this.body = new StringBody(body); } return this; }
public StringBodyDTO(StringBody stringBody, Boolean not) { super(stringBody.getType(), not, stringBody.getContentType()); string = stringBody.getValue(); subString = stringBody.isSubString(); }
param("queryStringParameterTwoName", "queryStringParameterTwoValue") .withBody(exact("some_body")) .withHeaders(header("headerName", "headerValue")) .withCookies(cookie("cookieName", "cookieValue")) param("queryStringParameterTwoName", "queryStringParameterTwoValue") .withBody(exact("some_body")) .withHeaders(header("headerName", "headerValue")) .withCookies(cookie("cookieName", "cookieOtherValue")), param("queryStringParameterTwoName", "queryStringParameterTwoValue") .withBody(exact("some_body")) .withHeaders(header("headerName", "headerValue")) .withCookies(cookie("cookieName", "cookieOtherValue")),