@Test public void testBodyMatchForDifferentRequestTypes() { RecordedHttpBody incomingHttpBody = new RecordedStringHttpBody("------wxyz1234abcd5e\nContent-Disposition: form-data; name=\"org\" \nMMM\n------wxyz1234abcd5e"); Multimap<String, String> headers1 = LinkedHashMultimap.create(); headers1.put(HttpHeaders.CONTENT_TYPE, "multipart/form-data; boundary=wxyz1234abcd5e"); Multimap<String, String> headers2 = LinkedHashMultimap.create(); RecordedHttpRequest recordedHttpRequest1 = new RecordedHttpRequest("POST", null, headers1, incomingHttpBody); RecordedHttpRequest recordedHttpRequest2 = new RecordedHttpRequest("GET", null, headers2, null); MatchRule matchRule = new MatchBodyWithAnyBoundary(); Assert.assertFalse(matchRule.test(recordedHttpRequest1, recordedHttpRequest2)); }
@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 { if (incomingRequest.getHeaders().containsKey(HttpHeaders.CONTENT_TYPE) && expectedRequest.getHeaders().containsKey(HttpHeaders.CONTENT_TYPE) && incomingRequest.getHeaders().get(HttpHeaders.CONTENT_TYPE).iterator().next().contains(MULTIPART) && expectedRequest.getHeaders().get(HttpHeaders.CONTENT_TYPE).iterator().next().contains(MULTIPART)) { String currentRequestBoundaryValue = Arrays.stream(incomingRequest.getHeaders().get(HttpHeaders.CONTENT_TYPE).iterator().next() .split(SEMI_COLON)) .filter((attr) -> attr.contains(BOUNDARY)) .findFirst() .orElse(EMPTY) .replaceFirst(BOUNDARY, EMPTY) .trim(); String recordedRequestBoundaryValue = Arrays.stream(expectedRequest.getHeaders().get(HttpHeaders.CONTENT_TYPE).iterator().next() .split(SEMI_COLON)) .filter((attr) -> attr.contains(BOUNDARY)) .findFirst() .orElse(EMPTY) .replaceFirst(BOUNDARY, EMPTY) .trim(); if (!currentRequestBoundaryValue.equals(recordedRequestBoundaryValue)) { String recordedBody = new String(expectedBody.getContent(charSet2)).replaceAll(recordedRequestBoundaryValue, currentRequestBoundaryValue); expectedBody = new RecordedByteHttpBody(recordedBody.getBytes(charSet2)); } } return Arrays.equals(incomingBody.getContent(charSet1), expectedBody.getContent(charSet2)); } catch (IOException e) { throw new RuntimeException("Failed to convert to byte arrays", e); } }
MatchBodyWithAnyBoundary extends BaseMatchRule { @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 { if (incomingRequest.getHeaders().containsKey(HttpHeaders.CONTENT_TYPE) && expectedRequest.getHeaders().containsKey(HttpHeaders.CONTENT_TYPE) && incomingRequest.getHeaders().get(HttpHeaders.CONTENT_TYPE).iterator().next().contains(MULTIPART) && expectedRequest.getHeaders().get(HttpHeaders.CONTENT_TYPE).iterator().next().contains(MULTIPART)) { String currentRequestBoundaryValue = Arrays.stream(incomingRequest.getHeaders().get(HttpHeaders.CONTENT_TYPE).iterator().next() .split(SEMI_COLON)) .filter((attr) -> attr.contains(BOUNDARY)) .findFirst() .orElse(EMPTY) .replaceFirst(BOUNDARY, EMPTY) .trim(); String recordedRequestBoundaryValue = Arrays.stream(expectedRequest.getHeaders().get(HttpHeaders.CONTENT_TYPE).iterator().next() .split(SEMI_COLON)) .filter((attr) -> attr.contains(BOUNDARY)) .findFirst() .orElse(EMPTY) .replaceFirst(BOUNDARY, EMPTY) .trim(); if (!currentRequestBoundaryValue.equals(recordedRequestBoundaryValue)) { String recordedBody = new String(expectedBody.getContent(charSet2)).replaceAll(recordedRequestBoundaryValue, currentRequestBoundaryValue); expectedBody = new RecordedByteHttpBody(recordedBody.getBytes(charSet2)); } } return Arrays.equals(incomingBody.getContent(charSet1), expectedBody.getContent(charSet2)); } catch (IOException e) { throw new RuntimeException("Failed to convert to byte arrays", e); } } }
MatchBodyWithAnyBoundary extends BaseMatchRule { @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 { if (incomingRequest.getHeaders().containsKey(HttpHeaders.CONTENT_TYPE) && expectedRequest.getHeaders().containsKey(HttpHeaders.CONTENT_TYPE) && incomingRequest.getHeaders().get(HttpHeaders.CONTENT_TYPE).iterator().next().contains(MULTIPART) && expectedRequest.getHeaders().get(HttpHeaders.CONTENT_TYPE).iterator().next().contains(MULTIPART)) { String currentRequestBoundaryValue = Arrays.stream(incomingRequest.getHeaders().get(HttpHeaders.CONTENT_TYPE).iterator().next() .split(SEMI_COLON)) .filter((attr) -> attr.contains(BOUNDARY)) .findFirst() .orElse(EMPTY) .replaceFirst(BOUNDARY, EMPTY) .trim(); String recordedRequestBoundaryValue = Arrays.stream(expectedRequest.getHeaders().get(HttpHeaders.CONTENT_TYPE).iterator().next() .split(SEMI_COLON)) .filter((attr) -> attr.contains(BOUNDARY)) .findFirst() .orElse(EMPTY) .replaceFirst(BOUNDARY, EMPTY) .trim(); if (!currentRequestBoundaryValue.equals(recordedRequestBoundaryValue)) { String recordedBody = new String(expectedBody.getContent(charSet2)).replaceAll(recordedRequestBoundaryValue, currentRequestBoundaryValue); expectedBody = new RecordedByteHttpBody(recordedBody.getBytes(charSet2)); } } return Arrays.equals(incomingBody.getContent(charSet1), expectedBody.getContent(charSet2)); } catch (IOException e) { throw new RuntimeException("Failed to convert to byte arrays", e); } }  }
MatchBodyWithAnyBoundary extends BaseMatchRule { @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 { if (incomingRequest.getHeaders().containsKey(HttpHeaders.CONTENT_TYPE) && expectedRequest.getHeaders().containsKey(HttpHeaders.CONTENT_TYPE) && incomingRequest.getHeaders().get(HttpHeaders.CONTENT_TYPE).iterator().next().contains(MULTIPART) && expectedRequest.getHeaders().get(HttpHeaders.CONTENT_TYPE).iterator().next().contains(MULTIPART)) { String currentRequestBoundaryValue = Arrays.stream(incomingRequest.getHeaders().get(HttpHeaders.CONTENT_TYPE).iterator().next() .split(SEMI_COLON)) .filter((attr) -> attr.contains(BOUNDARY)) .findFirst() .orElse(EMPTY) .replaceFirst(BOUNDARY, EMPTY) .trim(); String recordedRequestBoundaryValue = Arrays.stream(expectedRequest.getHeaders().get(HttpHeaders.CONTENT_TYPE).iterator().next() .split(SEMI_COLON)) .filter((attr) -> attr.contains(BOUNDARY)) .findFirst() .orElse(EMPTY) .replaceFirst(BOUNDARY, EMPTY) .trim(); if (!currentRequestBoundaryValue.equals(recordedRequestBoundaryValue)) { String recordedBody = new String(expectedBody.getContent(charSet2)).replaceAll(recordedRequestBoundaryValue, currentRequestBoundaryValue); expectedBody = new RecordedByteHttpBody(recordedBody.getBytes(charSet2)); } } return Arrays.equals(incomingBody.getContent(charSet1), expectedBody.getContent(charSet2)); } catch (IOException e) { throw new RuntimeException("Failed to convert to byte arrays", e); } }  @Override boolean test(RecordedHttpRequest incomingRequest, RecordedHttpRequest expectedRequest); @Override String getMatchFailureDescriptionForRequests(RecordedHttpRequest incomingRequest, RecordedHttpRequest expectedRequest); }
MatchBodyWithAnyBoundary extends BaseMatchRule { @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 { if (incomingRequest.getHeaders().containsKey(HttpHeaders.CONTENT_TYPE) && expectedRequest.getHeaders().containsKey(HttpHeaders.CONTENT_TYPE) && incomingRequest.getHeaders().get(HttpHeaders.CONTENT_TYPE).iterator().next().contains(MULTIPART) && expectedRequest.getHeaders().get(HttpHeaders.CONTENT_TYPE).iterator().next().contains(MULTIPART)) { String currentRequestBoundaryValue = Arrays.stream(incomingRequest.getHeaders().get(HttpHeaders.CONTENT_TYPE).iterator().next() .split(SEMI_COLON)) .filter((attr) -> attr.contains(BOUNDARY)) .findFirst() .orElse(EMPTY) .replaceFirst(BOUNDARY, EMPTY) .trim(); String recordedRequestBoundaryValue = Arrays.stream(expectedRequest.getHeaders().get(HttpHeaders.CONTENT_TYPE).iterator().next() .split(SEMI_COLON)) .filter((attr) -> attr.contains(BOUNDARY)) .findFirst() .orElse(EMPTY) .replaceFirst(BOUNDARY, EMPTY) .trim(); if (!currentRequestBoundaryValue.equals(recordedRequestBoundaryValue)) { String recordedBody = new String(expectedBody.getContent(charSet2)).replaceAll(recordedRequestBoundaryValue, currentRequestBoundaryValue); expectedBody = new RecordedByteHttpBody(recordedBody.getBytes(charSet2)); } } return Arrays.equals(incomingBody.getContent(charSet1), expectedBody.getContent(charSet2)); } catch (IOException e) { throw new RuntimeException("Failed to convert to byte arrays", e); } }  @Override boolean test(RecordedHttpRequest incomingRequest, RecordedHttpRequest expectedRequest); @Override String getMatchFailureDescriptionForRequests(RecordedHttpRequest incomingRequest, RecordedHttpRequest expectedRequest);  }