Search in sources :

Example 11 with CharArrayBuffer

use of org.apache.hc.core5.util.CharArrayBuffer in project httpcomponents-core by apache.

the class TestBasicHeaderValueParser method testNVParse.

@Test
public void testNVParse() {
    String s = "test";
    CharArrayBuffer buffer = new CharArrayBuffer(64);
    buffer.append(s);
    ParserCursor cursor = new ParserCursor(0, s.length());
    NameValuePair param = this.parser.parseNameValuePair(buffer, cursor);
    Assertions.assertEquals("test", param.getName());
    Assertions.assertNull(param.getValue());
    Assertions.assertEquals(s.length(), cursor.getPos());
    Assertions.assertTrue(cursor.atEnd());
    s = "test;";
    buffer = new CharArrayBuffer(16);
    buffer.append(s);
    cursor = new ParserCursor(0, s.length());
    param = this.parser.parseNameValuePair(buffer, cursor);
    Assertions.assertEquals("test", param.getName());
    Assertions.assertNull(param.getValue());
    Assertions.assertEquals(s.length(), cursor.getPos());
    Assertions.assertTrue(cursor.atEnd());
    s = "test  ,12";
    buffer = new CharArrayBuffer(16);
    buffer.append(s);
    cursor = new ParserCursor(0, s.length());
    param = this.parser.parseNameValuePair(buffer, cursor);
    Assertions.assertEquals("test", param.getName());
    Assertions.assertNull(param.getValue());
    Assertions.assertEquals(s.length() - 2, cursor.getPos());
    Assertions.assertFalse(cursor.atEnd());
    s = "test=stuff";
    buffer = new CharArrayBuffer(16);
    buffer.append(s);
    cursor = new ParserCursor(0, s.length());
    param = this.parser.parseNameValuePair(buffer, cursor);
    Assertions.assertEquals("test", param.getName());
    Assertions.assertEquals("stuff", param.getValue());
    Assertions.assertEquals(s.length(), cursor.getPos());
    Assertions.assertTrue(cursor.atEnd());
    s = "   test  =   stuff ";
    buffer = new CharArrayBuffer(16);
    buffer.append(s);
    cursor = new ParserCursor(0, s.length());
    param = this.parser.parseNameValuePair(buffer, cursor);
    Assertions.assertEquals("test", param.getName());
    Assertions.assertEquals("stuff", param.getValue());
    Assertions.assertEquals(s.length(), cursor.getPos());
    Assertions.assertTrue(cursor.atEnd());
    s = "   test  =   stuff ;1234";
    buffer = new CharArrayBuffer(16);
    buffer.append(s);
    cursor = new ParserCursor(0, s.length());
    param = this.parser.parseNameValuePair(buffer, cursor);
    Assertions.assertEquals("test", param.getName());
    Assertions.assertEquals("stuff", param.getValue());
    Assertions.assertEquals(s.length() - 4, cursor.getPos());
    Assertions.assertFalse(cursor.atEnd());
    s = "test  = \"stuff\"";
    buffer = new CharArrayBuffer(16);
    buffer.append(s);
    cursor = new ParserCursor(0, s.length());
    param = this.parser.parseNameValuePair(buffer, cursor);
    Assertions.assertEquals("test", param.getName());
    Assertions.assertEquals("stuff", param.getValue());
    s = "test  = \"  stuff\\\"\"";
    buffer = new CharArrayBuffer(16);
    buffer.append(s);
    cursor = new ParserCursor(0, s.length());
    param = this.parser.parseNameValuePair(buffer, cursor);
    Assertions.assertEquals("test", param.getName());
    Assertions.assertEquals("  stuff\"", param.getValue());
    s = "  test";
    buffer = new CharArrayBuffer(16);
    buffer.append(s);
    cursor = new ParserCursor(0, s.length());
    param = this.parser.parseNameValuePair(buffer, cursor);
    Assertions.assertEquals("test", param.getName());
    Assertions.assertNull(param.getValue());
    s = "  ";
    buffer = new CharArrayBuffer(16);
    buffer.append(s);
    cursor = new ParserCursor(0, s.length());
    param = this.parser.parseNameValuePair(buffer, cursor);
    Assertions.assertEquals("", param.getName());
    Assertions.assertNull(param.getValue());
    s = " = stuff ";
    buffer = new CharArrayBuffer(16);
    buffer.append(s);
    cursor = new ParserCursor(0, s.length());
    param = this.parser.parseNameValuePair(buffer, cursor);
    Assertions.assertEquals("", param.getName());
    Assertions.assertEquals("stuff", param.getValue());
}
Also used : NameValuePair(org.apache.hc.core5.http.NameValuePair) CharArrayBuffer(org.apache.hc.core5.util.CharArrayBuffer) Test(org.junit.jupiter.api.Test)

Example 12 with CharArrayBuffer

use of org.apache.hc.core5.util.CharArrayBuffer in project httpcomponents-core by apache.

the class TestBasicLineFormatter method testRLFormattingInvalidInput.

@Test
public void testRLFormattingInvalidInput() throws Exception {
    final CharArrayBuffer buf = new CharArrayBuffer(64);
    final RequestLine requestline = new RequestLine(Method.GET.name(), "/stuff", HttpVersion.HTTP_1_1);
    Assertions.assertThrows(NullPointerException.class, () -> formatter.formatRequestLine(null, requestline));
    Assertions.assertThrows(NullPointerException.class, () -> formatter.formatRequestLine(buf, null));
}
Also used : CharArrayBuffer(org.apache.hc.core5.util.CharArrayBuffer) Test(org.junit.jupiter.api.Test)

Example 13 with CharArrayBuffer

use of org.apache.hc.core5.util.CharArrayBuffer in project httpcomponents-core by apache.

the class TestBasicLineFormatter method testSLFormattingInvalidInput.

@Test
public void testSLFormattingInvalidInput() throws Exception {
    final CharArrayBuffer buf = new CharArrayBuffer(64);
    final StatusLine statusline = new StatusLine(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK");
    Assertions.assertThrows(NullPointerException.class, () -> formatter.formatStatusLine(null, statusline));
    Assertions.assertThrows(NullPointerException.class, () -> formatter.formatStatusLine(buf, null));
}
Also used : CharArrayBuffer(org.apache.hc.core5.util.CharArrayBuffer) Test(org.junit.jupiter.api.Test)

Example 14 with CharArrayBuffer

use of org.apache.hc.core5.util.CharArrayBuffer in project httpcomponents-core by apache.

the class TestBufferedHeader method testBasicConstructor.

@Test
public void testBasicConstructor() throws Exception {
    final CharArrayBuffer buf = new CharArrayBuffer(32);
    buf.append("name: value");
    final BufferedHeader header = new BufferedHeader(buf, false);
    Assertions.assertEquals("name", header.getName());
    Assertions.assertEquals("value", header.getValue());
    Assertions.assertSame(buf, header.getBuffer());
    Assertions.assertEquals(5, header.getValuePos());
}
Also used : CharArrayBuffer(org.apache.hc.core5.util.CharArrayBuffer) Test(org.junit.jupiter.api.Test)

Example 15 with CharArrayBuffer

use of org.apache.hc.core5.util.CharArrayBuffer in project httpcomponents-core by apache.

the class TestBufferedHeader method testSerialization.

@Test
public void testSerialization() throws Exception {
    final CharArrayBuffer buf = new CharArrayBuffer(32);
    buf.append("name: value");
    final BufferedHeader orig = new BufferedHeader(buf, false);
    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 BufferedHeader clone = (BufferedHeader) inStream.readObject();
    Assertions.assertEquals(orig.getName(), clone.getName());
    Assertions.assertEquals(orig.getValue(), clone.getValue());
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) CharArrayBuffer(org.apache.hc.core5.util.CharArrayBuffer) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) ObjectInputStream(java.io.ObjectInputStream) Test(org.junit.jupiter.api.Test)

Aggregations

CharArrayBuffer (org.apache.hc.core5.util.CharArrayBuffer)91 Test (org.junit.jupiter.api.Test)74 SessionOutputBuffer (org.apache.hc.core5.http.nio.SessionOutputBuffer)18 ByteArrayOutputStream (java.io.ByteArrayOutputStream)16 ByteArrayInputStream (java.io.ByteArrayInputStream)14 SessionInputBuffer (org.apache.hc.core5.http.io.SessionInputBuffer)13 BasicHttpTransportMetrics (org.apache.hc.core5.http.impl.BasicHttpTransportMetrics)11 ReadableByteChannel (java.nio.channels.ReadableByteChannel)10 Header (org.apache.hc.core5.http.Header)10 WritableByteChannelMock (org.apache.hc.core5.http.WritableByteChannelMock)10 SessionInputBuffer (org.apache.hc.core5.http.nio.SessionInputBuffer)10 HeaderElement (org.apache.hc.core5.http.HeaderElement)9 SessionOutputBuffer (org.apache.hc.core5.http.io.SessionOutputBuffer)9 NameValuePair (org.apache.hc.core5.http.NameValuePair)8 MessageConstraintException (org.apache.hc.core5.http.MessageConstraintException)7 WritableByteChannel (java.nio.channels.WritableByteChannel)6 CharsetDecoder (java.nio.charset.CharsetDecoder)6 CharsetEncoder (java.nio.charset.CharsetEncoder)6 ProtocolVersion (org.apache.hc.core5.http.ProtocolVersion)6 FormattedHeader (org.apache.hc.core5.http.FormattedHeader)5