|
| 1 | +package org.baeldung.client; |
| 2 | + |
| 3 | +import okhttp3.Request; |
| 4 | +import okhttp3.RequestBody; |
| 5 | +import org.junit.Before; |
| 6 | +import org.junit.Test; |
| 7 | +import org.springframework.boot.test.web.client.TestRestTemplate; |
| 8 | +import org.springframework.boot.web.client.RestTemplateBuilder; |
| 9 | +import org.springframework.http.HttpHeaders; |
| 10 | +import org.springframework.http.HttpStatus; |
| 11 | +import org.springframework.http.MediaType; |
| 12 | +import org.springframework.http.ResponseEntity; |
| 13 | +import org.springframework.web.client.RestTemplate; |
| 14 | + |
| 15 | +import static org.baeldung.client.Consts.APPLICATION_PORT; |
| 16 | +import static org.hamcrest.CoreMatchers.equalTo; |
| 17 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 18 | +import static org.junit.Assert.assertTrue; |
| 19 | + |
| 20 | +public class TestRestTemplateBasicLiveTest { |
| 21 | + |
| 22 | + private RestTemplate restTemplate; |
| 23 | + private static final String FOO_RESOURCE_URL = "http://localhost:" + APPLICATION_PORT + "/spring-rest/myfoos"; |
| 24 | + private static final String URL_SECURED_BY_AUTHENTICATION = "http://browserspy.dk/password-ok.php"; |
| 25 | + private static final String BASE_URL = "http://localhost:" + APPLICATION_PORT + "/spring-rest"; |
| 26 | + |
| 27 | + @Before |
| 28 | + public void beforeTest() { |
| 29 | + restTemplate = new RestTemplate(); |
| 30 | + } |
| 31 | + |
| 32 | + // GET |
| 33 | + @Test |
| 34 | + public void givenTestRestTemplate_whenSendGetForEntity_thenStatusOk() { |
| 35 | + TestRestTemplate testRestTemplate = new TestRestTemplate(); |
| 36 | + ResponseEntity<String> response = testRestTemplate.getForEntity(FOO_RESOURCE_URL + "/1", String.class); |
| 37 | + assertThat(response.getStatusCode(), equalTo(HttpStatus.OK)); |
| 38 | + } |
| 39 | + |
| 40 | + @Test |
| 41 | + public void givenRestTemplateWrapper_whenSendGetForEntity_thenStatusOk() { |
| 42 | + TestRestTemplate testRestTemplate = new TestRestTemplate(restTemplate); |
| 43 | + ResponseEntity<String> response = testRestTemplate.getForEntity(FOO_RESOURCE_URL + "/1", String.class); |
| 44 | + assertThat(response.getStatusCode(), equalTo(HttpStatus.OK)); |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + public void givenRestTemplateBuilderWrapper_whenSendGetForEntity_thenStatusOk() { |
| 49 | + RestTemplateBuilder restTemplateBuilder = new RestTemplateBuilder(); |
| 50 | + restTemplateBuilder.build(); |
| 51 | + TestRestTemplate testRestTemplate = new TestRestTemplate(restTemplateBuilder); |
| 52 | + ResponseEntity<String> response = testRestTemplate.getForEntity(FOO_RESOURCE_URL + "/1", String.class); |
| 53 | + assertThat(response.getStatusCode(), equalTo(HttpStatus.OK)); |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + public void givenRestTemplateWrapperWithCredentials_whenSendGetForEntity_thenStatusOk() { |
| 58 | + TestRestTemplate testRestTemplate = new TestRestTemplate(restTemplate, "test", "test"); |
| 59 | + ResponseEntity<String> response = testRestTemplate.getForEntity(URL_SECURED_BY_AUTHENTICATION + "/1", |
| 60 | + String.class); |
| 61 | + assertThat(response.getStatusCode(), equalTo(HttpStatus.OK)); |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + public void givenTestRestTemplateWithCredentials_whenSendGetForEntity_thenStatusOk() { |
| 66 | + TestRestTemplate testRestTemplate = new TestRestTemplate("test", "test"); |
| 67 | + ResponseEntity<String> response = testRestTemplate.getForEntity(URL_SECURED_BY_AUTHENTICATION + "/1", |
| 68 | + String.class); |
| 69 | + assertThat(response.getStatusCode(), equalTo(HttpStatus.OK)); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + public void givenTestRestTemplateWithBasicAuth_whenSendGetForEntity_thenStatusOk() { |
| 74 | + TestRestTemplate testRestTemplate = new TestRestTemplate(); |
| 75 | + ResponseEntity<String> response = testRestTemplate.withBasicAuth("test", "test"). |
| 76 | + getForEntity(URL_SECURED_BY_AUTHENTICATION + "/1", String.class); |
| 77 | + assertThat(response.getStatusCode(), equalTo(HttpStatus.OK)); |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + public void givenTestRestTemplateWithCredentialsAndEnabledCookies_whenSendGetForEntity_thenStatusOk() { |
| 82 | + TestRestTemplate testRestTemplate = new TestRestTemplate("test", "test", TestRestTemplate. |
| 83 | + HttpClientOption.ENABLE_COOKIES); |
| 84 | + ResponseEntity<String> response = testRestTemplate.getForEntity(URL_SECURED_BY_AUTHENTICATION + "/1", |
| 85 | + String.class); |
| 86 | + assertThat(response.getStatusCode(), equalTo(HttpStatus.OK)); |
| 87 | + } |
| 88 | + |
| 89 | + // HEAD |
| 90 | + @Test |
| 91 | + public void givenFooService_whenCallHeadForHeaders_thenReceiveAllHeaders() { |
| 92 | + TestRestTemplate testRestTemplate = new TestRestTemplate(); |
| 93 | + final HttpHeaders httpHeaders = testRestTemplate.headForHeaders(FOO_RESOURCE_URL); |
| 94 | + assertTrue(httpHeaders.getContentType().includes(MediaType.APPLICATION_JSON)); |
| 95 | + } |
| 96 | + |
| 97 | + // POST |
| 98 | + @Test |
| 99 | + public void givenService_whenPostForObject_thenCreatedObjectIsReturned() { |
| 100 | + TestRestTemplate testRestTemplate = new TestRestTemplate("test", "test"); |
| 101 | + final RequestBody body = RequestBody.create(okhttp3.MediaType.parse("text/html; charset=utf-8"), |
| 102 | + "{\"id\":1,\"name\":\"Jim\"}"); |
| 103 | + final Request request = new Request.Builder().url(BASE_URL + "/users/detail").post(body).build(); |
| 104 | + Object response = testRestTemplate.postForObject(URL_SECURED_BY_AUTHENTICATION, request, String.class); |
| 105 | + assertTrue(response.toString().contains("Success")); |
| 106 | + } |
| 107 | + |
| 108 | + // PUT |
| 109 | + @Test |
| 110 | + public void givenService_whenPutForObject_thenCreatedObjectIsReturned() { |
| 111 | + TestRestTemplate testRestTemplate = new TestRestTemplate("test", "test"); |
| 112 | + final RequestBody body = RequestBody.create(okhttp3.MediaType.parse("text/html; charset=utf-8"), |
| 113 | + "{\"id\":1,\"name\":\"Jim\"}"); |
| 114 | + final Request request = new Request.Builder().url(BASE_URL + "/users/detail").post(body).build(); |
| 115 | + testRestTemplate.put(URL_SECURED_BY_AUTHENTICATION, request, String.class); |
| 116 | + } |
| 117 | + |
| 118 | +} |
0 commit comments