org.springframework.test.web.reactive.server.WebTestClient.ResponseSpec Java Examples

The following examples show how to use org.springframework.test.web.reactive.server.WebTestClient.ResponseSpec. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MicroServiceTest.java    From Spring-5.0-Cookbook with MIT License 6 votes vote down vote up
@Test
public void testSaveEmps(){
	Employee newEmp = new Employee();
	newEmp.setEmpid(87978);
	newEmp.setAge(22);
	newEmp.setBirthday(new Date(72, 1,22));
	newEmp.setDeptid(362);
	newEmp.setEmail("[email protected]");
	newEmp.setFirstname("Keith");
	newEmp.setLastname("Smith");
	

	/*
	Mono<ClientResponse> resp = WebClient.create().post().uri("http://localhost:8098/saveEmp").accept(MediaType.APPLICATION_JSON)
             .body(BodyInserters.fromObject(newEmp)).exchange();
	*/
   	ResponseSpec resp = webTestClient.post().uri("http://localhost:9002/saveEmp").accept(MediaType.APPLICATION_JSON_UTF8)
			.body(BodyInserters.fromObject(newEmp)).exchange();
    
		
		
	System.out.println(resp);
	System.out.println("sherwin");
	
}
 
Example #2
Source File: TestReactService.java    From Spring-5.0-Cookbook with MIT License 6 votes vote down vote up
@Test
public void testSaveEmps(){
	Employee newEmp = new Employee();
	newEmp.setEmpid(87978);
	newEmp.setAge(22);
	newEmp.setBirthday(new Date(72, 1,22));
	newEmp.setDeptid(362);
	newEmp.setEmail("[email protected]");
	newEmp.setFirstname("Keith");
	newEmp.setLastname("Smith");

	/*
	Mono<ClientResponse> resp = WebClient.create().post().uri("http://localhost:8098/saveEmp").accept(MediaType.APPLICATION_JSON)
             .body(BodyInserters.fromObject(newEmp)).exchange();
	*/
   	ResponseSpec resp = webTestClient.post().uri("http://localhost:9002/saveEmp").accept(MediaType.APPLICATION_JSON_UTF8)
			.body(BodyInserters.fromObject(newEmp)).exchange();
    
		
		
	System.out.println(resp);
	System.out.println("sherwin");
	
}
 
Example #3
Source File: WebFilterFactoriesLiveTest.java    From tutorials with MIT License 6 votes vote down vote up
@Test
public void whenCallHeaderPostThroughGateway_thenAllHTTPResponseHeadersAreSet() {
    ResponseSpec response = client.post()
      .uri("/header/post")
      .exchange();

    response.expectStatus()
      .isOk()
      .expectHeader()
      .valueEquals("My-Header-Rewrite", "password=***")
      .expectHeader()
      .valueEquals("My-Header-Set", "Set")
      .expectHeader()
      .valueEquals("My-Header-Good", "Good")
      .expectHeader()
      .doesNotExist("My-Header-Remove");
}
 
Example #4
Source File: CustomFiltersLiveTest.java    From tutorials with MIT License 6 votes vote down vote up
@Test
public void givenRequestWithLocaleQueryParam_whenCallServiceThroughGateway_thenAllConfiguredFiltersGetExecuted() {
    ResponseSpec response = client.get()
        .uri("/service/resource?locale=en")
        .exchange();

    response.expectStatus()
        .isOk()
        .expectHeader()
        .exists("Bael-Custom-Language-Header")
        .expectBody(String.class)
        .isEqualTo("Service Resource");

    assertThat(LoggerListAppender.getEvents())
        // Modify Response
        .haveAtLeastOne(eventContains("Added custom header to Response"))
        .haveAtLeastOne(eventContainsExcept("Removed all query params: ", "locale"));
}
 
Example #5
Source File: CorsOnGlobalConfigLiveTest.java    From tutorials with MIT License 6 votes vote down vote up
@Test
public void whenPreflightAllowedDeleteEndpoint_thenObtainResponseWithCorsHeaders() {
    ResponseSpec response = client.options()
        .uri(BASE_EXTRA_CORS_CONFIG_URL + "/further-mixed-config-endpoint")
        .header("Access-Control-Request-Method", "DELETE")
        .header("Access-Control-Request-Headers", "Baeldung-Not-Allowed, Baeldung-Allowed, Baeldung-Other-Allowed")
        .exchange();

    response.expectHeader()
        .valueEquals("Access-Control-Allow-Origin", CORS_DEFAULT_ORIGIN);
    response.expectHeader()
        .valueEquals("Access-Control-Allow-Methods", "PUT,DELETE");
    response.expectHeader()
        .valueEquals("Access-Control-Allow-Headers", "Baeldung-Allowed, Baeldung-Other-Allowed");
    response.expectHeader()
        .exists("Access-Control-Max-Age");
}
 
Example #6
Source File: CorsOnWebFilterLiveTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void whenRequestDeleteEndpointWithExtraConfigs_thenObtainForbiddenResponse() {
    ResponseSpec response = client.delete()
        .uri(BASE_EXTRA_CORS_CONFIG_URL + "/further-mixed-config-endpoint")
        .exchange();

    response.expectStatus()
        .isForbidden();
}
 
Example #7
Source File: GreetingLiveTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void shouldGreet() {
    final String name = "Baeldung";

    ResponseSpec response = webTestClient.get()
                                         .uri("/greet/{name}", name)
                                         .exchange();

    response.expectStatus()
            .isOk()
            .expectBody(String.class)
            .isEqualTo("Greeting Baeldung");
}
 
Example #8
Source File: CorsOnWebFilterLiveTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void whenPreflightFunctionalEndpoint_thenObtain404Response() {
    ResponseSpec response = client.options()
        .uri(BASE_FUNCTIONALS_URL + "/functional-endpoint")
        .header("Access-Control-Request-Method", "PUT")
        .exchange();

    response.expectHeader()
        .valueEquals("Access-Control-Allow-Origin", CORS_DEFAULT_ORIGIN);
    response.expectHeader()
        .valueEquals("Access-Control-Allow-Methods", "PUT");
    response.expectHeader()
        .valueEquals("Access-Control-Max-Age", "8000");
}
 
Example #9
Source File: CorsOnWebFilterLiveTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void whenRequestFunctionalEndpoint_thenObtainResponseWithCorsHeaders() {
    ResponseSpec response = client.put()
        .uri(BASE_FUNCTIONALS_URL + "/functional-endpoint")
        .exchange();

    response.expectHeader()
        .valueEquals("Access-Control-Allow-Origin", CORS_DEFAULT_ORIGIN);
}
 
Example #10
Source File: CorsOnGlobalConfigLiveTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void whenRequestingRegularEndpoint_thenObtainResponseWithCorsHeaders() {
    ResponseSpec response = client.put()
        .uri(BASE_REGULAR_URL + "/regular-put-endpoint")
        .exchange();

    response.expectHeader()
        .valueEquals("Access-Control-Allow-Origin", CORS_DEFAULT_ORIGIN);
}
 
Example #11
Source File: CorsOnGlobalConfigLiveTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void whenRequestingRegularDeleteEndpoint_thenObtainForbiddenResponse() {
    ResponseSpec response = client.delete()
        .uri(BASE_REGULAR_URL + "/regular-delete-endpoint")
        .exchange();

    response.expectStatus()
        .isForbidden();
}
 
Example #12
Source File: CorsOnGlobalConfigLiveTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void whenRequestAllowedDeleteEndpoint_thenObtainResponseWithCorsHeaders() {
    ResponseSpec response = client.delete()
        .uri(BASE_EXTRA_CORS_CONFIG_URL + "/further-mixed-config-endpoint")
        .exchange();

    response.expectStatus()
        .isOk();
}
 
Example #13
Source File: CorsOnGlobalConfigLiveTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void whenPreflightFunctionalEndpoint_thenObtain404Response() {
    ResponseSpec response = client.options()
        .uri(BASE_FUNCTIONALS_URL + "/cors-disabled-functional-endpoint")
        .header("Access-Control-Request-Method", "PUT")
        .exchange();

    response.expectStatus()
        .isNotFound();
}
 
Example #14
Source File: CorsOnGlobalConfigLiveTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void whenRequestFunctionalEndpoint_thenObtainResponseWithCorsHeaders() {
    ResponseSpec response = client.put()
        .uri(BASE_FUNCTIONALS_URL + "/cors-disabled-functional-endpoint")
        .exchange();

    response.expectHeader()
        .valueEquals("Access-Control-Allow-Origin", CORS_DEFAULT_ORIGIN);
}
 
Example #15
Source File: CorsOnAnnotatedElementsLiveTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void whenRequestingMethodCorsEnabledEndpoint_thenObtainResponseWithCorsHeaders() {
    ResponseSpec response = client.put()
        .uri(BASE_CORS_ON_METHODS_URL + "/cors-enabled-endpoint")
        .exchange();

    response.expectHeader()
        .valueEquals("Access-Control-Allow-Origin", "*");
}
 
Example #16
Source File: CorsOnAnnotatedElementsLiveTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void whenPreflightMethodCorsEnabled_thenObtainResponseWithCorsHeaders() {
    ResponseSpec response = client.options()
        .uri(BASE_CORS_ON_METHODS_URL + "/cors-enabled-endpoint")
        .header("Access-Control-Request-Method", "PUT")
        .exchange();

    response.expectHeader()
        .valueEquals("Access-Control-Allow-Origin", "*");
    response.expectHeader()
        .valueEquals("Access-Control-Allow-Methods", "PUT");
    response.expectHeader()
        .exists("Access-Control-Max-Age");
}
 
Example #17
Source File: CorsOnAnnotatedElementsLiveTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void whenRequestingMethodCorsDisabledEndpoint_thenObtainResponseWithoutCorsHeaders() {
    ResponseSpec response = client.put()
        .uri(BASE_CORS_ON_METHODS_URL + "/cors-disabled-put-endpoint")
        .exchange();

    response.expectHeader()
        .doesNotExist("Access-Control-Allow-Origin");
}
 
Example #18
Source File: CorsOnAnnotatedElementsLiveTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void whenRequestingMethodCorsRestrictiveOrigin_thenObtainForbiddenResponse() {
    ResponseSpec response = client.put()
        .uri(BASE_CORS_ON_METHODS_URL + "/cors-enabled-origin-restrictive-endpoint")
        .exchange();

    response.expectStatus()
        .isForbidden();
}
 
Example #19
Source File: CorsOnAnnotatedElementsLiveTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void whenPreflightMethodCorsRestrictiveOrigin_thenObtainForbiddenResponse() {
    ResponseSpec response = client.options()
        .uri(BASE_CORS_ON_METHODS_URL + "/cors-enabled-origin-restrictive-endpoint")
        .header("Access-Control-Request-Method", "PUT")
        .exchange();

    response.expectStatus()
        .isForbidden();
}
 
Example #20
Source File: CorsOnAnnotatedElementsLiveTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void whenPreflightMethodCorsRestrictiveHeader_thenObtainResponseWithAllowedHeaders() {
    ResponseSpec response = client.options()
        .uri(BASE_CORS_ON_METHODS_URL + "/cors-enabled-header-restrictive-endpoint")
        .header("Access-Control-Request-Method", "PUT")
        .header("Access-Control-Request-Headers", "Baeldung-Not-Allowed, Baeldung-Allowed")
        .exchange();

    response.expectHeader()
        .valueEquals("Access-Control-Allow-Headers", "Baeldung-Allowed");
}
 
Example #21
Source File: CorsOnAnnotatedElementsLiveTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void whenPreflightControllerCorsRegularEndpoint_thenObtainResponseWithCorsHeaders() {
    ResponseSpec response = client.options()
        .uri(BASE_CORS_ON_CONTROLLER_URL + "/regular-endpoint")
        .header("Origin", CONTROLLER_CORS_ALLOWED_ORIGIN)
        .header("Access-Control-Request-Method", "PUT")
        .exchange();

    response.expectHeader()
        .valueEquals("Access-Control-Allow-Origin", CONTROLLER_CORS_ALLOWED_ORIGIN);
}
 
Example #22
Source File: CorsOnAnnotatedElementsLiveTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void whenPreflightControllerCorsRestrictiveOrigin_thenObtainResponseWithCorsHeaders() {
    ResponseSpec response = client.options()
        .uri(BASE_CORS_ON_CONTROLLER_URL + "/cors-enabled-origin-restrictive-endpoint")
        .header("Origin", CONTROLLER_CORS_ALLOWED_ORIGIN)
        .header("Access-Control-Request-Method", "PUT")
        .exchange();

    response.expectHeader()
        .valueEquals("Access-Control-Allow-Origin", CONTROLLER_CORS_ALLOWED_ORIGIN);
}
 
Example #23
Source File: CorsOnAnnotatedElementsLiveTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void whenPreflightControllerCorsRestrictiveOriginWithAnotherAllowedOrigin_thenObtainResponseWithCorsHeaders() {
    final String anotherAllowedOrigin = "http://another-allowed-origin.com";
    ResponseSpec response = client.options()
        .uri(BASE_CORS_ON_CONTROLLER_URL + "/cors-enabled-origin-restrictive-endpoint")
        .header("Origin", anotherAllowedOrigin)
        .header("Access-Control-Request-Method", "PUT")
        .exchange();

    response.expectHeader()
        .valueEquals("Access-Control-Allow-Origin", anotherAllowedOrigin);
}
 
Example #24
Source File: CorsOnAnnotatedElementsLiveTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void whenPreflightControllerCorsExposingHeaders_thenObtainResponseWithCorsHeaders() {
    ResponseSpec response = client.options()
        .uri(BASE_CORS_ON_CONTROLLER_URL + "/cors-enabled-exposed-header-endpoint")
        .header("Origin", CONTROLLER_CORS_ALLOWED_ORIGIN)
        .header("Access-Control-Request-Method", "PUT")
        .exchange();

    response.expectHeader()
        .valueEquals("Access-Control-Expose-Headers", "Baeldung-Exposed");
}
 
Example #25
Source File: FunctionalEndpointValidationsLiveTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void whenRequestingDryEPWithValidBody_thenObtainBadRequest() {
    CustomRequestEntity body = new CustomRequestEntity("name", "1234567");

    ResponseSpec response = client.post()
        .uri(DRY_EP_URL)
        .body(Mono.just(body), CustomRequestEntity.class)
        .exchange();

    response.expectStatus()
        .isOk();
}
 
Example #26
Source File: SSLHandshakeTimeoutTests.java    From spring-cloud-gateway with Apache License 2.0 5 votes vote down vote up
@Test
@Override // here we validate that it the handshake times out
public void testSslTrust() {
	ResponseSpec responseSpec = testClient.get().uri("/ssltrust").exchange();
	responseSpec.expectStatus().is5xxServerError();
	JsonPathAssertions jsonPath = responseSpec.expectBody().jsonPath("message");
	jsonPath.isEqualTo("handshake timed out after 1ms");
}
 
Example #27
Source File: AbstractBasePathWebApplicationIntegrationTest.java    From spring-cloud-open-service-broker with Apache License 2.0 5 votes vote down vote up
private ResponseSpec createExchange(String baseUri) {
	String request = JsonUtils.toJson(CreateServiceInstanceRequest.builder()
			.serviceDefinitionId("default-service")
			.planId("default-plan")
			.build()
	);

	return client.put()
			.uri(format("%s/v2/service_instances/default-service", baseUri))
			.accept(MediaType.APPLICATION_JSON)
			.contentType(MediaType.APPLICATION_JSON)
			.bodyValue(request)
			.exchange();
}
 
Example #28
Source File: OAuth2ManualRequestLiveTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void whenRequestingDebugHookOn_thenObtainExpectedMessage() {
    ResponseSpec response = client.get()
        .uri(MANUAL_REQUEST_URI)
        .exchange();
    
    response.expectStatus()
        .isOk()
        .expectBody(String.class)
        .value(Matchers.containsString("Retrieved the resource using a manual approach: {\"id\""));
}
 
Example #29
Source File: WebFilterFactoriesLiveTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void whenCallDeleteThroughGateway_thenIsUnauthorizedCodeIsSet() {
    ResponseSpec response = client.delete()
      .uri("/delete")
      .exchange();

    response.expectStatus()
      .isUnauthorized();
}
 
Example #30
Source File: WebFilterFactoriesLiveTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void whenCallFakePostThroughGateway_thenIsUnauthorizedCodeIsSet() {
    ResponseSpec response = client.post()
      .uri("/fake/post")
      .exchange();

    response.expectStatus()
      .is3xxRedirection();
}