[BAEL-3311] LiveTests

This commit is contained in:
Philippe 2020-01-21 00:09:15 -03:00
parent 68dee74200
commit 69005500f1
3 changed files with 71 additions and 2 deletions

View File

@ -4,10 +4,10 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
@SpringBootApplication
public class CustomRoutersGatewayApplication {
public class CustomPredicatesApplication {
public static void main(String[] args) {
new SpringApplicationBuilder(CustomRoutersGatewayApplication.class)
new SpringApplicationBuilder(CustomPredicatesApplication.class)
.profiles("customroutes")
.run(args);
}

View File

@ -5,6 +5,7 @@ import static org.assertj.core.api.Assertions.assertThat;
import org.assertj.core.api.Condition;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.web.server.LocalServerPort;
@ -27,6 +28,7 @@ public class CustomFiltersLiveTest {
@LocalServerPort
String port;
@Autowired
private WebTestClient client;
@BeforeEach

View File

@ -0,0 +1,67 @@
package com.baeldung.springcloudgateway.custompredicates;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.*;
import java.net.URI;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;
import org.junit.Before;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.boot.web.server.LocalServerPort;
import org.springframework.http.HttpStatus;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.ActiveProfiles;
/**
* This test requires
*/
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
@ActiveProfiles("customroutes")
public class CustomPredicatesApplicationLiveTest {
@LocalServerPort
String serverPort;
@Autowired
private TestRestTemplate restTemplate;
@Test
void givenNormalCustomer_whenCallHeadersApi_thenResponseForNormalCustomer() throws JSONException {
String url = "http://localhost:" + serverPort + "/api/headers";
ResponseEntity<String> response = restTemplate.getForEntity(url, String.class);
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
JSONObject json = new JSONObject(response.getBody());
JSONObject headers = json.getJSONObject("headers");
assertThat(headers.getString("Goldencustomer")).isEqualTo("false");
}
@Test
void givenGoldenCustomer_whenCallHeadersApi_thenResponseForGoldenCustomer() throws JSONException {
String url = "http://localhost:" + serverPort + "/api/headers";
RequestEntity<Void> request = RequestEntity
.get(URI.create(url))
.header("Cookie", "customerId=baeldung")
.build();
ResponseEntity<String> response = restTemplate.exchange(request, String.class);
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
JSONObject json = new JSONObject(response.getBody());
JSONObject headers = json.getJSONObject("headers");
assertThat(headers.getString("Goldencustomer")).isEqualTo("true");
}
}