@Override public void match(ClientHttpRequest request) throws IOException { for (RequestMatcher matcher : getRequestMatchers()) { matcher.match(request); } }
@Test public void headerContainsWithMissingHeader() throws Exception { AssertionError error = assertThrows(AssertionError.class, () -> MockRestRequestMatchers.header("foo", containsString("baz")).match(this.request)); assertThat(error.getMessage(), containsString("but was null")); }
@Test public void headersWithMissingHeader() throws Exception { AssertionError error = assertThrows(AssertionError.class, () -> MockRestRequestMatchers.header("foo", "bar").match(this.request)); assertThat(error.getMessage(), containsString("but was null")); }
@Test public void headerMissing() throws Exception { AssertionError error = assertThrows(AssertionError.class, () -> MockRestRequestMatchers.header("foo", "bar").match(this.request)); assertThat(error.getMessage(), containsString("was null")); }
@Test public void queryParamContainsWithMissingValue() throws Exception { this.request.setURI(new URI("http://foo.com/a?foo=bar&foo=baz")); AssertionError error = assertThrows(AssertionError.class, () -> MockRestRequestMatchers.queryParam("foo", containsString("bx")).match(this.request)); assertThat(error.getMessage(), containsString("was \"bar\"")); }
@Test public void queryParamMissingValue() throws Exception { this.request.setURI(new URI("http://foo.com/a?foo=bar&foo=baz")); AssertionError error = assertThrows(AssertionError.class, () -> MockRestRequestMatchers.queryParam("foo", "bad").match(this.request)); assertThat(error.getMessage(), containsString("expected:<bad> but was:<bar>")); }
@Test public void requestToContains() throws Exception { this.request.setURI(new URI("http://foo.com/bar")); MockRestRequestMatchers.requestTo(containsString("bar")).match(this.request); }
@Test public void queryParamMissing() throws Exception { this.request.setURI(new URI("http://foo.com/a")); AssertionError error = assertThrows(AssertionError.class, () -> MockRestRequestMatchers.queryParam("foo", "bar").match(this.request)); assertThat(error.getMessage(), containsString("but was null")); }
@Test public void queryParamContains() throws Exception { this.request.setURI(new URI("http://foo.com/a?foo=bar&foo=baz")); MockRestRequestMatchers.queryParam("foo", containsString("ba")).match(this.request); }
@Test public void testFormData() throws Exception { String contentType = "application/x-www-form-urlencoded;charset=UTF-8"; String body = "name+1=value+1&name+2=value+A&name+2=value+B&name+3"; this.request.getHeaders().setContentType(MediaType.parseMediaType(contentType)); this.request.getBody().write(body.getBytes(StandardCharsets.UTF_8)); MultiValueMap<String, String> map = new LinkedMultiValueMap<>(); map.add("name 1", "value 1"); map.add("name 2", "value A"); map.add("name 2", "value B"); map.add("name 3", null); MockRestRequestMatchers.content().formData(map).match(this.request); }
@Test public void headerContainsWithMissingValue() throws Exception { this.request.getHeaders().put("foo", Arrays.asList("bar", "baz")); AssertionError error = assertThrows(AssertionError.class, () -> MockRestRequestMatchers.header("foo", containsString("bx")).match(this.request)); assertThat(error.getMessage(), containsString("was \"bar\"")); }
@Test(expected = AssertionError.class) public void testBytesNoMatch() throws Exception { this.request.getBody().write("test".getBytes()); MockRestRequestMatchers.content().bytes("Test".getBytes()).match(this.request); }
@Test public void methodNoMatch() throws Exception { this.request.setMethod(HttpMethod.POST); AssertionError error = assertThrows(AssertionError.class, () -> MockRestRequestMatchers.method(HttpMethod.GET).match(this.request)); assertThat(error.getMessage(), containsString("expected:<GET> but was:<POST>")); }
@Test // SPR-15819 public void requestToUriTemplate() throws Exception { this.request.setURI(new URI("http://foo.com/bar")); MockRestRequestMatchers.requestToUriTemplate("http://foo.com/{bar}", "bar").match(this.request); }
@Test public void headerMissingValue() throws Exception { this.request.getHeaders().put("foo", Arrays.asList("bar", "baz")); AssertionError error = assertThrows(AssertionError.class, () -> MockRestRequestMatchers.header("foo", "bad").match(this.request)); assertThat(error.getMessage(), containsString("expected:<bad> but was:<bar>")); }
@Test public void headersWithMissingValue() throws Exception { this.request.getHeaders().put("foo", Collections.singletonList("bar")); AssertionError error = assertThrows(AssertionError.class, () -> MockRestRequestMatchers.header("foo", "bar", "baz").match(this.request)); assertThat(error.getMessage(), containsString("to have at least <2> values")); }
@Test public void queryParam() throws Exception { this.request.setURI(new URI("http://foo.com/a?foo=bar&foo=baz")); MockRestRequestMatchers.queryParam("foo", "bar", "baz").match(this.request); }
@Test public void testJsonLenientMatch() throws Exception { String content = "{\n \"foo array\":[\"first\",\"second\"] , \"someExtraProperty\": \"which is allowed\" \n}"; this.request.getBody().write(content.getBytes()); MockRestRequestMatchers.content().json("{\n \"foo array\":[\"second\",\"first\"] \n}") .match(this.request); MockRestRequestMatchers.content().json("{\n \"foo array\":[\"second\",\"first\"] \n}", false) .match(this.request); }
@Test public void requestTo() throws Exception { this.request.setURI(new URI("http://foo.com/bar")); MockRestRequestMatchers.requestTo("http://foo.com/bar").match(this.request); }
@Test // SPR-14498 public void valueWithMatcherAndNumberConversion() throws Exception { new JsonPathRequestMatchers("$.num").value(equalTo(5.0f), Float.class).match(request); }