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.test.web.client.response;
018
019import java.io.IOException;
020import java.io.InputStream;
021import java.net.URI;
022import java.nio.charset.StandardCharsets;
023
024import org.springframework.core.io.Resource;
025import org.springframework.http.HttpHeaders;
026import org.springframework.http.HttpStatus;
027import org.springframework.http.MediaType;
028import org.springframework.http.client.ClientHttpRequest;
029import org.springframework.http.client.ClientHttpResponse;
030import org.springframework.lang.Nullable;
031import org.springframework.mock.http.client.MockClientHttpResponse;
032import org.springframework.test.web.client.ResponseCreator;
033import org.springframework.util.Assert;
034
035/**
036 * A {@code ResponseCreator} with builder-style methods for adding response details.
037 *
038 * @author Rossen Stoyanchev
039 * @since 3.2
040 */
041public class DefaultResponseCreator implements ResponseCreator {
042
043        private HttpStatus statusCode;
044
045        private byte[] content = new byte[0];
046
047        @Nullable
048        private Resource contentResource;
049
050        private final HttpHeaders headers = new HttpHeaders();
051
052
053        /**
054         * Protected constructor.
055         * Use static factory methods in {@link MockRestResponseCreators}.
056         */
057        protected DefaultResponseCreator(HttpStatus statusCode) {
058                Assert.notNull(statusCode, "HttpStatus must not be null");
059                this.statusCode = statusCode;
060        }
061
062
063        /**
064         * Set the body as a UTF-8 String.
065         */
066        public DefaultResponseCreator body(String content) {
067                this.content = content.getBytes(StandardCharsets.UTF_8);
068                return this;
069        }
070
071        /**
072         * Set the body as a byte array.
073         */
074        public DefaultResponseCreator body(byte[] content) {
075                this.content = content;
076                return this;
077        }
078
079        /**
080         * Set the body as a {@link Resource}.
081         */
082        public DefaultResponseCreator body(Resource resource) {
083                this.contentResource = resource;
084                return this;
085        }
086
087        /**
088         * Set the {@code Content-Type} header.
089         */
090        public DefaultResponseCreator contentType(MediaType mediaType) {
091                this.headers.setContentType(mediaType);
092                return this;
093        }
094
095        /**
096         * Set the {@code Location} header.
097         */
098        public DefaultResponseCreator location(URI location) {
099                this.headers.setLocation(location);
100                return this;
101        }
102
103        /**
104         * Copy all given headers.
105         */
106        public DefaultResponseCreator headers(HttpHeaders headers) {
107                this.headers.putAll(headers);
108                return this;
109        }
110
111
112        @Override
113        public ClientHttpResponse createResponse(@Nullable ClientHttpRequest request) throws IOException {
114                MockClientHttpResponse response;
115                if (this.contentResource != null) {
116                        InputStream stream = this.contentResource.getInputStream();
117                        response = new MockClientHttpResponse(stream, this.statusCode);
118                }
119                else {
120                        response = new MockClientHttpResponse(this.content, this.statusCode);
121                }
122                response.getHeaders().putAll(this.headers);
123                return response;
124        }
125
126}