Search in sources :

Example 41 with HttpResponse

use of io.netty.handler.codec.http.HttpResponse in project netty by netty.

the class CorsHandlerTest method preflightRequestWithNullOrigin.

@Test
public void preflightRequestWithNullOrigin() {
    final String origin = "null";
    final CorsConfig config = forOrigin(origin).allowNullOrigin().allowCredentials().build();
    final HttpResponse response = preflightRequest(config, origin, "content-type, xheader1");
    assertThat(response.headers().get(ACCESS_CONTROL_ALLOW_ORIGIN), is(equalTo("null")));
    assertThat(response.headers().get(ACCESS_CONTROL_ALLOW_CREDENTIALS), is(equalTo("true")));
}
Also used : DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) HttpResponse(io.netty.handler.codec.http.HttpResponse) AsciiString(io.netty.util.AsciiString) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Test(org.junit.Test)

Example 42 with HttpResponse

use of io.netty.handler.codec.http.HttpResponse in project netty by netty.

the class CorsHandlerTest method preflightRequestAllowCredentials.

@Test
public void preflightRequestAllowCredentials() {
    final String origin = "null";
    final CorsConfig config = forOrigin(origin).allowCredentials().build();
    final HttpResponse response = preflightRequest(config, origin, "content-type, xheader1");
    assertThat(response.headers().get(ACCESS_CONTROL_ALLOW_CREDENTIALS), is(equalTo("true")));
}
Also used : DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) HttpResponse(io.netty.handler.codec.http.HttpResponse) AsciiString(io.netty.util.AsciiString) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Test(org.junit.Test)

Example 43 with HttpResponse

use of io.netty.handler.codec.http.HttpResponse in project netty by netty.

the class CorsHandlerTest method preflightGetRequestWithCustomHeaders.

@Test
public void preflightGetRequestWithCustomHeaders() {
    final CorsConfig config = forOrigin("http://localhost:8888").allowedRequestMethods(OPTIONS, GET, DELETE).allowedRequestHeaders("content-type", "xheader1").build();
    final HttpResponse response = preflightRequest(config, "http://localhost:8888", "content-type, xheader1");
    assertThat(response.headers().get(ACCESS_CONTROL_ALLOW_ORIGIN), is("http://localhost:8888"));
    assertThat(response.headers().get(ACCESS_CONTROL_ALLOW_METHODS), containsString("OPTIONS"));
    assertThat(response.headers().get(ACCESS_CONTROL_ALLOW_METHODS), containsString("GET"));
    assertThat(response.headers().get(ACCESS_CONTROL_ALLOW_HEADERS), containsString("content-type"));
    assertThat(response.headers().get(ACCESS_CONTROL_ALLOW_HEADERS), containsString("xheader1"));
    assertThat(response.headers().get(VARY), equalTo(ORIGIN.toString()));
}
Also used : DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) HttpResponse(io.netty.handler.codec.http.HttpResponse) Test(org.junit.Test)

Example 44 with HttpResponse

use of io.netty.handler.codec.http.HttpResponse in project netty by netty.

the class CorsHandlerTest method simpleRequest.

private static HttpResponse simpleRequest(final CorsConfig config, final String origin, final String requestHeaders, final HttpMethod method) {
    final EmbeddedChannel channel = new EmbeddedChannel(new CorsHandler(config), new EchoHandler());
    final FullHttpRequest httpRequest = createHttpRequest(method);
    if (origin != null) {
        httpRequest.headers().set(ORIGIN, origin);
    }
    if (requestHeaders != null) {
        httpRequest.headers().set(ACCESS_CONTROL_REQUEST_HEADERS, requestHeaders);
    }
    assertThat(channel.writeInbound(httpRequest), is(false));
    return (HttpResponse) channel.readOutbound();
}
Also used : DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) HttpResponse(io.netty.handler.codec.http.HttpResponse)

Example 45 with HttpResponse

use of io.netty.handler.codec.http.HttpResponse in project netty by netty.

the class CorsHandlerTest method preflightRequestWithValueGenerator.

@Test
public void preflightRequestWithValueGenerator() {
    final CorsConfig config = forOrigin("http://localhost:8888").preflightResponseHeader("GenHeader", new Callable<String>() {

        @Override
        public String call() throws Exception {
            return "generatedValue";
        }
    }).build();
    final HttpResponse response = preflightRequest(config, "http://localhost:8888", "content-type, xheader1");
    assertThat(response.headers().get(of("GenHeader")), equalTo("generatedValue"));
    assertThat(response.headers().get(VARY), equalTo(ORIGIN.toString()));
}
Also used : DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) HttpResponse(io.netty.handler.codec.http.HttpResponse) Callable(java.util.concurrent.Callable) Test(org.junit.Test)

Aggregations

HttpResponse (io.netty.handler.codec.http.HttpResponse)127 DefaultFullHttpResponse (io.netty.handler.codec.http.DefaultFullHttpResponse)57 Test (org.junit.Test)50 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)30 HttpRequest (io.netty.handler.codec.http.HttpRequest)27 DefaultHttpResponse (io.netty.handler.codec.http.DefaultHttpResponse)26 FullHttpResponse (io.netty.handler.codec.http.FullHttpResponse)24 HttpContent (io.netty.handler.codec.http.HttpContent)18 LastHttpContent (io.netty.handler.codec.http.LastHttpContent)18 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)14 ChannelFuture (io.netty.channel.ChannelFuture)12 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)12 ByteBuf (io.netty.buffer.ByteBuf)10 AsciiString (io.netty.util.AsciiString)9 HttpObject (io.netty.handler.codec.http.HttpObject)7 Map (java.util.Map)7 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)7 WebSocketExtensionData (io.netty.handler.codec.http.websocketx.extensions.WebSocketExtensionData)6 IOException (java.io.IOException)6 Settings (org.elasticsearch.common.settings.Settings)6