Search in sources :

Example 6 with HttpMessage

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

the class TestDefaultContentLengthStrategy method testEntityWithIdentityTransferEncoding.

@Test
public void testEntityWithIdentityTransferEncoding() throws Exception {
    final ContentLengthStrategy lenStrategy = new DefaultContentLengthStrategy();
    final HttpMessage message = new TestHttpMessage();
    message.addHeader("Transfer-Encoding", "Identity");
    Assertions.assertThrows(NotImplementedException.class, () -> lenStrategy.determineLength(message));
}
Also used : ContentLengthStrategy(org.apache.hc.core5.http.ContentLengthStrategy) HttpMessage(org.apache.hc.core5.http.HttpMessage) Test(org.junit.jupiter.api.Test)

Example 7 with HttpMessage

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

the class TestDefaultContentLengthStrategy method testEntityWithContentLength.

@Test
public void testEntityWithContentLength() throws Exception {
    final ContentLengthStrategy lenStrategy = new DefaultContentLengthStrategy();
    final HttpMessage message = new TestHttpMessage();
    message.addHeader("Content-Length", "100");
    Assertions.assertEquals(100, lenStrategy.determineLength(message));
}
Also used : ContentLengthStrategy(org.apache.hc.core5.http.ContentLengthStrategy) HttpMessage(org.apache.hc.core5.http.HttpMessage) Test(org.junit.jupiter.api.Test)

Example 8 with HttpMessage

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

the class TestDefaultContentLengthStrategy method testEntityWithInvalidContentLength.

@Test
public void testEntityWithInvalidContentLength() throws Exception {
    final ContentLengthStrategy lenStrategy = new DefaultContentLengthStrategy();
    final HttpMessage message = new TestHttpMessage();
    message.addHeader("Content-Length", "whatever");
    Assertions.assertThrows(ProtocolException.class, () -> lenStrategy.determineLength(message));
}
Also used : ContentLengthStrategy(org.apache.hc.core5.http.ContentLengthStrategy) HttpMessage(org.apache.hc.core5.http.HttpMessage) Test(org.junit.jupiter.api.Test)

Example 9 with HttpMessage

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

the class TestMessageSupport method testContentHeadersAlreadyPresent.

@Test
public void testContentHeadersAlreadyPresent() throws Exception {
    final HttpEntity entity = HttpEntities.create("some stuff with trailers", StandardCharsets.US_ASCII, new BasicHeader("z", "this"), new BasicHeader("b", "that"), new BasicHeader("a", "this and that"));
    final HttpMessage message = new BasicHttpResponse(200);
    message.addHeader(HttpHeaders.TRAILER, "a, a, a");
    message.addHeader(HttpHeaders.CONTENT_TYPE, "text/plain; charset=ascii");
    MessageSupport.addTrailerHeader(message, entity);
    MessageSupport.addContentTypeHeader(message, entity);
    final Header h1 = message.getFirstHeader(HttpHeaders.TRAILER);
    final Header h2 = message.getFirstHeader(HttpHeaders.CONTENT_TYPE);
    Assertions.assertNotNull(h1);
    Assertions.assertEquals("a, a, a", h1.getValue());
    Assertions.assertNotNull(h2);
    Assertions.assertEquals("text/plain; charset=ascii", h2.getValue());
}
Also used : HttpEntity(org.apache.hc.core5.http.HttpEntity) Header(org.apache.hc.core5.http.Header) HttpMessage(org.apache.hc.core5.http.HttpMessage) Test(org.junit.jupiter.api.Test)

Example 10 with HttpMessage

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

the class TestMessageSupport method testAddContentHeaders.

@Test
public void testAddContentHeaders() throws Exception {
    final HttpEntity entity = HttpEntities.create("some stuff with trailers", StandardCharsets.US_ASCII, new BasicHeader("z", "this"), new BasicHeader("b", "that"), new BasicHeader("a", "this and that"));
    final HttpMessage message = new BasicHttpResponse(200);
    MessageSupport.addTrailerHeader(message, entity);
    MessageSupport.addContentTypeHeader(message, entity);
    final Header h1 = message.getFirstHeader(HttpHeaders.TRAILER);
    final Header h2 = message.getFirstHeader(HttpHeaders.CONTENT_TYPE);
    Assertions.assertNotNull(h1);
    Assertions.assertEquals("z, b, a", h1.getValue());
    Assertions.assertNotNull(h2);
    Assertions.assertEquals("text/plain; charset=US-ASCII", h2.getValue());
}
Also used : HttpEntity(org.apache.hc.core5.http.HttpEntity) Header(org.apache.hc.core5.http.Header) HttpMessage(org.apache.hc.core5.http.HttpMessage) Test(org.junit.jupiter.api.Test)

Aggregations

HttpMessage (org.apache.hc.core5.http.HttpMessage)9 Test (org.junit.jupiter.api.Test)9 ContentLengthStrategy (org.apache.hc.core5.http.ContentLengthStrategy)7 Header (org.apache.hc.core5.http.Header)3 HttpEntity (org.apache.hc.core5.http.HttpEntity)2 NotImplementedException (org.apache.hc.core5.http.NotImplementedException)1 ProtocolException (org.apache.hc.core5.http.ProtocolException)1