001/*
002 * Copyright 2002-2016 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.mock.http.client;
018
019import java.io.IOException;
020import java.net.URI;
021
022import org.springframework.http.HttpMethod;
023import org.springframework.http.client.ClientHttpRequest;
024import org.springframework.http.client.ClientHttpResponse;
025import org.springframework.mock.http.MockHttpOutputMessage;
026
027/**
028 * Mock implementation of {@link ClientHttpRequest}.
029 *
030 * @author Rossen Stoyanchev
031 * @author Sam Brannen
032 * @since 3.2
033 */
034public class MockClientHttpRequest extends MockHttpOutputMessage implements ClientHttpRequest {
035
036        private HttpMethod httpMethod;
037
038        private URI uri;
039
040        private ClientHttpResponse clientHttpResponse;
041
042        private boolean executed = false;
043
044
045        /**
046         * Default constructor.
047         */
048        public MockClientHttpRequest() {
049        }
050
051        /**
052         * Create an instance with the given HttpMethod and URI.
053         */
054        public MockClientHttpRequest(HttpMethod httpMethod, URI uri) {
055                this.httpMethod = httpMethod;
056                this.uri = uri;
057        }
058
059
060        public void setMethod(HttpMethod httpMethod) {
061                this.httpMethod = httpMethod;
062        }
063
064        @Override
065        public HttpMethod getMethod() {
066                return this.httpMethod;
067        }
068
069        public void setURI(URI uri) {
070                this.uri = uri;
071        }
072
073        @Override
074        public URI getURI() {
075                return this.uri;
076        }
077
078        public void setResponse(ClientHttpResponse clientHttpResponse) {
079                this.clientHttpResponse = clientHttpResponse;
080        }
081
082        public boolean isExecuted() {
083                return this.executed;
084        }
085
086        /**
087         * Set the {@link #isExecuted() executed} flag to {@code true} and return the
088         * configured {@link #setResponse(ClientHttpResponse) response}.
089         * @see #executeInternal()
090         */
091        @Override
092        public final ClientHttpResponse execute() throws IOException {
093                this.executed = true;
094                return executeInternal();
095        }
096
097        /**
098         * The default implementation returns the configured
099         * {@link #setResponse(ClientHttpResponse) response}.
100         * <p>Override this method to execute the request and provide a response,
101         * potentially different than the configured response.
102         */
103        protected ClientHttpResponse executeInternal() throws IOException {
104                return this.clientHttpResponse;
105        }
106
107
108        @Override
109        public String toString() {
110                StringBuilder sb = new StringBuilder();
111                if (this.httpMethod != null) {
112                        sb.append(this.httpMethod);
113                }
114                if (this.uri != null) {
115                        sb.append(" ").append(this.uri);
116                }
117                if (!getHeaders().isEmpty()) {
118                        sb.append(", headers: ").append(getHeaders());
119                }
120                if (sb.length() == 0) {
121                        sb.append("Not yet initialized");
122                }
123                return sb.toString();
124        }
125
126}