Search in sources :

Example 16 with ServerHttpRequest

use of org.springframework.http.server.reactive.ServerHttpRequest in project spring-framework by spring-projects.

the class BodyExtractorsTests method toFormData.

@Test
public void toFormData() throws Exception {
    BodyExtractor<Mono<MultiValueMap<String, String>>, ServerHttpRequest> extractor = BodyExtractors.toFormData();
    DefaultDataBufferFactory factory = new DefaultDataBufferFactory();
    DefaultDataBuffer dataBuffer = factory.wrap(ByteBuffer.wrap("name+1=value+1&name+2=value+2%2B1&name+2=value+2%2B2&name+3".getBytes(StandardCharsets.UTF_8)));
    Flux<DataBuffer> body = Flux.just(dataBuffer);
    MockServerHttpRequest request = MockServerHttpRequest.post("/").contentType(MediaType.APPLICATION_FORM_URLENCODED).body(body);
    Mono<MultiValueMap<String, String>> result = extractor.extract(request, this.context);
    StepVerifier.create(result).consumeNextWith(form -> {
        assertEquals("Invalid result", 3, form.size());
        assertEquals("Invalid result", "value 1", form.getFirst("name 1"));
        List<String> values = form.get("name 2");
        assertEquals("Invalid result", 2, values.size());
        assertEquals("Invalid result", "value 2+1", values.get(0));
        assertEquals("Invalid result", "value 2+2", values.get(1));
        assertNull("Invalid result", form.getFirst("name 3"));
    }).expectComplete().verify();
}
Also used : DefaultDataBuffer(org.springframework.core.io.buffer.DefaultDataBuffer) MockServerHttpRequest(org.springframework.mock.http.server.reactive.test.MockServerHttpRequest) DefaultDataBufferFactory(org.springframework.core.io.buffer.DefaultDataBufferFactory) Mono(reactor.core.publisher.Mono) ServerHttpRequest(org.springframework.http.server.reactive.ServerHttpRequest) MockServerHttpRequest(org.springframework.mock.http.server.reactive.test.MockServerHttpRequest) MultiValueMap(org.springframework.util.MultiValueMap) DefaultDataBuffer(org.springframework.core.io.buffer.DefaultDataBuffer) DataBuffer(org.springframework.core.io.buffer.DataBuffer) Test(org.junit.Test)

Example 17 with ServerHttpRequest

use of org.springframework.http.server.reactive.ServerHttpRequest in project spring-framework by spring-projects.

the class DefaultCorsProcessor method processRequest.

@Override
public boolean processRequest(CorsConfiguration config, ServerWebExchange exchange) {
    ServerHttpRequest request = exchange.getRequest();
    ServerHttpResponse response = exchange.getResponse();
    if (!CorsUtils.isCorsRequest(request)) {
        return true;
    }
    if (responseHasCors(response)) {
        logger.debug("Skip CORS: response already contains \"Access-Control-Allow-Origin\" header");
        return true;
    }
    if (CorsUtils.isSameOrigin(request)) {
        logger.debug("Skip CORS: request is from same origin");
        return true;
    }
    boolean preFlightRequest = CorsUtils.isPreFlightRequest(request);
    if (config == null) {
        if (preFlightRequest) {
            rejectRequest(response);
            return false;
        } else {
            return true;
        }
    }
    return handleInternal(exchange, config, preFlightRequest);
}
Also used : ServerHttpRequest(org.springframework.http.server.reactive.ServerHttpRequest) ServerHttpResponse(org.springframework.http.server.reactive.ServerHttpResponse)

Example 18 with ServerHttpRequest

use of org.springframework.http.server.reactive.ServerHttpRequest in project spring-framework by spring-projects.

the class HttpHandlerConnector method connect.

@Override
public Mono<ClientHttpResponse> connect(HttpMethod httpMethod, URI uri, Function<? super ClientHttpRequest, Mono<Void>> requestCallback) {
    MonoProcessor<ClientHttpResponse> result = MonoProcessor.create();
    MockClientHttpRequest mockClientRequest = new MockClientHttpRequest(httpMethod, uri);
    MockServerHttpResponse mockServerResponse = new MockServerHttpResponse();
    mockClientRequest.setWriteHandler(requestBody -> {
        log("Invoking HttpHandler for ", httpMethod, uri);
        ServerHttpRequest mockServerRequest = adaptRequest(mockClientRequest, requestBody);
        this.handler.handle(mockServerRequest, mockServerResponse).subscribe(aVoid -> {
        }, result::onError);
        return Mono.empty();
    });
    mockServerResponse.setWriteHandler(responseBody -> {
        log("Creating client response for ", httpMethod, uri);
        result.onNext(adaptResponse(mockServerResponse, responseBody));
        return Mono.empty();
    });
    log("Writing client request for ", httpMethod, uri);
    requestCallback.apply(mockClientRequest).subscribe(aVoid -> {
    }, result::onError);
    return result;
}
Also used : ServerHttpRequest(org.springframework.http.server.reactive.ServerHttpRequest) MockServerHttpRequest(org.springframework.mock.http.server.reactive.MockServerHttpRequest) MockClientHttpRequest(org.springframework.mock.http.client.reactive.MockClientHttpRequest) ClientHttpResponse(org.springframework.http.client.reactive.ClientHttpResponse) MockClientHttpResponse(org.springframework.mock.http.client.reactive.MockClientHttpResponse) MockServerHttpResponse(org.springframework.mock.http.server.reactive.MockServerHttpResponse)

Aggregations

ServerHttpRequest (org.springframework.http.server.reactive.ServerHttpRequest)18 HandshakeInfo (org.springframework.web.reactive.socket.HandshakeInfo)7 ServerHttpResponse (org.springframework.http.server.reactive.ServerHttpResponse)6 Principal (java.security.Principal)5 ServletServerHttpRequest (org.springframework.http.server.reactive.ServletServerHttpRequest)4 DataBufferFactory (org.springframework.core.io.buffer.DataBufferFactory)3 HttpHeaders (org.springframework.http.HttpHeaders)3 HttpMethod (org.springframework.http.HttpMethod)3 Mono (reactor.core.publisher.Mono)3 IOException (java.io.IOException)2 Collections (java.util.Collections)2 List (java.util.List)2 Map (java.util.Map)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 HttpServletResponse (javax.servlet.http.HttpServletResponse)2 ReactiveAdapter (org.springframework.core.ReactiveAdapter)2 ReactiveAdapterRegistry (org.springframework.core.ReactiveAdapterRegistry)2 ResolvableType (org.springframework.core.ResolvableType)2 MediaType (org.springframework.http.MediaType)2 ServletServerHttpResponse (org.springframework.http.server.reactive.ServletServerHttpResponse)2