@Test public void shouldLogMessageIndicatingFailedHeaderMatch() { context.checking( new Expectations() { { one(notifier) .info( "URL /for/logging is match, but header Content-Type is not. For a match, value should equal text/xml"); } }); RequestPattern requestPattern = new RequestPattern(POST, "/for/logging"); ValuePattern headerPattern = new ValuePattern(); headerPattern.setEqualTo("text/xml"); requestPattern.addHeader("Content-Type", headerPattern); Request request = aRequest(context) .withUrl("/for/logging") .withMethod(POST) .withHeader("Content-Type", "text/plain") .build(); requestPattern.isMatchedBy(request); }
@Test public void shouldMatchOnBodyPattern() { RequestPattern requestPattern = new RequestPattern(GET, "/with/body"); requestPattern.setBodyPatterns( asList(ValuePattern.matches(".*<important>Value</important>.*"))); Request request = aRequest(context).withUrl("/with/body").withMethod(GET).withBody(XML_SAMPLE).build(); assertTrue(requestPattern.isMatchedBy(request)); }
@Test public void supportsMatchingOnAbsentHeader() { ignoringNotifier(); RequestPattern requestPattern = new RequestPattern(GET, "/without/header"); requestPattern.addHeader("X-My-Header", ValuePattern.absent()); Request request = aRequest(context) .withUrl("/without/header") .withMethod(GET) .withHeader("X-Another-Header", "value") .build(); assertTrue( "Request is not a match for the request pattern", requestPattern.isMatchedBy(request)); }
@Test public void shouldFailMatchWhenRequiredAbsentHeaderIsPresent() { ignoringNotifier(); RequestPattern requestPattern = new RequestPattern(GET, "/without/header/fail"); requestPattern.addHeader("X-My-Header", ValuePattern.absent()); Request request = aRequest(context) .withUrl("/without/header/fail") .withMethod(GET) .withHeader("X-My-Header", "value") .build(); assertFalse( "Request is a match for the request pattern and should not be", requestPattern.isMatchedBy(request)); }
@Test public void shouldNotMatchWhenBodyDoesNotMatchPattern() { ignoringNotifier(); RequestPattern requestPattern = new RequestPattern(GET, "/with/body"); requestPattern.setBodyPatterns( asList(ValuePattern.matches(".*<important>Value</important>.*"))); Request request = aRequest(context) .withUrl("/with/body") .withMethod(GET) .withBody("<important>Wrong value</important>") .build(); assertFalse(requestPattern.isMatchedBy(request)); }
@Test public void shouldLogMessageIndicatingFailedBodyMatch() { context.checking( new Expectations() { { one(notifier).info("URL /for/logging is match, but body is not: Actual Content"); } }); RequestPattern requestPattern = new RequestPattern(POST, "/for/logging"); requestPattern.setBodyPatterns(asList(ValuePattern.matches("Expected content"))); Request request = aRequest(context) .withUrl("/for/logging") .withMethod(POST) .withBody("Actual Content") .build(); requestPattern.isMatchedBy(request); }
private ValuePattern parameterDoesNotMatch(String value) { ValuePattern parameterPattern = new ValuePattern(); parameterPattern.setDoesNotMatch(value); return parameterPattern; }
private ValuePattern parameterMatches(String value) { ValuePattern parameterPattern = new ValuePattern(); parameterPattern.setMatches(value); return parameterPattern; }
private ValuePattern parameterEqualTo(String value) { ValuePattern parameterPattern = new ValuePattern(); parameterPattern.setEqualTo(value); return parameterPattern; }
private ValuePattern headerDoesNotMatch(String value) { ValuePattern headerPattern = new ValuePattern(); headerPattern.setDoesNotMatch(value); return headerPattern; }
private ValuePattern headerMatches(String value) { ValuePattern headerPattern = new ValuePattern(); headerPattern.setMatches(value); return headerPattern; }
private ValuePattern headerEqualTo(String value) { ValuePattern headerPattern = new ValuePattern(); headerPattern.setEqualTo(value); return headerPattern; }