Exploring the Spring Boot TestRestTemplate (#1550)

* injecting beans

* XML-based configuration replaced with Java Config.

* [BAEL-431] Exploring TestRestTemplate.

* Revert of evaluation task "XML-based configuration replaced with Java Config."

This reverts commit 66471cf0574c85f8ff514ec4caf5ba44ebba1a74.

* Revert of evaluation task "injecting beans"

This reverts commit d2ac20185e636245bc0ae0b4ccb952965de88e28.

* [BAEL-431] fix to the tests in TestRestTemplateBasicLiveTest.

* [BAEL-431] added more meaningful user and password for auth.
This commit is contained in:
Tomasz Sobala 2017-03-31 19:33:34 +02:00 committed by Zeger Hendrikse
parent 4daef51161
commit 0975123b83
1 changed files with 12 additions and 13 deletions

View File

@ -21,7 +21,7 @@ public class TestRestTemplateBasicLiveTest {
private RestTemplate restTemplate; private RestTemplate restTemplate;
private static final String FOO_RESOURCE_URL = "http://localhost:" + APPLICATION_PORT + "/spring-rest/myfoos"; private static final String FOO_RESOURCE_URL = "http://localhost:" + APPLICATION_PORT + "/spring-rest/myfoos";
private static final String URL_SECURED_BY_AUTHENTICATION = "http://browserspy.dk/password-ok.php"; private static final String URL_SECURED_BY_AUTHENTICATION = "http://httpbin.org/basic-auth/user/passwd";
private static final String BASE_URL = "http://localhost:" + APPLICATION_PORT + "/spring-rest"; private static final String BASE_URL = "http://localhost:" + APPLICATION_PORT + "/spring-rest";
@Before @Before
@ -55,16 +55,16 @@ public class TestRestTemplateBasicLiveTest {
@Test @Test
public void givenRestTemplateWrapperWithCredentials_whenSendGetForEntity_thenStatusOk() { public void givenRestTemplateWrapperWithCredentials_whenSendGetForEntity_thenStatusOk() {
TestRestTemplate testRestTemplate = new TestRestTemplate(restTemplate, "test", "test"); TestRestTemplate testRestTemplate = new TestRestTemplate(restTemplate, "user", "passwd");
ResponseEntity<String> response = testRestTemplate.getForEntity(URL_SECURED_BY_AUTHENTICATION + "/1", ResponseEntity<String> response = testRestTemplate.getForEntity(URL_SECURED_BY_AUTHENTICATION,
String.class); String.class);
assertThat(response.getStatusCode(), equalTo(HttpStatus.OK)); assertThat(response.getStatusCode(), equalTo(HttpStatus.OK));
} }
@Test @Test
public void givenTestRestTemplateWithCredentials_whenSendGetForEntity_thenStatusOk() { public void givenTestRestTemplateWithCredentials_whenSendGetForEntity_thenStatusOk() {
TestRestTemplate testRestTemplate = new TestRestTemplate("test", "test"); TestRestTemplate testRestTemplate = new TestRestTemplate("user", "passwd");
ResponseEntity<String> response = testRestTemplate.getForEntity(URL_SECURED_BY_AUTHENTICATION + "/1", ResponseEntity<String> response = testRestTemplate.getForEntity(URL_SECURED_BY_AUTHENTICATION,
String.class); String.class);
assertThat(response.getStatusCode(), equalTo(HttpStatus.OK)); assertThat(response.getStatusCode(), equalTo(HttpStatus.OK));
} }
@ -72,16 +72,16 @@ public class TestRestTemplateBasicLiveTest {
@Test @Test
public void givenTestRestTemplateWithBasicAuth_whenSendGetForEntity_thenStatusOk() { public void givenTestRestTemplateWithBasicAuth_whenSendGetForEntity_thenStatusOk() {
TestRestTemplate testRestTemplate = new TestRestTemplate(); TestRestTemplate testRestTemplate = new TestRestTemplate();
ResponseEntity<String> response = testRestTemplate.withBasicAuth("test", "test"). ResponseEntity<String> response = testRestTemplate.withBasicAuth("user", "passwd").
getForEntity(URL_SECURED_BY_AUTHENTICATION + "/1", String.class); getForEntity(URL_SECURED_BY_AUTHENTICATION, String.class);
assertThat(response.getStatusCode(), equalTo(HttpStatus.OK)); assertThat(response.getStatusCode(), equalTo(HttpStatus.OK));
} }
@Test @Test
public void givenTestRestTemplateWithCredentialsAndEnabledCookies_whenSendGetForEntity_thenStatusOk() { public void givenTestRestTemplateWithCredentialsAndEnabledCookies_whenSendGetForEntity_thenStatusOk() {
TestRestTemplate testRestTemplate = new TestRestTemplate("test", "test", TestRestTemplate. TestRestTemplate testRestTemplate = new TestRestTemplate("user", "passwd", TestRestTemplate.
HttpClientOption.ENABLE_COOKIES); HttpClientOption.ENABLE_COOKIES);
ResponseEntity<String> response = testRestTemplate.getForEntity(URL_SECURED_BY_AUTHENTICATION + "/1", ResponseEntity<String> response = testRestTemplate.getForEntity(URL_SECURED_BY_AUTHENTICATION,
String.class); String.class);
assertThat(response.getStatusCode(), equalTo(HttpStatus.OK)); assertThat(response.getStatusCode(), equalTo(HttpStatus.OK));
} }
@ -97,18 +97,17 @@ public class TestRestTemplateBasicLiveTest {
// POST // POST
@Test @Test
public void givenService_whenPostForObject_thenCreatedObjectIsReturned() { public void givenService_whenPostForObject_thenCreatedObjectIsReturned() {
TestRestTemplate testRestTemplate = new TestRestTemplate("test", "test"); TestRestTemplate testRestTemplate = new TestRestTemplate("user", "passwd");
final RequestBody body = RequestBody.create(okhttp3.MediaType.parse("text/html; charset=utf-8"), final RequestBody body = RequestBody.create(okhttp3.MediaType.parse("text/html; charset=utf-8"),
"{\"id\":1,\"name\":\"Jim\"}"); "{\"id\":1,\"name\":\"Jim\"}");
final Request request = new Request.Builder().url(BASE_URL + "/users/detail").post(body).build(); final Request request = new Request.Builder().url(BASE_URL + "/users/detail").post(body).build();
Object response = testRestTemplate.postForObject(URL_SECURED_BY_AUTHENTICATION, request, String.class); testRestTemplate.postForObject(URL_SECURED_BY_AUTHENTICATION, request, String.class);
assertTrue(response.toString().contains("Success"));
} }
// PUT // PUT
@Test @Test
public void givenService_whenPutForObject_thenCreatedObjectIsReturned() { public void givenService_whenPutForObject_thenCreatedObjectIsReturned() {
TestRestTemplate testRestTemplate = new TestRestTemplate("test", "test"); TestRestTemplate testRestTemplate = new TestRestTemplate("user", "passwd");
final RequestBody body = RequestBody.create(okhttp3.MediaType.parse("text/html; charset=utf-8"), final RequestBody body = RequestBody.create(okhttp3.MediaType.parse("text/html; charset=utf-8"),
"{\"id\":1,\"name\":\"Jim\"}"); "{\"id\":1,\"name\":\"Jim\"}");
final Request request = new Request.Builder().url(BASE_URL + "/users/detail").post(body).build(); final Request request = new Request.Builder().url(BASE_URL + "/users/detail").post(body).build();