View Javadoc
1   /*
2    * Copyright 2002-2015 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.springframework.http;
18  
19  import java.net.URI;
20  import java.net.URISyntaxException;
21  import java.util.List;
22  
23  import org.junit.Test;
24  
25  import static org.junit.Assert.*;
26  
27  /**
28   * @author Arjen Poutsma
29   * @author Marcel Overdijk
30   * @author Kazuki Shimizu
31   */
32  public class ResponseEntityTests {
33  
34  	@Test
35  	public void normal() {
36  		String headerName = "My-Custom-Header";
37  		String headerValue1 = "HeaderValue1";
38  		String headerValue2 = "HeaderValue2";
39  		Integer entity = 42;
40  
41  		ResponseEntity<Integer> responseEntity =
42  				ResponseEntity.status(HttpStatus.OK).header(headerName, headerValue1, headerValue2).body(entity);
43  
44  		assertNotNull(responseEntity);
45  		assertEquals(HttpStatus.OK, responseEntity.getStatusCode());
46  		assertTrue(responseEntity.getHeaders().containsKey(headerName));
47  		List<String> list = responseEntity.getHeaders().get(headerName);
48  		assertEquals(2, list.size());
49  		assertEquals(headerValue1, list.get(0));
50  		assertEquals(headerValue2, list.get(1));
51  		assertEquals(entity, responseEntity.getBody());
52  	}
53  
54  	@Test
55  	public void okNoBody() {
56  		ResponseEntity<Void> responseEntity = ResponseEntity.ok().build();
57  
58  		assertNotNull(responseEntity);
59  		assertEquals(HttpStatus.OK, responseEntity.getStatusCode());
60  		assertNull(responseEntity.getBody());
61  	}
62  
63  	@Test
64  	public void okEntity() {
65  		Integer entity = 42;
66  		ResponseEntity<Integer> responseEntity = ResponseEntity.ok(entity);
67  
68  		assertNotNull(responseEntity);
69  		assertEquals(HttpStatus.OK, responseEntity.getStatusCode());
70  		assertEquals(entity, responseEntity.getBody());
71  	}
72  
73  	@Test
74  	public void createdLocation() throws URISyntaxException {
75  		URI location = new URI("location");
76  		ResponseEntity<Void> responseEntity = ResponseEntity.created(location).build();
77  
78  		assertNotNull(responseEntity);
79  		assertEquals(HttpStatus.CREATED, responseEntity.getStatusCode());
80  		assertTrue(responseEntity.getHeaders().containsKey("Location"));
81  		assertEquals(location.toString(),
82  				responseEntity.getHeaders().getFirst("Location"));
83  		assertNull(responseEntity.getBody());
84  
85  		ResponseEntity.created(location).header("MyResponseHeader", "MyValue").body("Hello World");
86  	}
87  
88  	@Test
89  	public void acceptedNoBody() throws URISyntaxException {
90  		ResponseEntity<Void> responseEntity = ResponseEntity.accepted().build();
91  
92  		assertNotNull(responseEntity);
93  		assertEquals(HttpStatus.ACCEPTED, responseEntity.getStatusCode());
94  		assertNull(responseEntity.getBody());
95  	}
96  
97  	@Test
98  	public void noContent() throws URISyntaxException {
99  		ResponseEntity<Void> responseEntity = ResponseEntity.noContent().build();
100 
101 		assertNotNull(responseEntity);
102 		assertEquals(HttpStatus.NO_CONTENT, responseEntity.getStatusCode());
103 		assertNull(responseEntity.getBody());
104 	}
105 
106 	@Test
107 	public void badRequest() throws URISyntaxException {
108 		ResponseEntity<Void> responseEntity = ResponseEntity.badRequest().build();
109 
110 		assertNotNull(responseEntity);
111 		assertEquals(HttpStatus.BAD_REQUEST, responseEntity.getStatusCode());
112 		assertNull(responseEntity.getBody());
113 	}
114 
115 	@Test
116 	public void notFound() throws URISyntaxException {
117 		ResponseEntity<Void> responseEntity = ResponseEntity.notFound().build();
118 
119 		assertNotNull(responseEntity);
120 		assertEquals(HttpStatus.NOT_FOUND, responseEntity.getStatusCode());
121 		assertNull(responseEntity.getBody());
122 	}
123 
124 	@Test
125 	public void unprocessableEntity() throws URISyntaxException {
126 		ResponseEntity<String> responseEntity = ResponseEntity.unprocessableEntity().body("error");
127 
128 		assertNotNull(responseEntity);
129 		assertEquals(HttpStatus.UNPROCESSABLE_ENTITY, responseEntity.getStatusCode());
130 		assertEquals("error", responseEntity.getBody());
131 	}
132 
133 	@Test
134 	public void headers() throws URISyntaxException {
135 		String eTag = "\"foo\"";
136 		URI location = new URI("location");
137 		long contentLength = 67890;
138 		MediaType contentType = MediaType.TEXT_PLAIN;
139 
140 		ResponseEntity<Void> responseEntity = ResponseEntity.ok().
141 				allow(HttpMethod.GET).
142 				eTag(eTag).
143 				lastModified(12345L).
144 				location(location).
145 				contentLength(contentLength).
146 				contentType(contentType).
147 				build();
148 
149 		assertNotNull(responseEntity);
150 		assertEquals(HttpStatus.OK, responseEntity.getStatusCode());
151 		HttpHeaders responseHeaders = responseEntity.getHeaders();
152 
153 		assertEquals("GET", responseHeaders.getFirst("Allow"));
154 		assertEquals(eTag, responseHeaders.getFirst("ETag"));
155 		assertEquals("Thu, 01 Jan 1970 00:00:12 GMT",
156 				responseHeaders.getFirst("Last-Modified"));
157 		assertEquals(location.toASCIIString(),
158 				responseHeaders.getFirst("Location"));
159 		assertEquals(String.valueOf(contentLength), responseHeaders.getFirst("Content-Length"));
160 		assertEquals(contentType.toString(), responseHeaders.getFirst("Content-Type"));
161 
162 		assertNull(responseEntity.getBody());
163 	}
164 
165 	@Test
166 	public void headersCopy(){
167 		HttpHeaders customHeaders = new HttpHeaders();
168 		customHeaders.set("X-CustomHeader", "vale");
169 
170 		ResponseEntity<Void> responseEntity = ResponseEntity.ok().headers(customHeaders).build();
171 		HttpHeaders responseHeaders = responseEntity.getHeaders();
172 
173 		assertEquals(HttpStatus.OK, responseEntity.getStatusCode());
174 		assertEquals(1, responseHeaders.size());
175 		assertEquals(1, responseHeaders.get("X-CustomHeader").size());
176 		assertEquals("vale", responseHeaders.getFirst("X-CustomHeader"));
177 
178 	}
179 
180 	@Test  // SPR-12792
181 	public void headersCopyWithEmptyAndNull(){
182 		ResponseEntity<Void> responseEntityWithEmptyHeaders =
183 				ResponseEntity.ok().headers(new HttpHeaders()).build();
184 		ResponseEntity<Void> responseEntityWithNullHeaders =
185 				ResponseEntity.ok().headers(null).build();
186 
187 		assertEquals(HttpStatus.OK, responseEntityWithEmptyHeaders.getStatusCode());
188 		assertTrue(responseEntityWithEmptyHeaders.getHeaders().isEmpty());
189 		assertEquals(responseEntityWithEmptyHeaders.toString(), responseEntityWithNullHeaders.toString());
190 	}
191 
192 }