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