/** * @return rule to match the request POST body parameters contained in the whitelist */ public static MatchRule matchBodyPostParametersWithWhitelist(Set<String> whiteList) { return new MatchBodyPostParameters(new MatchRuleWhitelistTransform(whiteList)); }
@Override public boolean test(RecordedHttpRequest incomingRequest, RecordedHttpRequest expectedRequest) { if (HttpUtilities.isFormURLEncodedContentType(incomingRequest.getContentType()) && HttpUtilities .isFormURLEncodedContentType(expectedRequest.getContentType())) { try { Map<String, String> incomingParams = getPostParametersFromRequest(incomingRequest); Map<String, String> expectedParams = getPostParametersFromRequest(expectedRequest); return testParameterEquivalency(incomingParams, expectedParams); } catch (UnsupportedEncodingException e) { logger.error("Caught exception " + e + " while decoding POST parameters"); } } return super.test(incomingRequest, expectedRequest); }
@Override public String getMatchFailureDescriptionForRequests(RecordedHttpRequest incomingRequest, RecordedHttpRequest expectedRequest) { StringBuilder resultBuilder = new StringBuilder("HTTP Body Parameters Mismatch"); if (_transform instanceof MatchRuleBlacklistTransform) { resultBuilder.append(" (with Blacklist)"); } else if (_transform instanceof MatchRuleWhitelistTransform) { resultBuilder.append(" (with Whitelist)"); } try { Map<String, String> incomingParams = getPostParametersFromRequest(incomingRequest); Map<String, String> expectedParams = getPostParametersFromRequest(expectedRequest); resultBuilder.append("%n") .append(String.format("Incoming Parameters: %s%n", _transform.transform(incomingParams))) .append(String.format("Expected Parameters: %s%n", _transform.transform(expectedParams))); } catch (UnsupportedEncodingException e) { logger.error("Caught exception " + e + " while decoding POST parameters"); } return resultBuilder.toString(); } }
/** * @return rule to match the request POST body parameters, except for those in the blacklist */ public static MatchRule matchBodyPostParametersWithBlacklist(Set<String> blackList) { return new MatchBodyPostParameters(new MatchRuleBlacklistTransform(blackList)); } }
@Test public void testDifferentParameters() throws Exception { RecordedStringHttpBody stringHttpBody1 = new RecordedStringHttpBody("a=a&b=b&c=c"); RecordedStringHttpBody stringHttpBody2 = new RecordedStringHttpBody("a=a&b=b&c=ccc"); Multimap<String, String> headers = LinkedHashMultimap.create(); headers.put("Content-Type", "application/x-www-form-urlencoded"); RecordedHttpRequest recordedHttpRequest1 = new RecordedHttpRequest(null, null, headers, stringHttpBody1); RecordedHttpRequest recordedHttpRequest2 = new RecordedHttpRequest(null, null, headers, stringHttpBody2); MatchRule matchRule = new MatchBodyPostParameters(); Assert.assertFalse(matchRule.test(recordedHttpRequest1, recordedHttpRequest2)); }
@Test public void testExactMatch() throws Exception { RecordedStringHttpBody stringHttpBody1 = new RecordedStringHttpBody("a=a&b=b&c=c"); RecordedStringHttpBody stringHttpBody2 = new RecordedStringHttpBody("a=a&b=b&c=c"); Multimap<String, String> headers = LinkedHashMultimap.create(); headers.put("Content-Type", "application/x-www-form-urlencoded"); RecordedHttpRequest recordedHttpRequest1 = new RecordedHttpRequest(null, null, headers, stringHttpBody1); RecordedHttpRequest recordedHttpRequest2 = new RecordedHttpRequest(null, null, headers, stringHttpBody2); MatchRule matchRule = new MatchBodyPostParameters(); Assert.assertTrue(matchRule.test(recordedHttpRequest1, recordedHttpRequest2)); }
@Test public void testNotMatchDifferentOrder() throws Exception { RecordedStringHttpBody stringHttpBody1 = new RecordedStringHttpBody("a=a&b=b&c=c"); RecordedStringHttpBody stringHttpBody2 = new RecordedStringHttpBody("a=a&c=c&b=b"); Multimap<String, String> headers = LinkedHashMultimap.create(); headers.put("Content-Type", "application/x-www-form-urlencoded"); RecordedHttpRequest recordedHttpRequest1 = new RecordedHttpRequest(null, null, headers, stringHttpBody1); RecordedHttpRequest recordedHttpRequest2 = new RecordedHttpRequest(null, null, headers, stringHttpBody2); MatchRule matchRule = new MatchBodyPostParameters(); Assert.assertFalse(matchRule.test(recordedHttpRequest1, recordedHttpRequest2)); }
@Test public void testMatchWithSomeNullParameters() throws Exception { RecordedStringHttpBody stringHttpBody1 = new RecordedStringHttpBody("a=a&b=&c=c&d="); RecordedStringHttpBody stringHttpBody2 = new RecordedStringHttpBody("a=a&b=&c=c&d="); Multimap<String, String> headers = LinkedHashMultimap.create(); headers.put("Content-Type", "application/x-www-form-urlencoded"); RecordedHttpRequest recordedHttpRequest1 = new RecordedHttpRequest(null, null, headers, stringHttpBody1); RecordedHttpRequest recordedHttpRequest2 = new RecordedHttpRequest(null, null, headers, stringHttpBody2); MatchRule matchRule = new MatchBodyPostParameters(); Assert.assertTrue(matchRule.test(recordedHttpRequest1, recordedHttpRequest2)); } }