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; 022 023/** 024 * {@code RequestExpectationManager} that matches requests to expectations 025 * regardless of the order of declaration of expected requests. 026 * 027 * @author Rossen Stoyanchev 028 * @since 4.3 029 */ 030public class UnorderedRequestExpectationManager extends AbstractRequestExpectationManager { 031 032 private final RequestExpectationGroup remainingExpectations = new RequestExpectationGroup(); 033 034 035 @Override 036 protected void afterExpectationsDeclared() { 037 this.remainingExpectations.addAllExpectations(getExpectations()); 038 } 039 040 @Override 041 public RequestExpectation matchRequest(ClientHttpRequest request) throws IOException { 042 RequestExpectation expectation = this.remainingExpectations.findExpectation(request); 043 if (expectation == null) { 044 throw createUnexpectedRequestError(request); 045 } 046 this.remainingExpectations.update(expectation); 047 return expectation; 048 } 049 050 @Override 051 public void reset() { 052 super.reset(); 053 this.remainingExpectations.reset(); 054 } 055 056}