@SuppressWarnings("deprecation") @Override public ClientHttpResponse validateRequest(ClientHttpRequest request) throws IOException { RequestExpectation expectation = null; synchronized (this.requests) { if (this.requests.isEmpty()) { afterExpectationsDeclared(); } try { // Try this first for backwards compatibility ClientHttpResponse response = validateRequestInternal(request); if (response != null) { return response; } else { expectation = matchRequest(request); } } finally { this.requests.add(request); } } return expectation.createResponse(request); }
@Test public void hasRemainingCount() { RequestExpectation expectation = new DefaultRequestExpectation(twice(), requestTo("/foo")); expectation.andRespond(withSuccess()); expectation.incrementAndValidate(); assertTrue(expectation.hasRemainingCount()); expectation.incrementAndValidate(); assertFalse(expectation.hasRemainingCount()); }
@Test public void isSatisfied() { RequestExpectation expectation = new DefaultRequestExpectation(twice(), requestTo("/foo")); expectation.andRespond(withSuccess()); expectation.incrementAndValidate(); assertFalse(expectation.isSatisfied()); expectation.incrementAndValidate(); assertTrue(expectation.isSatisfied()); }
/** * Return a matching expectation, or {@code null} if none match. */ @Nullable public RequestExpectation findExpectation(ClientHttpRequest request) throws IOException { for (RequestExpectation expectation : this.expectations) { try { expectation.match(request); return expectation; } catch (AssertionError error) { // We're looking to find a match or return null.. } } return null; }
@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; }
private void updateInternal(RequestExpectation expectation) { if (expectation.hasRemainingCount()) { this.expectations.add(expectation); } else { this.expectations.remove(expectation); } }
@Override public void verify() { if (this.expectations.isEmpty()) { return; } int count = 0; for (RequestExpectation expectation : this.expectations) { if (!expectation.isSatisfied()) { count++; } } if (count > 0) { String message = "Further request(s) expected leaving " + count + " unsatisfied expectation(s).\n"; throw new AssertionError(message + getRequestDetails()); } }
@Test public void matchWithFailedExpectation() throws Exception { RequestExpectation expectation = new DefaultRequestExpectation(once(), requestTo("/foo")); expectation.andExpect(method(POST)); this.thrown.expectMessage("Unexpected HttpMethod expected:<POST> but was:<GET>"); expectation.match(createRequest(GET, "/foo")); }
/** * Invoke this for an expectation that has been matched. * <p>The count of the given expectation is incremented, then it is * either stored if remainingCount > 0 or removed otherwise. */ public void update(RequestExpectation expectation) { expectation.incrementAndValidate(); updateInternal(expectation); }
@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; }
private void updateInternal(RequestExpectation expectation) { if (expectation.hasRemainingCount()) { this.expectations.add(expectation); } else { this.expectations.remove(expectation); } }
@Override public void verify() { if (this.expectations.isEmpty()) { return; } int count = 0; for (RequestExpectation expectation : this.expectations) { if (!expectation.isSatisfied()) { count++; } } if (count > 0) { String message = "Further request(s) expected leaving " + count + " unsatisfied expectation(s).\n"; throw new AssertionError(message + getRequestDetails()); } }
/** * Invoke this for an expectation that has been matched. * <p>The count of the given expectation is incremented, then it is * either stored if remainingCount > 0 or removed otherwise. */ public void update(RequestExpectation expectation) { expectation.incrementAndValidate(); updateInternal(expectation); }
@Test public void match() throws Exception { RequestExpectation expectation = new DefaultRequestExpectation(once(), requestTo("/foo")); expectation.match(createRequest(GET, "/foo")); }
@SuppressWarnings("deprecation") @Override public ClientHttpResponse validateRequest(ClientHttpRequest request) throws IOException { RequestExpectation expectation = null; synchronized (this.requests) { if (this.requests.isEmpty()) { afterExpectationsDeclared(); } try { // Try this first for backwards compatibility ClientHttpResponse response = validateRequestInternal(request); if (response != null) { return response; } else { expectation = matchRequest(request); } } finally { this.requests.add(request); } } return expectation.createResponse(request); }
/** * Invoke this for an expectation that has been matched. * <p>The given expectation will either be stored if it has a remaining * count or it will be removed otherwise. */ public void update(RequestExpectation expectation) { if (expectation.hasRemainingCount()) { getExpectations().add(expectation); } else { getExpectations().remove(expectation); } }
@Override public void verify() { if (getExpectations().isEmpty()) { return; } int count = 0; for (RequestExpectation expectation : getExpectations()) { if (!expectation.isSatisfied()) { count++; } } if (count > 0) { String message = "Further request(s) expected leaving " + count + " unsatisfied expectation(s).\n"; throw new AssertionError(message + getRequestDetails()); } }
/** * Return a matching expectation, or {@code null} if none match. */ @Nullable public RequestExpectation findExpectation(ClientHttpRequest request) throws IOException { for (RequestExpectation expectation : this.expectations) { try { expectation.match(request); return expectation; } catch (AssertionError error) { // We're looking to find a match or return null.. } } return null; }
@Override public ClientHttpResponse validateRequestInternal(ClientHttpRequest request) throws IOException { RequestExpectation expectation = this.remainingExpectations.findExpectation(request); if (expectation != null) { ClientHttpResponse response = expectation.createResponse(request); this.remainingExpectations.update(expectation); return response; } throw createUnexpectedRequestError(request); }
/** * Return a matching expectation, or {@code null} if none match. */ public RequestExpectation findExpectation(ClientHttpRequest request) throws IOException { for (RequestExpectation expectation : getExpectations()) { try { expectation.match(request); return expectation; } catch (AssertionError error) { // We're looking to find a match or return null.. } } return null; }