Search in sources :

Example 36 with MockServerHttpResponse

use of org.springframework.web.testfixture.http.server.reactive.MockServerHttpResponse in project spring-framework by spring-projects.

the class FilteringWebHandlerTests method handleErrorFromFilter.

@Test
public void handleErrorFromFilter() throws Exception {
    MockServerHttpRequest request = MockServerHttpRequest.get("/").build();
    MockServerHttpResponse response = new MockServerHttpResponse();
    TestExceptionHandler exceptionHandler = new TestExceptionHandler();
    WebHttpHandlerBuilder.webHandler(new StubWebHandler()).filter(new ExceptionFilter()).exceptionHandler(exceptionHandler).build().handle(request, response).block();
    assertThat(response.getStatusCode()).isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR);
    assertThat(exceptionHandler.ex).isNotNull();
    assertThat(exceptionHandler.ex.getMessage()).isEqualTo("boo");
}
Also used : MockServerHttpRequest(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest) MockServerHttpResponse(org.springframework.web.testfixture.http.server.reactive.MockServerHttpResponse) Test(org.junit.jupiter.api.Test)

Example 37 with MockServerHttpResponse

use of org.springframework.web.testfixture.http.server.reactive.MockServerHttpResponse in project spring-framework by spring-projects.

the class DefaultWebSessionManagerTests method setUp.

@BeforeEach
void setUp() throws Exception {
    given(this.createSession.save()).willReturn(Mono.empty());
    given(this.createSession.getId()).willReturn("create-session-id");
    given(this.updateSession.getId()).willReturn("update-session-id");
    given(this.sessionStore.createWebSession()).willReturn(Mono.just(this.createSession));
    given(this.sessionStore.retrieveSession(this.updateSession.getId())).willReturn(Mono.just(this.updateSession));
    this.sessionManager = new DefaultWebSessionManager();
    this.sessionManager.setSessionIdResolver(this.sessionIdResolver);
    this.sessionManager.setSessionStore(this.sessionStore);
    MockServerHttpRequest request = MockServerHttpRequest.get("/path").build();
    MockServerHttpResponse response = new MockServerHttpResponse();
    this.exchange = new DefaultServerWebExchange(request, response, this.sessionManager, ServerCodecConfigurer.create(), new AcceptHeaderLocaleContextResolver());
}
Also used : AcceptHeaderLocaleContextResolver(org.springframework.web.server.i18n.AcceptHeaderLocaleContextResolver) DefaultServerWebExchange(org.springframework.web.server.adapter.DefaultServerWebExchange) MockServerHttpRequest(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest) MockServerHttpResponse(org.springframework.web.testfixture.http.server.reactive.MockServerHttpResponse) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 38 with MockServerHttpResponse

use of org.springframework.web.testfixture.http.server.reactive.MockServerHttpResponse in project spring-framework by spring-projects.

the class ContextPathCompositeHandlerTests method matchWithNativeContextPath.

@Test
public void matchWithNativeContextPath() {
    MockServerHttpRequest request = MockServerHttpRequest.get("/yet/another/path").contextPath(// contextPath in underlying request
    "/yet").build();
    TestHttpHandler handler = new TestHttpHandler();
    Map<String, HttpHandler> map = Collections.singletonMap("/another/path", handler);
    new ContextPathCompositeHandler(map).handle(request, new MockServerHttpResponse());
    assertThat(handler.wasInvoked()).isTrue();
    assertThat(handler.getRequest().getPath().contextPath().value()).isEqualTo("/yet/another/path");
}
Also used : MockServerHttpRequest(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest) MockServerHttpResponse(org.springframework.web.testfixture.http.server.reactive.MockServerHttpResponse) Test(org.junit.jupiter.api.Test)

Example 39 with MockServerHttpResponse

use of org.springframework.web.testfixture.http.server.reactive.MockServerHttpResponse in project spring-framework by spring-projects.

the class BodyInsertersTests method ofObjectWithHints.

@Test
public void ofObjectWithHints() {
    User body = new User("foo", "bar");
    BodyInserter<User, ReactiveHttpOutputMessage> inserter = BodyInserters.fromValue(body);
    this.hints.put(JSON_VIEW_HINT, SafeToSerialize.class);
    MockServerHttpResponse response = new MockServerHttpResponse();
    Mono<Void> result = inserter.insert(response, this.context);
    StepVerifier.create(result).expectComplete().verify();
    StepVerifier.create(response.getBodyAsString()).expectNext("{\"username\":\"foo\"}").expectComplete().verify();
}
Also used : ReactiveHttpOutputMessage(org.springframework.http.ReactiveHttpOutputMessage) MockServerHttpResponse(org.springframework.web.testfixture.http.server.reactive.MockServerHttpResponse) Test(org.junit.jupiter.api.Test)

Example 40 with MockServerHttpResponse

use of org.springframework.web.testfixture.http.server.reactive.MockServerHttpResponse in project spring-framework by spring-projects.

the class BodyInsertersTests method ofDataBuffers.

@Test
public void ofDataBuffers() {
    byte[] bytes = "foo".getBytes(UTF_8);
    DefaultDataBuffer dataBuffer = DefaultDataBufferFactory.sharedInstance.wrap(ByteBuffer.wrap(bytes));
    Flux<DataBuffer> body = Flux.just(dataBuffer);
    BodyInserter<Flux<DataBuffer>, ReactiveHttpOutputMessage> inserter = BodyInserters.fromDataBuffers(body);
    MockServerHttpResponse response = new MockServerHttpResponse();
    Mono<Void> result = inserter.insert(response, this.context);
    StepVerifier.create(result).expectComplete().verify();
    StepVerifier.create(response.getBody()).expectNext(dataBuffer).expectComplete().verify();
}
Also used : DefaultDataBuffer(org.springframework.core.io.buffer.DefaultDataBuffer) ReactiveHttpOutputMessage(org.springframework.http.ReactiveHttpOutputMessage) Flux(reactor.core.publisher.Flux) MockServerHttpResponse(org.springframework.web.testfixture.http.server.reactive.MockServerHttpResponse) DefaultDataBuffer(org.springframework.core.io.buffer.DefaultDataBuffer) DataBuffer(org.springframework.core.io.buffer.DataBuffer) Test(org.junit.jupiter.api.Test)

Aggregations

MockServerHttpResponse (org.springframework.web.testfixture.http.server.reactive.MockServerHttpResponse)64 Test (org.junit.jupiter.api.Test)55 MockServerHttpRequest (org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest)28 MockServerWebExchange (org.springframework.web.testfixture.server.MockServerWebExchange)24 HttpHandler (org.springframework.http.server.reactive.HttpHandler)14 HttpHeaders (org.springframework.http.HttpHeaders)13 Flux (reactor.core.publisher.Flux)13 Mono (reactor.core.publisher.Mono)13 Collections (java.util.Collections)12 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)12 LinkedMultiValueMap (org.springframework.util.LinkedMultiValueMap)12 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)11 DataBuffer (org.springframework.core.io.buffer.DataBuffer)11 MultiValueMap (org.springframework.util.MultiValueMap)11 StandardCharsets (java.nio.charset.StandardCharsets)10 HttpStatus (org.springframework.http.HttpStatus)10 ResponseCookie (org.springframework.http.ResponseCookie)10 ServerWebExchange (org.springframework.web.server.ServerWebExchange)10 WebFilter (org.springframework.web.server.WebFilter)10 StepVerifier (reactor.test.StepVerifier)10