@Override public MockRestServiceServer build() { if (this.ignoreExpectOrder) { return build(new UnorderedRequestExpectationManager()); } else { return build(new SimpleRequestExpectationManager()); } }
@Override protected RequestExpectation matchRequest(ClientHttpRequest request) throws IOException { RequestExpectation expectation = this.repeatExpectations.findExpectation(request); if (expectation == null) { if (this.expectationIterator == null || !this.expectationIterator.hasNext()) { throw createUnexpectedRequestError(request); } expectation = this.expectationIterator.next(); expectation.match(request); } this.repeatExpectations.update(expectation); return expectation; }
@Override protected void afterExpectationsDeclared() { Assert.state(this.expectationIterator == null, "Expectations already declared"); this.expectationIterator = getExpectations().iterator(); }
@Test public void repeatedRequests() throws Exception { this.manager.expectRequest(times(3), requestTo("/foo")).andExpect(method(GET)).andRespond(withSuccess()); this.manager.expectRequest(times(3), requestTo("/bar")).andExpect(method(GET)).andRespond(withSuccess()); this.manager.validateRequest(createRequest(GET, "/foo")); this.manager.validateRequest(createRequest(GET, "/bar")); this.manager.validateRequest(createRequest(GET, "/foo")); this.manager.validateRequest(createRequest(GET, "/bar")); this.manager.validateRequest(createRequest(GET, "/foo")); this.manager.validateRequest(createRequest(GET, "/bar")); this.manager.verify(); }
@Test // SPR-15719 public void repeatedRequestsInSequentialOrder() throws Exception { this.manager.expectRequest(times(2), requestTo("/foo")).andExpect(method(GET)).andRespond(withSuccess()); this.manager.expectRequest(times(2), requestTo("/bar")).andExpect(method(GET)).andRespond(withSuccess()); this.manager.validateRequest(createRequest(GET, "/foo")); this.manager.validateRequest(createRequest(GET, "/foo")); this.manager.validateRequest(createRequest(GET, "/bar")); this.manager.validateRequest(createRequest(GET, "/bar")); }
@Test public void zeroExpectedRequests() throws Exception { this.manager.verify(); }
@Test public void unexpectedRequest() throws Exception { try { this.manager.validateRequest(createRequest(GET, "/foo")); } catch (AssertionError error) { assertEquals("No further requests expected: HTTP GET /foo\n" + "0 request(s) executed.\n", error.getMessage()); } }
@Test public void sequentialRequests() throws Exception { this.manager.expectRequest(once(), requestTo("/foo")).andExpect(method(GET)).andRespond(withSuccess()); this.manager.expectRequest(once(), requestTo("/bar")).andExpect(method(GET)).andRespond(withSuccess()); this.manager.validateRequest(createRequest(GET, "/foo")); this.manager.validateRequest(createRequest(GET, "/bar")); this.manager.verify(); }
@Test public void repeatedRequestsTooMany() throws Exception { this.manager.expectRequest(max(2), requestTo("/foo")).andExpect(method(GET)).andRespond(withSuccess()); this.manager.expectRequest(max(2), requestTo("/bar")).andExpect(method(GET)).andRespond(withSuccess()); this.thrown.expectMessage("No further requests expected: HTTP GET /foo\n" + "4 request(s) executed:\n" + "GET /foo\n" + "GET /bar\n" + "GET /foo\n" + "GET /bar\n"); this.manager.validateRequest(createRequest(GET, "/foo")); this.manager.validateRequest(createRequest(GET, "/bar")); this.manager.validateRequest(createRequest(GET, "/foo")); this.manager.validateRequest(createRequest(GET, "/bar")); this.manager.validateRequest(createRequest(GET, "/foo")); }
@Test public void repeatedRequestsTooFew() throws Exception { this.manager.expectRequest(min(2), requestTo("/foo")).andExpect(method(GET)).andRespond(withSuccess()); this.manager.expectRequest(min(2), requestTo("/bar")).andExpect(method(GET)).andRespond(withSuccess()); this.thrown.expectMessage("3 request(s) executed:\n" + "GET /foo\n" + "GET /bar\n" + "GET /foo\n"); this.manager.validateRequest(createRequest(GET, "/foo")); this.manager.validateRequest(createRequest(GET, "/bar")); this.manager.validateRequest(createRequest(GET, "/foo")); this.manager.verify(); }
/** * Return a bound {@link MockRestServiceServer} for the given {@link RestTemplate}, * configured with {@link RootUriRequestExpectationManager} when possible. * @param restTemplate the source REST template * @return a configured {@link MockRestServiceServer} */ public static MockRestServiceServer bindTo(RestTemplate restTemplate) { return bindTo(restTemplate, new SimpleRequestExpectationManager()); }
@Test // SPR-15672 public void sequentialRequestsWithDifferentCount() throws Exception { this.manager.expectRequest(times(2), requestTo("/foo")).andExpect(method(GET)).andRespond(withSuccess()); this.manager.expectRequest(once(), requestTo("/bar")).andExpect(method(GET)).andRespond(withSuccess()); this.manager.validateRequest(createRequest(GET, "/foo")); this.manager.validateRequest(createRequest(GET, "/foo")); this.manager.validateRequest(createRequest(GET, "/bar")); }
@Override protected void afterExpectationsDeclared() { Assert.state(this.expectationIterator == null, "Expectations already declared"); this.expectationIterator = getExpectations().iterator(); }
@Override protected RequestExpectation matchRequest(ClientHttpRequest request) throws IOException { RequestExpectation expectation = this.repeatExpectations.findExpectation(request); if (expectation == null) { if (this.expectationIterator == null || !this.expectationIterator.hasNext()) { throw createUnexpectedRequestError(request); } expectation = this.expectationIterator.next(); expectation.match(request); } this.repeatExpectations.update(expectation); return expectation; }
@Test // SPR-16132 public void sequentialRequestsWithFirstFailing() throws Exception { this.manager.expectRequest(once(), requestTo("/foo")). andExpect(method(GET)).andRespond(request -> { throw new SocketException("pseudo network error"); }); this.manager.expectRequest(once(), requestTo("/handle-error")). andExpect(method(POST)).andRespond(withSuccess()); try { this.manager.validateRequest(createRequest(GET, "/foo")); fail("Expected SocketException"); } catch (SocketException ex) { // expected } this.manager.validateRequest(createRequest(POST, "/handle-error")); this.manager.verify(); }
@Override public MockRestServiceServer build() { if (this.ignoreExpectOrder) { return build(new UnorderedRequestExpectationManager()); } else { return build(new SimpleRequestExpectationManager()); } }
@Test public void sequentialRequestsTooMany() throws Exception { this.manager.expectRequest(max(1), requestTo("/foo")).andExpect(method(GET)).andRespond(withSuccess()); this.manager.expectRequest(max(1), requestTo("/bar")).andExpect(method(GET)).andRespond(withSuccess()); this.thrown.expectMessage("No further requests expected: HTTP GET /baz\n" + "2 request(s) executed:\n" + "GET /foo\n" + "GET /bar\n"); this.manager.validateRequest(createRequest(GET, "/foo")); this.manager.validateRequest(createRequest(GET, "/bar")); this.manager.validateRequest(createRequest(GET, "/baz")); }
@Override protected void afterExpectationsDeclared() { Assert.state(this.expectationIterator == null, "Expectations already declared"); this.expectationIterator = getExpectations().iterator(); }
@Override public ClientHttpResponse validateRequestInternal(ClientHttpRequest request) throws IOException { RequestExpectation expectation = this.repeatExpectations.findExpectation(request); if (expectation == null) { if (!this.expectationIterator.hasNext()) { throw createUnexpectedRequestError(request); } expectation = this.expectationIterator.next(); expectation.match(request); } ClientHttpResponse response = expectation.createResponse(request); this.repeatExpectations.update(expectation); return response; }
@Test public void sequentialRequestsTooFew() throws Exception { this.manager.expectRequest(min(1), requestTo("/foo")).andExpect(method(GET)).andRespond(withSuccess()); this.manager.expectRequest(min(1), requestTo("/bar")).andExpect(method(GET)).andRespond(withSuccess()); this.thrown.expectMessage("Further request(s) expected leaving 1 unsatisfied expectation(s).\n" + "1 request(s) executed:\nGET /foo\n"); this.manager.validateRequest(createRequest(GET, "/foo")); this.manager.verify(); }