001/* 002 * Copyright 2002-2018 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.test.web.client; 018 019import java.io.IOException; 020 021import org.springframework.http.client.ClientHttpRequest; 022import org.springframework.http.client.ClientHttpResponse; 023 024/** 025 * Encapsulates the behavior required to implement {@link MockRestServiceServer} 026 * including its public API (create expectations + verify/reset) along with an 027 * extra method for verifying actual requests. 028 * 029 * <p>This contract is not used directly in applications but a custom 030 * implementation can be 031 * {@link org.springframework.test.web.client.MockRestServiceServer.MockRestServiceServerBuilder#build(RequestExpectationManager) 032 * plugged} in through the {@code MockRestServiceServer} builder. 033 * 034 * @author Rossen Stoyanchev 035 * @since 4.3 036 */ 037public interface RequestExpectationManager { 038 039 /** 040 * Set up a new request expectation. The returned {@link ResponseActions} is 041 * used to add more expectations and define a response. 042 * <p>This is a delegate for 043 * {@link MockRestServiceServer#expect(ExpectedCount, RequestMatcher)}. 044 * 045 * @param requestMatcher a request expectation 046 * @return for setting up further expectations and define a response 047 * @see MockRestServiceServer#expect(RequestMatcher) 048 * @see MockRestServiceServer#expect(ExpectedCount, RequestMatcher) 049 */ 050 ResponseActions expectRequest(ExpectedCount count, RequestMatcher requestMatcher); 051 052 /** 053 * Verify that all expectations have been met. 054 * <p>This is a delegate for {@link MockRestServiceServer#verify()}. 055 * @throws AssertionError when some expectations were not met 056 * @see MockRestServiceServer#verify() 057 */ 058 void verify(); 059 060 /** 061 * Reset the internal state removing all expectations and recorded requests. 062 * <p>This is a delegate for {@link MockRestServiceServer#reset()}. 063 * @see MockRestServiceServer#reset() 064 */ 065 void reset(); 066 067 068 /** 069 * Validate the given actual request against the declared expectations. 070 * Is successful return the mock response to use or raise an error. 071 * <p>This is used in {@link MockRestServiceServer} against actual requests. 072 * @param request the request 073 * @return the response to return if the request was validated. 074 * @throws AssertionError when some expectations were not met 075 * @throws IOException in case of any validation errors 076 */ 077 ClientHttpResponse validateRequest(ClientHttpRequest request) throws IOException; 078 079}