private RecordedHttpBody getBodyFromRequest(RecordedHttpRequest request) { RecordedHttpBody body = request.getHttpBody(); if (body instanceof RecordedEncodedHttpBody) { return ((RecordedEncodedHttpBody) body).getDecodedBody(); } else { return body; } } }
private RecordedHttpBody getBodyFromRequest(RecordedHttpRequest request) { RecordedHttpBody body = request.getHttpBody(); if (body instanceof RecordedEncodedHttpBody) { return ((RecordedEncodedHttpBody) body).getDecodedBody(); } else { return body; } } }
public static FullHttpResponse generateNoMatchResponse(RecordedHttpRequest recordedHttpRequest) { StringBuilder bodyTextBuilder = new StringBuilder(); bodyTextBuilder.append("No Matching Request\n").append("Incoming Request Method: ") .append(recordedHttpRequest.getMethod()).append("\n").append("Incoming Request URI: ") .append(recordedHttpRequest.getUri()).append("\n").append("Incoming Request Headers: ") .append(recordedHttpRequest.getHeaders()).append("\n"); RecordedHttpBody incomingBody = recordedHttpRequest.getHttpBody(); if (incomingBody != null) { if (incomingBody instanceof RecordedEncodedHttpBody) { incomingBody = ((RecordedEncodedHttpBody) incomingBody).getDecodedBody(); } if (incomingBody instanceof RecordedStringHttpBody) { bodyTextBuilder.append("Incoming Request Body: ").append(((RecordedStringHttpBody) incomingBody).getContent()); } else { bodyTextBuilder.append("Incoming Request Body: (binary content)"); } } ByteBuf badRequestBody = Unpooled.wrappedBuffer(bodyTextBuilder.toString().getBytes(Charset.forName("UTF-8"))); return new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.BAD_REQUEST, badRequestBody); }
@Override public boolean test(RecordedHttpRequest incomingRequest, RecordedHttpRequest expectedRequest) { String charSet1 = incomingRequest.getCharset(); String charSet2 = expectedRequest.getCharset(); RecordedHttpBody incomingBody = incomingRequest.getHttpBody(); if (incomingBody == null) { incomingBody = new RecordedByteHttpBody(new byte[0]); } RecordedHttpBody expectedBody = expectedRequest.getHttpBody(); if (expectedBody == null) { expectedBody = new RecordedByteHttpBody(new byte[0]); } try { return Arrays.equals(incomingBody.getContent(charSet1), expectedBody.getContent(charSet2)); } catch (IOException e) { //TODO: PLACEHOLDER, error handling will be in separate RB. throw new RuntimeException("Failed to convert to byte arrays", e); } }
String charSet1 = incomingRequest.getCharset(); String charSet2 = expectedRequest.getCharset(); RecordedHttpBody incomingBody = incomingRequest.getHttpBody(); if (incomingBody == null) { incomingBody = new RecordedByteHttpBody(new byte[0]); RecordedHttpBody expectedBody = expectedRequest.getHttpBody(); if (expectedBody == null) { expectedBody = new RecordedByteHttpBody(new byte[0]);
private Map<String, String> getPostParametersFromRequest(RecordedHttpRequest request) throws UnsupportedEncodingException { RecordedHttpBody body = request.getHttpBody(); if (body instanceof RecordedEncodedHttpBody) { body = ((RecordedEncodedHttpBody) body).getDecodedBody(); } assert (body instanceof RecordedStringHttpBody); String content = ((RecordedStringHttpBody) body).getContent(); return HttpUtilities.stringToUrlParams(content, request.getCharset()); }
@Test public void testBuildContent() throws Exception { HttpRequest nettyRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_0, HttpMethod.GET, "www.google.com"); RecordedHttpRequestBuilder recordedHttpRequestBuilder = new RecordedHttpRequestBuilder(nettyRequest); String charset = "UTF-8"; String str1 = "first content"; HttpContent httpContent1 = new DefaultHttpContent(Unpooled.copiedBuffer(str1.getBytes(charset))); recordedHttpRequestBuilder.appendHttpContent(httpContent1); String str2 = "second content"; HttpContent httpContent2 = new DefaultHttpContent(Unpooled.copiedBuffer(str2.getBytes(charset))); recordedHttpRequestBuilder.appendHttpContent(httpContent2); String lastStr = "Last chunk"; HttpContent lastContent = new DefaultLastHttpContent(Unpooled.copiedBuffer(lastStr.getBytes(charset))); recordedHttpRequestBuilder.appendHttpContent(lastContent); RecordedHttpRequest recordedHttpRequest = recordedHttpRequestBuilder.build(); Assert .assertEquals((str1 + str2 + lastStr).getBytes(charset), recordedHttpRequest.getHttpBody().getContent(charset)); } }