protected boolean testUriEquivalency(URI incomingUri, URI expectedUri) { try { URI incomingCanonicalizedUri = getCanonicalizedUri(incomingUri); URI expectedCanonicalizedUri = getCanonicalizedUri(expectedUri); return incomingCanonicalizedUri.equals(expectedCanonicalizedUri); } catch (URISyntaxException e) { LOGGER.error("Caught exception " + e + " while constructing modified URI"); } return incomingUri.equals(expectedUri); }
/** * Returns a canonicalized version of the Uri, accounting for the default port for the scheme * @param uri * @return a canonicalized URI with the default port for the scheme (http or https) explicitly added if no port is specified * @throws java.net.URISyntaxException */ private URI getCanonicalizedUri(URI uri) throws URISyntaxException { int defaultPort = getPortForScheme(uri.getScheme()); if (uri.getPort() == -1 && defaultPort != -1) { return new URI(uri.getScheme(), uri.getUserInfo(), uri.getHost(), defaultPort, uri.getPath(), uri.getQuery(), uri.getFragment()); } return uri; } }
@Override protected boolean testUriEquivalency(URI incomingUri, URI expectedUri) { try { URI modifiedIncomingUri = getModifiedUri(incomingUri); URI modifiedExpectedUri = getModifiedUri(expectedUri); return super.testUriEquivalency(modifiedIncomingUri, modifiedExpectedUri); } catch (URISyntaxException | UnsupportedEncodingException e) { LOGGER.error("Caught exception " + e + " while constructing modified URI"); } return false; }
@Test public void testIsMatch() throws Exception { RecordedHttpRequest recordedHttpRequest1 = new RecordedHttpRequest(null, new URI("google.com"), null, null); RecordedHttpRequest recordedHttpRequest2 = new RecordedHttpRequest(null, new URI("google.com"), null, null); MatchRule matchRule = new MatchUri(); Assert.assertTrue(matchRule.test(recordedHttpRequest1, recordedHttpRequest2)); }
@Test public void testMatchDefaultPortHttp() throws Exception { RecordedHttpRequest recordedHttpRequest1 = new RecordedHttpRequest(null, new URI("http://www.example.org/"), null, null); RecordedHttpRequest recordedHttpRequest2 = new RecordedHttpRequest(null, new URI("http://www.example.org:80/"), null, null); MatchRule matchRule = new MatchUri(); Assert.assertTrue(matchRule.test(recordedHttpRequest1, recordedHttpRequest2)); }
@Test public void testNotMatchNonDefaultPort() throws Exception { RecordedHttpRequest recordedHttpRequest1 = new RecordedHttpRequest(null, new URI("http://www.example.org/"), null, null); RecordedHttpRequest recordedHttpRequest2 = new RecordedHttpRequest(null, new URI("http://www.example.org:8080/"), null, null); MatchRule matchRule = new MatchUri(); Assert.assertFalse(matchRule.test(recordedHttpRequest1, recordedHttpRequest2)); }
@Override public boolean test(RecordedHttpRequest incomingRequest, RecordedHttpRequest expectedRequest) { return testUriEquivalency(incomingRequest.getUri(), expectedRequest.getUri()); }
@Test public void testIsNotMatch() throws URISyntaxException { RecordedHttpRequest recordedHttpRequest1 = new RecordedHttpRequest(null, new URI("google.com"), null, null); RecordedHttpRequest recordedHttpRequest2 = new RecordedHttpRequest(null, new URI("yahoo.com"), null, null); MatchRule matchRule = new MatchUri(); Assert.assertFalse(matchRule.test(recordedHttpRequest1, recordedHttpRequest2)); } }
@Test public void testMatchDefaultPortHttps() throws Exception { RecordedHttpRequest recordedHttpRequest1 = new RecordedHttpRequest(null, new URI("https://www.example.org/"), null, null); RecordedHttpRequest recordedHttpRequest2 = new RecordedHttpRequest(null, new URI("https://www.example.org:443/"), null, null); MatchRule matchRule = new MatchUri(); Assert.assertTrue(matchRule.test(recordedHttpRequest1, recordedHttpRequest2)); }
/** * @return rule to match request URI and Method */ public static MatchRule matchMethodUri() { CompositeMatchRule compositeMatchRule = new CompositeMatchRule(); compositeMatchRule.addRule(new MatchMethod()); compositeMatchRule.addRule(new MatchUri()); return compositeMatchRule; }
@Test public void testIsMatch() throws Exception { RecordedHttpRequest recordedHttpRequest1 = new RecordedHttpRequest("get", new URI("google.com"), null, null); RecordedHttpRequest recordedHttpRequest2 = new RecordedHttpRequest("get", new URI("google.com"), null, null); Set<MatchRule> matchRuleSet = new HashSet<MatchRule>(); matchRuleSet.add(new MatchUri()); matchRuleSet.add(new MatchMethod()); CompositeMatchRule compositeMatchRule = new CompositeMatchRule(); compositeMatchRule.addAll(matchRuleSet); Assert.assertTrue(compositeMatchRule.test(recordedHttpRequest1, recordedHttpRequest2)); }
@Test public void testIsNotMatch() throws Exception { RecordedHttpRequest recordedHttpRequest1 = new RecordedHttpRequest("get", new URI("google.com"), null, null); RecordedHttpRequest recordedHttpRequest2 = new RecordedHttpRequest("post", new URI("google.com"), null, null); Set<MatchRule> matchRuleSet = new HashSet<MatchRule>(); matchRuleSet.add(new MatchUri()); matchRuleSet.add(new MatchMethod()); CompositeMatchRule compositeMatchRule = new CompositeMatchRule(); compositeMatchRule.addAll(matchRuleSet); Assert.assertFalse(compositeMatchRule.test(recordedHttpRequest1, recordedHttpRequest2)); } }
/** * @return rule to match request URI, Body and Method */ public static MatchRule matchMethodBodyUri() { CompositeMatchRule compositeMatchRule = new CompositeMatchRule(); compositeMatchRule.addRule(new MatchBody()); compositeMatchRule.addRule(new MatchMethod()); compositeMatchRule.addRule(new MatchUri()); return compositeMatchRule; }
/** * @return rule to match request Method, URI and Body with any boundary */ public static MatchRule matchMethodUriBodyWithAnyBoundary() { CompositeMatchRule compositeMatchRule = new CompositeMatchRule(); compositeMatchRule.addRule(new MatchMethod()); compositeMatchRule.addRule(new MatchUri()); compositeMatchRule.addRule(new MatchBodyWithAnyBoundary()); return compositeMatchRule; }
/** * @return rule to match request URI, Method, Headers, and Body */ public static MatchRule matchEntireRequest() { CompositeMatchRule compositeMatchRule = new CompositeMatchRule(); compositeMatchRule.addRule(new MatchMethod()); compositeMatchRule.addRule(new MatchUri()); compositeMatchRule.addRule(new MatchHeaders()); compositeMatchRule.addRule(new MatchBody()); return compositeMatchRule; }