更新requests框架

This commit is contained in:
eson 2019-06-04 00:51:43 +08:00
parent 818a03ecd9
commit 9bdd142742
2 changed files with 21 additions and 46 deletions

View File

@ -19,6 +19,8 @@ import org.springframework.http.ResponseEntity;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.reactive.server.WebTestClient;
import org.springframework.util.Assert;
import org.springframework.web.reactive.function.client.ClientResponse;
import org.springframework.web.reactive.function.client.WebClient;
import ocean.gateway.service.ServiceApplication;
import ocean.gateway.service.filters.TokenFilter;
@ -32,36 +34,34 @@ public class TokenFilterTests {
@LocalServerPort
int randomServerPort;
@Autowired
private TestRestTemplate restTemplate ;
// private WebTestClient requests;
String baseUri;
WebClient request;
@Before
public void before() {
request = WebClient.create();
baseUri = "http://127.0.0.1:" + randomServerPort;
}
@Before
public void setup() {
baseUri = "http://localhost:" + randomServerPort;
}
@Test
public void GetHelloWithoutToken() {
ResponseEntity<String> response = restTemplate.getForEntity(baseUri +"/hello", String.class);
ClientResponse response = request.get().uri(baseUri + "/hello").exchange().block();
Assert.notNull(response, "The class must not be null");
Assert.state(response.getStatusCode() == HttpStatus.UNAUTHORIZED, "response.getStatusCode() must be HttpStatus.UNAUTHORIZED");
Assert.state(response.statusCode() == HttpStatus.UNAUTHORIZED, "response.getStatusCode() must be HttpStatus.UNAUTHORIZED");
}
@Test
public void GetHelloWithToken() {
HttpHeaders headers = new HttpHeaders();
String key = TokenFilter.TEST_TOKEN;
headers.add("token",key);
HttpEntity<String> entity = new HttpEntity<>("", headers);
ClientResponse response = request.get().uri(baseUri + "/hello").header("token", TokenFilter.TEST_TOKEN).exchange().block();
ResponseEntity<String> response = restTemplate.exchange(baseUri + "/hello", HttpMethod.GET, entity, String.class);
Assert.notNull(response, "The class must not be null");
Assert.state(response.getStatusCode() != HttpStatus.UNAUTHORIZED, "response.getStatusCode() must not be HttpStatus.UNAUTHORIZED");
Assert.state(response.statusCode() != HttpStatus.UNAUTHORIZED, "response.getStatusCode() must not be HttpStatus.UNAUTHORIZED");
}
}

View File

@ -39,26 +39,19 @@ import static com.github.tomakehurst.wiremock.client.WireMock.*;
@SpringBootTest(classes = { ServiceApplication.class }, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class BusinessTests {
// @RestController
// @RequestMapping(value = "/my")
// public class MyController {
// @RequestMapping(value = "/integration/{name}", method = RequestMethod.GET)
// public String integrationTest(@PathVariable String name) {
// return "name:" + name;
// }
// }
@LocalServerPort
public int randomServerPort;
public String baseUri;
@Rule
public WireMockRule routeTestServer = new WireMockRule(3030);
WebClient request;
@Before
public void before() {
request = WebClient.create();
baseUri = "http://127.0.0.1:" + randomServerPort;
routeTestServer.stubFor(get(urlPathEqualTo("/hello")).willReturn(
aResponse().withStatus(200).withBody("hello route")));
}
@ -71,35 +64,17 @@ public class BusinessTests {
@Test
public void TestRouteLocator() {
WebClient cli = WebClient.create();
ClientResponse response = cli.get().uri(baseUri + "/hello").header("token", TokenFilter.TEST_TOKEN).exchange().block();
ClientResponse response = request.get().uri(baseUri + "/hello").header("token", TokenFilter.TEST_TOKEN).exchange().block();
String content = response.bodyToMono(String.class).block();
Assert.hasText(content, "hello route");
Assert.notNull(response, "The class must not be null");
Assert.state(response.statusCode() != HttpStatus.UNAUTHORIZED, "response.getStatusCode() must not be HttpStatus.UNAUTHORIZED");
Assert.state(response.statusCode() == HttpStatus.OK, String.format("the statusCode is %s, not is %s\n", response.statusCode(), HttpStatus.OK));
Assert.hasText(content, "hello route");
}
// @Configuration
// public class HttpServerConfig {
// @Autowired
// private Environment environment;
// private String serverPort;
// @Bean
// public HttpServer httpServer() {
// HttpServer server = HttpServer.create().host("http://localhost:" + serverPort);
// server.handle((req, res) -> res.sendString(Flux.just("HelloTest")));
// return server;
// }
// }
}