Search in sources :

Example 1 with BasicHttpResponse

use of org.apache.hc.core5.http.message.BasicHttpResponse in project httpcomponents-core by apache.

the class TestDefaultH2ResponseConverter method testConvertFromMessageConnectionHeader.

@Test
public void testConvertFromMessageConnectionHeader() throws Exception {
    final HttpResponse response = new BasicHttpResponse(200);
    response.addHeader("Connection", "Keep-Alive");
    final DefaultH2ResponseConverter converter = new DefaultH2ResponseConverter();
    Assertions.assertThrows(HttpException.class, () -> converter.convert(response), "Header 'Connection: Keep-Alive' is illegal for HTTP/2 messages");
}
Also used : BasicHttpResponse(org.apache.hc.core5.http.message.BasicHttpResponse) BasicHttpResponse(org.apache.hc.core5.http.message.BasicHttpResponse) HttpResponse(org.apache.hc.core5.http.HttpResponse) Test(org.junit.jupiter.api.Test)

Example 2 with BasicHttpResponse

use of org.apache.hc.core5.http.message.BasicHttpResponse in project httpcomponents-core by apache.

the class TestDefaultH2ResponseConverter method testConvertFromMessageBasic.

@Test
public void testConvertFromMessageBasic() throws Exception {
    final HttpResponse response = new BasicHttpResponse(200);
    response.addHeader("custom123", "Value");
    final DefaultH2ResponseConverter converter = new DefaultH2ResponseConverter();
    final List<Header> headers = converter.convert(response);
    Assertions.assertNotNull(headers);
    Assertions.assertEquals(2, headers.size());
    final Header header1 = headers.get(0);
    Assertions.assertEquals(":status", header1.getName());
    Assertions.assertEquals("200", header1.getValue());
    final Header header2 = headers.get(1);
    Assertions.assertEquals("custom123", header2.getName());
    Assertions.assertEquals("Value", header2.getValue());
}
Also used : BasicHttpResponse(org.apache.hc.core5.http.message.BasicHttpResponse) BasicHeader(org.apache.hc.core5.http.message.BasicHeader) Header(org.apache.hc.core5.http.Header) BasicHttpResponse(org.apache.hc.core5.http.message.BasicHttpResponse) HttpResponse(org.apache.hc.core5.http.HttpResponse) Test(org.junit.jupiter.api.Test)

Example 3 with BasicHttpResponse

use of org.apache.hc.core5.http.message.BasicHttpResponse in project httpcomponents-core by apache.

the class EchoHandler method handleRequest.

@Override
public void handleRequest(final HttpRequest request, final EntityDetails entityDetails, final ResponseChannel responseChannel, final HttpContext context) throws HttpException, IOException {
    final HttpResponse response = new BasicHttpResponse(HttpStatus.SC_OK);
    responseChannel.sendResponse(response, entityDetails, context);
}
Also used : BasicHttpResponse(org.apache.hc.core5.http.message.BasicHttpResponse) BasicHttpResponse(org.apache.hc.core5.http.message.BasicHttpResponse) HttpResponse(org.apache.hc.core5.http.HttpResponse)

Example 4 with BasicHttpResponse

use of org.apache.hc.core5.http.message.BasicHttpResponse in project httpcomponents-core by apache.

the class ReactiveEchoProcessor method processRequest.

@Override
public void processRequest(final HttpRequest request, final EntityDetails entityDetails, final ResponseChannel responseChannel, final HttpContext context, final Publisher<ByteBuffer> requestBody, final Callback<Publisher<ByteBuffer>> responseBodyFuture) throws HttpException, IOException {
    if (new BasicHeader(HttpHeaders.EXPECT, HeaderElements.CONTINUE).equals(request.getHeader(HttpHeaders.EXPECT))) {
        responseChannel.sendInformation(new BasicHttpResponse(100), context);
    }
    responseChannel.sendResponse(new BasicHttpResponse(200), new BasicEntityDetails(-1, ContentType.APPLICATION_OCTET_STREAM), context);
    responseBodyFuture.execute(requestBody);
}
Also used : BasicHttpResponse(org.apache.hc.core5.http.message.BasicHttpResponse) BasicEntityDetails(org.apache.hc.core5.http.impl.BasicEntityDetails) BasicHeader(org.apache.hc.core5.http.message.BasicHeader)

Example 5 with BasicHttpResponse

use of org.apache.hc.core5.http.message.BasicHttpResponse in project httpcomponents-core by apache.

the class ReactiveRandomProcessor method processRequest.

@Override
public void processRequest(final HttpRequest request, final EntityDetails entityDetails, final ResponseChannel responseChannel, final HttpContext context, final Publisher<ByteBuffer> requestBody, final Callback<Publisher<ByteBuffer>> responseBodyCallback) throws HttpException, IOException {
    final String method = request.getMethod();
    if (!"GET".equalsIgnoreCase(method) && !"HEAD".equalsIgnoreCase(method) && !"POST".equalsIgnoreCase(method) && !"PUT".equalsIgnoreCase(method)) {
        throw new MethodNotSupportedException(method + " not supported by " + getClass().getName());
    }
    final URI uri;
    try {
        uri = request.getUri();
    } catch (final URISyntaxException ex) {
        throw new ProtocolException(ex.getMessage(), ex);
    }
    final String path = uri.getPath();
    final int slash = path.lastIndexOf('/');
    if (slash != -1) {
        final String payload = path.substring(slash + 1);
        final long n;
        if (!payload.isEmpty()) {
            try {
                n = Long.parseLong(payload);
            } catch (final NumberFormatException ex) {
                throw new ProtocolException("Invalid request path: " + path);
            }
        } else {
            // random length, but make sure at least something is sent
            n = 1 + (int) (Math.random() * 79.0);
        }
        if (new BasicHeader(HttpHeaders.EXPECT, HeaderElements.CONTINUE).equals(request.getHeader(HttpHeaders.EXPECT))) {
            responseChannel.sendInformation(new BasicHttpResponse(100), context);
        }
        final HttpResponse response = new BasicHttpResponse(HttpStatus.SC_OK);
        final Flowable<ByteBuffer> stream = ReactiveTestUtils.produceStream(n);
        final String hash = ReactiveTestUtils.getStreamHash(n);
        response.addHeader("response-hash-code", hash);
        final BasicEntityDetails basicEntityDetails = new BasicEntityDetails(n, ContentType.APPLICATION_OCTET_STREAM);
        responseChannel.sendResponse(response, basicEntityDetails, context);
        responseBodyCallback.execute(stream);
    } else {
        throw new ProtocolException("Invalid request path: " + path);
    }
}
Also used : ProtocolException(org.apache.hc.core5.http.ProtocolException) BasicHttpResponse(org.apache.hc.core5.http.message.BasicHttpResponse) HttpResponse(org.apache.hc.core5.http.HttpResponse) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) ByteBuffer(java.nio.ByteBuffer) BasicHttpResponse(org.apache.hc.core5.http.message.BasicHttpResponse) MethodNotSupportedException(org.apache.hc.core5.http.MethodNotSupportedException) BasicEntityDetails(org.apache.hc.core5.http.impl.BasicEntityDetails) BasicHeader(org.apache.hc.core5.http.message.BasicHeader)

Aggregations

BasicHttpResponse (org.apache.hc.core5.http.message.BasicHttpResponse)60 HttpResponse (org.apache.hc.core5.http.HttpResponse)57 Test (org.junit.jupiter.api.Test)40 HttpRequest (org.apache.hc.core5.http.HttpRequest)15 Header (org.apache.hc.core5.http.Header)14 HttpException (org.apache.hc.core5.http.HttpException)11 IOException (java.io.IOException)10 BasicHttpRequest (org.apache.hc.core5.http.message.BasicHttpRequest)9 HttpContext (org.apache.hc.core5.http.protocol.HttpContext)9 InetSocketAddress (java.net.InetSocketAddress)8 EntityDetails (org.apache.hc.core5.http.EntityDetails)8 ByteBuffer (java.nio.ByteBuffer)7 BasicHeader (org.apache.hc.core5.http.message.BasicHeader)7 Test (org.junit.Test)7 ProtocolException (org.apache.hc.core5.http.ProtocolException)6 AsyncEntityProducer (org.apache.hc.core5.http.nio.AsyncEntityProducer)6 AsyncServerExchangeHandler (org.apache.hc.core5.http.nio.AsyncServerExchangeHandler)6 Message (org.apache.hc.core5.http.Message)5 CapacityChannel (org.apache.hc.core5.http.nio.CapacityChannel)5 DataStreamChannel (org.apache.hc.core5.http.nio.DataStreamChannel)5