Search in sources :

Example 1 with HeaderGroup

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

the class TestHeaderGroup method testSerialization.

@Test
public void testSerialization() throws Exception {
    final HeaderGroup orig = new HeaderGroup();
    final Header header1 = new BasicHeader("name", "value1");
    final Header header2 = new BasicHeader("name", "value2");
    final Header header3 = new BasicHeader("name", "value3");
    orig.setHeaders(header1, header2, header3);
    final ByteArrayOutputStream outbuffer = new ByteArrayOutputStream();
    final ObjectOutputStream outStream = new ObjectOutputStream(outbuffer);
    outStream.writeObject(orig);
    outStream.close();
    final byte[] raw = outbuffer.toByteArray();
    final ByteArrayInputStream inBuffer = new ByteArrayInputStream(raw);
    final ObjectInputStream inStream = new ObjectInputStream(inBuffer);
    final HeaderGroup clone = (HeaderGroup) inStream.readObject();
    final Header[] headers1 = orig.getHeaders();
    final Header[] headers2 = clone.getHeaders();
    Assertions.assertNotNull(headers1);
    Assertions.assertNotNull(headers2);
    Assertions.assertEquals(headers1.length, headers2.length);
    for (int i = 0; i < headers1.length; i++) {
        Assertions.assertEquals(headers1[i].getName(), headers2[i].getName());
        Assertions.assertEquals(headers1[i].getValue(), headers2[i].getValue());
    }
}
Also used : Header(org.apache.hc.core5.http.Header) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) ObjectInputStream(java.io.ObjectInputStream) Test(org.junit.jupiter.api.Test)

Example 2 with HeaderGroup

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

the class TestHeaderGroup method testHeaderRemove.

@Test
public void testHeaderRemove() {
    final HeaderGroup headergroup = new HeaderGroup();
    final Header header1 = new BasicHeader("name", "value1");
    final Header header2 = new BasicHeader("name", "value2");
    final Header header3 = new BasicHeader("name", "value3");
    headergroup.setHeaders(header1, header2, header3);
    final Iterator<Header> i = headergroup.headerIterator();
    Assertions.assertNotNull(i);
    Assertions.assertTrue(i.hasNext());
    i.next();
    Assertions.assertTrue(i.hasNext());
    i.next();
    i.remove();
    Assertions.assertEquals(2, headergroup.getHeaders().length);
    Assertions.assertTrue(i.hasNext());
    i.next();
    i.remove();
    Assertions.assertEquals(1, headergroup.getHeaders().length);
    Assertions.assertFalse(i.hasNext());
}
Also used : Header(org.apache.hc.core5.http.Header) Test(org.junit.jupiter.api.Test)

Example 3 with HeaderGroup

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

the class TestHeaderGroup method testSetHeaders.

@Test
public void testSetHeaders() {
    final HeaderGroup headergroup = new HeaderGroup();
    final Header header1 = new BasicHeader("name1", "value1");
    final Header header2 = new BasicHeader("name2", "value2");
    final Header header3 = new BasicHeader("name3", "value3");
    headergroup.addHeader(header1);
    headergroup.setHeaders(header2, header3);
    Assertions.assertEquals(2, headergroup.getHeaders().length);
    Assertions.assertEquals(0, headergroup.getHeaders("name1").length);
    Assertions.assertFalse(headergroup.containsHeader("name1"));
    Assertions.assertEquals(1, headergroup.getHeaders("name2").length);
    Assertions.assertTrue(headergroup.containsHeader("name2"));
    Assertions.assertEquals(1, headergroup.getHeaders("name3").length);
    Assertions.assertTrue(headergroup.containsHeader("name3"));
    headergroup.setHeaders();
    Assertions.assertEquals(0, headergroup.getHeaders().length);
}
Also used : Header(org.apache.hc.core5.http.Header) Test(org.junit.jupiter.api.Test)

Example 4 with HeaderGroup

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

the class TestHeaderGroup method testCondensedHeader.

@Test
public void testCondensedHeader() {
    final HeaderGroup headergroup = new HeaderGroup();
    Assertions.assertNull(headergroup.getCondensedHeader("name"));
    final Header header1 = new BasicHeader("name", "value1");
    final Header header2 = new BasicHeader("name", "value2");
    final Header header3 = new BasicHeader("name", "value3");
    headergroup.setHeaders(header1, header2, header3);
    Assertions.assertEquals("value1, value2, value3", headergroup.getCondensedHeader("name").getValue());
    headergroup.setHeaders(header1);
    Assertions.assertEquals(header1, headergroup.getCondensedHeader("name"));
}
Also used : Header(org.apache.hc.core5.http.Header) Test(org.junit.jupiter.api.Test)

Example 5 with HeaderGroup

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

the class TestHeaderGroup method testAddRemoveHeaderWithDifferentButEqualHeaders.

@Test
public void testAddRemoveHeaderWithDifferentButEqualHeaders() {
    final HeaderGroup headergroup = new HeaderGroup();
    final Header header = new BasicHeader("name", "value");
    final Header header2 = new BasicHeader("name", "value");
    headergroup.addHeader(header);
    Assertions.assertEquals(1, headergroup.getHeaders().length);
    Assertions.assertTrue(headergroup.removeHeader(header2));
    Assertions.assertEquals(0, headergroup.getHeaders().length);
}
Also used : Header(org.apache.hc.core5.http.Header) Test(org.junit.jupiter.api.Test)

Aggregations

Test (org.junit.jupiter.api.Test)12 Header (org.apache.hc.core5.http.Header)11 HeaderGroup (org.apache.hc.core5.http.message.HeaderGroup)3 MessageHeaders (org.apache.hc.core5.http.MessageHeaders)2 BasicHeader (org.apache.hc.core5.http.message.BasicHeader)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 ObjectInputStream (java.io.ObjectInputStream)1 ObjectOutputStream (java.io.ObjectOutputStream)1 HashSet (java.util.HashSet)1