Search in sources :

Example 1 with HttpCookie

use of org.springframework.http.HttpCookie in project spring-framework by spring-projects.

the class HttpHandlerConnector method adaptRequest.

private ServerHttpRequest adaptRequest(MockClientHttpRequest request, Publisher<DataBuffer> body) {
    HttpMethod method = request.getMethod();
    URI uri = request.getURI();
    HttpHeaders headers = request.getHeaders();
    MultiValueMap<String, HttpCookie> cookies = request.getCookies();
    return MockServerHttpRequest.method(method, uri).headers(headers).cookies(cookies).body(body);
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) URI(java.net.URI) HttpCookie(org.springframework.http.HttpCookie) HttpMethod(org.springframework.http.HttpMethod)

Example 2 with HttpCookie

use of org.springframework.http.HttpCookie in project spring-framework by spring-projects.

the class HttpHandlerConnectorTests method adaptRequest.

@Test
public void adaptRequest() throws Exception {
    TestHttpHandler handler = new TestHttpHandler(response -> {
        response.setStatusCode(HttpStatus.OK);
        return response.setComplete();
    });
    new HttpHandlerConnector(handler).connect(HttpMethod.POST, URI.create("/custom-path"), request -> {
        request.getHeaders().put("custom-header", Arrays.asList("h0", "h1"));
        request.getCookies().add("custom-cookie", new HttpCookie("custom-cookie", "c0"));
        return request.writeWith(Mono.just(toDataBuffer("Custom body")));
    }).block(Duration.ofSeconds(5));
    MockServerHttpRequest request = (MockServerHttpRequest) handler.getSavedRequest();
    assertEquals(HttpMethod.POST, request.getMethod());
    assertEquals("/custom-path", request.getURI().toString());
    assertEquals(Arrays.asList("h0", "h1"), request.getHeaders().get("custom-header"));
    assertEquals(new HttpCookie("custom-cookie", "c0"), request.getCookies().getFirst("custom-cookie"));
    DataBuffer buffer = request.getBody().blockFirst(Duration.ZERO);
    assertEquals("Custom body", DataBufferTestUtils.dumpString(buffer, UTF_8));
}
Also used : Arrays(java.util.Arrays) DefaultDataBufferFactory(org.springframework.core.io.buffer.DefaultDataBufferFactory) ServerHttpResponse(org.springframework.http.server.reactive.ServerHttpResponse) MockServerHttpRequest(org.springframework.mock.http.server.reactive.MockServerHttpRequest) UTF_8(java.nio.charset.StandardCharsets.UTF_8) HttpMethod(org.springframework.http.HttpMethod) Test(org.junit.Test) Mono(reactor.core.publisher.Mono) DataBuffer(org.springframework.core.io.buffer.DataBuffer) Function(java.util.function.Function) ReactiveHttpOutputMessage(org.springframework.http.ReactiveHttpOutputMessage) ClientHttpResponse(org.springframework.http.client.reactive.ClientHttpResponse) HttpCookie(org.springframework.http.HttpCookie) HttpStatus(org.springframework.http.HttpStatus) DataBufferTestUtils(org.springframework.core.io.buffer.support.DataBufferTestUtils) HttpHandler(org.springframework.http.server.reactive.HttpHandler) Duration(java.time.Duration) URI(java.net.URI) Assert.assertEquals(org.junit.Assert.assertEquals) ResponseCookie(org.springframework.http.ResponseCookie) ServerHttpRequest(org.springframework.http.server.reactive.ServerHttpRequest) MockServerHttpRequest(org.springframework.mock.http.server.reactive.MockServerHttpRequest) HttpCookie(org.springframework.http.HttpCookie) DataBuffer(org.springframework.core.io.buffer.DataBuffer) Test(org.junit.Test)

Example 3 with HttpCookie

use of org.springframework.http.HttpCookie in project spring-framework by spring-projects.

the class RxNettyServerHttpRequest method initCookies.

@Override
protected MultiValueMap<String, HttpCookie> initCookies() {
    MultiValueMap<String, HttpCookie> cookies = new LinkedMultiValueMap<>();
    for (String name : this.request.getCookies().keySet()) {
        for (Cookie cookie : this.request.getCookies().get(name)) {
            HttpCookie httpCookie = new HttpCookie(name, cookie.value());
            cookies.add(name, httpCookie);
        }
    }
    return cookies;
}
Also used : Cookie(io.netty.handler.codec.http.cookie.Cookie) HttpCookie(org.springframework.http.HttpCookie) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) HttpCookie(org.springframework.http.HttpCookie)

Example 4 with HttpCookie

use of org.springframework.http.HttpCookie in project spring-framework by spring-projects.

the class ServletServerHttpRequest method initCookies.

@Override
protected MultiValueMap<String, HttpCookie> initCookies() {
    MultiValueMap<String, HttpCookie> httpCookies = new LinkedMultiValueMap<>();
    Cookie[] cookies;
    synchronized (this.cookieLock) {
        cookies = this.request.getCookies();
    }
    if (cookies != null) {
        for (Cookie cookie : cookies) {
            String name = cookie.getName();
            HttpCookie httpCookie = new HttpCookie(name, cookie.getValue());
            httpCookies.add(name, httpCookie);
        }
    }
    return httpCookies;
}
Also used : HttpCookie(org.springframework.http.HttpCookie) Cookie(javax.servlet.http.Cookie) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) HttpCookie(org.springframework.http.HttpCookie)

Example 5 with HttpCookie

use of org.springframework.http.HttpCookie in project spring-framework by spring-projects.

the class UndertowServerHttpRequest method initCookies.

@Override
protected MultiValueMap<String, HttpCookie> initCookies() {
    MultiValueMap<String, HttpCookie> cookies = new LinkedMultiValueMap<>();
    for (String name : this.exchange.getRequestCookies().keySet()) {
        Cookie cookie = this.exchange.getRequestCookies().get(name);
        HttpCookie httpCookie = new HttpCookie(name, cookie.getValue());
        cookies.add(name, httpCookie);
    }
    return cookies;
}
Also used : Cookie(io.undertow.server.handlers.Cookie) HttpCookie(org.springframework.http.HttpCookie) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) HttpCookie(org.springframework.http.HttpCookie)

Aggregations

HttpCookie (org.springframework.http.HttpCookie)10 Test (org.junit.Test)4 LinkedMultiValueMap (org.springframework.util.LinkedMultiValueMap)4 URI (java.net.URI)3 Cookie (io.netty.handler.codec.http.cookie.Cookie)2 HttpMethod (org.springframework.http.HttpMethod)2 MockServerWebExchange (org.springframework.mock.http.server.reactive.test.MockServerWebExchange)2 ServerWebExchange (org.springframework.web.server.ServerWebExchange)2 Cookie (io.undertow.server.handlers.Cookie)1 UTF_8 (java.nio.charset.StandardCharsets.UTF_8)1 Duration (java.time.Duration)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 List (java.util.List)1 Function (java.util.function.Function)1 Cookie (javax.servlet.http.Cookie)1 Assert.assertEquals (org.junit.Assert.assertEquals)1 DataBuffer (org.springframework.core.io.buffer.DataBuffer)1 DefaultDataBufferFactory (org.springframework.core.io.buffer.DefaultDataBufferFactory)1 DataBufferTestUtils (org.springframework.core.io.buffer.support.DataBufferTestUtils)1