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