|
1 | 1 | /*
|
2 |
| - * Copyright 2012-2016 the original author or authors. |
| 2 | + * Copyright 2012-2018 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
16 | 16 |
|
17 | 17 | package sample;
|
18 | 18 |
|
| 19 | +import java.net.URI; |
| 20 | + |
19 | 21 | import org.junit.Test;
|
20 | 22 |
|
21 | 23 | import org.springframework.boot.test.web.client.TestRestTemplate;
|
22 | 24 | import org.springframework.http.HttpStatus;
|
| 25 | +import org.springframework.http.MediaType; |
| 26 | +import org.springframework.http.RequestEntity; |
23 | 27 | import org.springframework.http.ResponseEntity;
|
24 | 28 |
|
25 | 29 | import static org.assertj.core.api.Assertions.assertThat;
|
|
29 | 33 | */
|
30 | 34 | public class SampleTomcatDeployApplicationIT {
|
31 | 35 |
|
| 36 | + private final TestRestTemplate rest = new TestRestTemplate(); |
| 37 | + |
32 | 38 | private int port = Integer.valueOf(System.getProperty("port"));
|
33 | 39 |
|
34 | 40 | @Test
|
35 | 41 | public void testHome() throws Exception {
|
36 | 42 | String url = "http://localhost:" + this.port + "/bootapp/";
|
37 |
| - ResponseEntity<String> entity = new TestRestTemplate().getForEntity(url, |
38 |
| - String.class); |
| 43 | + ResponseEntity<String> entity = this.rest.getForEntity(url, String.class); |
39 | 44 | assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
|
40 | 45 | assertThat(entity.getBody()).isEqualTo("Hello World");
|
41 | 46 | }
|
42 | 47 |
|
| 48 | + @Test |
| 49 | + public void errorFromExceptionForRequestAcceptingAnythingProducesAJsonResponse() |
| 50 | + throws Exception { |
| 51 | + assertThatErrorFromExceptionProducesExpectedResponse(MediaType.ALL, |
| 52 | + MediaType.APPLICATION_JSON); |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + public void errorFromExceptionForRequestAcceptingJsonProducesAJsonResponse() |
| 57 | + throws Exception { |
| 58 | + assertThatErrorFromExceptionProducesExpectedResponse(MediaType.APPLICATION_JSON, |
| 59 | + MediaType.APPLICATION_JSON); |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + public void errorFromExceptionForRequestAcceptingHtmlProducesAnHtmlResponse() |
| 64 | + throws Exception { |
| 65 | + assertThatErrorFromExceptionProducesExpectedResponse(MediaType.TEXT_HTML, |
| 66 | + MediaType.TEXT_HTML); |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + public void sendErrorForRequestAcceptingAnythingProducesAJsonResponse() |
| 71 | + throws Exception { |
| 72 | + assertThatSendErrorProducesExpectedResponse(MediaType.ALL, |
| 73 | + MediaType.APPLICATION_JSON); |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + public void sendErrorForRequestAcceptingJsonProducesAJsonResponse() throws Exception { |
| 78 | + assertThatSendErrorProducesExpectedResponse(MediaType.APPLICATION_JSON, |
| 79 | + MediaType.APPLICATION_JSON); |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + public void sendErrorForRequestAcceptingHtmlProducesAnHtmlResponse() |
| 84 | + throws Exception { |
| 85 | + assertThatSendErrorProducesExpectedResponse(MediaType.TEXT_HTML, |
| 86 | + MediaType.TEXT_HTML); |
| 87 | + } |
| 88 | + |
| 89 | + private void assertThatSendErrorProducesExpectedResponse(MediaType accept, |
| 90 | + MediaType contentType) { |
| 91 | + RequestEntity<Void> request = RequestEntity |
| 92 | + .get(URI.create("http://localhost:" + this.port + "/bootapp/send-error")) |
| 93 | + .accept(accept).build(); |
| 94 | + ResponseEntity<String> response = this.rest.exchange(request, String.class); |
| 95 | + assertThat(response.getStatusCode()).isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR); |
| 96 | + assertThat(contentType.isCompatibleWith(response.getHeaders().getContentType())) |
| 97 | + .as("%s is compatible with %s", contentType, |
| 98 | + response.getHeaders().getContentType()) |
| 99 | + .isTrue(); |
| 100 | + } |
| 101 | + |
| 102 | + private void assertThatErrorFromExceptionProducesExpectedResponse(MediaType accept, |
| 103 | + MediaType contentType) { |
| 104 | + RequestEntity<Void> request = RequestEntity |
| 105 | + .get(URI.create("http://localhost:" + this.port + "/bootapp/exception")) |
| 106 | + .accept(accept).build(); |
| 107 | + ResponseEntity<String> response = this.rest.exchange(request, String.class); |
| 108 | + assertThat(response.getStatusCode()).isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR); |
| 109 | + assertThat(contentType.isCompatibleWith(response.getHeaders().getContentType())) |
| 110 | + .as("%s is compatible with %s", contentType, |
| 111 | + response.getHeaders().getContentType()) |
| 112 | + .isTrue(); |
| 113 | + } |
| 114 | + |
43 | 115 | }
|
0 commit comments