Search in sources :

Example 1 with CharArrayBuffer

use of org.apache.hc.core5.util.CharArrayBuffer in project light-4j by networknt.

the class DistinguishedNameParser method parse.

List<NameValuePair> parse(final CharArrayBuffer buf, final ParserCursor cursor) {
    final List<NameValuePair> params = new ArrayList<>();
    tokenParser.skipWhiteSpace(buf, cursor);
    while (!cursor.atEnd()) {
        final NameValuePair param = parseParameter(buf, cursor);
        params.add(param);
    }
    return params;
}
Also used : BasicNameValuePair(org.apache.hc.core5.http.message.copied.BasicNameValuePair) NameValuePair(org.apache.hc.core5.http.copied.NameValuePair) ArrayList(java.util.ArrayList)

Example 2 with CharArrayBuffer

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

the class TestBasicLineFormatter method testRLFormatting.

@Test
public void testRLFormatting() throws Exception {
    final RequestLine requestline = new RequestLine(Method.GET.name(), "/stuff", HttpVersion.HTTP_1_1);
    final CharArrayBuffer buf = new CharArrayBuffer(64);
    this.formatter.formatRequestLine(buf, requestline);
    Assertions.assertEquals("GET /stuff HTTP/1.1", buf.toString());
}
Also used : CharArrayBuffer(org.apache.hc.core5.util.CharArrayBuffer) Test(org.junit.jupiter.api.Test)

Example 3 with CharArrayBuffer

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

the class TestBasicLineParser method testSLParseFailure.

@Test
public void testSLParseFailure() throws Exception {
    final CharArrayBuffer buf = new CharArrayBuffer(64);
    buf.clear();
    buf.append("xxx 200 OK");
    Assertions.assertThrows(ParseException.class, () -> parser.parseStatusLine(buf));
    buf.clear();
    buf.append("HTTP/1.1 xxx OK");
    Assertions.assertThrows(ParseException.class, () -> parser.parseStatusLine(buf));
    buf.clear();
    buf.append("HTTP/1.1    ");
    Assertions.assertThrows(ParseException.class, () -> parser.parseStatusLine(buf));
    buf.clear();
    buf.append("HTTP/1.1");
    Assertions.assertThrows(ParseException.class, () -> parser.parseStatusLine(buf));
    buf.clear();
    buf.append("HTTP/1.1 -200 OK");
    Assertions.assertThrows(ParseException.class, () -> parser.parseStatusLine(buf));
}
Also used : CharArrayBuffer(org.apache.hc.core5.util.CharArrayBuffer) Test(org.junit.jupiter.api.Test)

Example 4 with CharArrayBuffer

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

the class TestBasicLineParser method testSLParse.

@Test
public void testSLParse() throws Exception {
    final CharArrayBuffer buf = new CharArrayBuffer(64);
    // typical status line
    buf.clear();
    buf.append("HTTP/1.1 200 OK");
    StatusLine statusLine = this.parser.parseStatusLine(buf);
    Assertions.assertEquals("HTTP/1.1 200 OK", statusLine.toString());
    Assertions.assertEquals(HttpVersion.HTTP_1_1, statusLine.getProtocolVersion());
    Assertions.assertEquals(200, statusLine.getStatusCode());
    Assertions.assertEquals("OK", statusLine.getReasonPhrase());
    // status line with multi word reason phrase
    buf.clear();
    buf.append("HTTP/1.1 404 Not Found");
    statusLine = this.parser.parseStatusLine(buf);
    Assertions.assertEquals(404, statusLine.getStatusCode());
    Assertions.assertEquals("Not Found", statusLine.getReasonPhrase());
    // reason phrase can be anyting
    buf.clear();
    buf.append("HTTP/1.1 404 Non Trouve");
    statusLine = this.parser.parseStatusLine(buf);
    Assertions.assertEquals("Non Trouve", statusLine.getReasonPhrase());
    // its ok to end with a \n\r
    buf.clear();
    buf.append("HTTP/1.1 404 Not Found\r\n");
    statusLine = this.parser.parseStatusLine(buf);
    Assertions.assertEquals("Not Found", statusLine.getReasonPhrase());
    // this is valid according to the Status-Line BNF
    buf.clear();
    buf.append("HTTP/1.1 200 ");
    statusLine = this.parser.parseStatusLine(buf);
    Assertions.assertEquals(200, statusLine.getStatusCode());
    Assertions.assertEquals("", statusLine.getReasonPhrase());
    // this is not strictly valid, but is lenient
    buf.clear();
    buf.append("HTTP/1.1 200");
    statusLine = this.parser.parseStatusLine(buf);
    Assertions.assertEquals(200, statusLine.getStatusCode());
    Assertions.assertEquals("", statusLine.getReasonPhrase());
    // this is not strictly valid, but is lenient
    buf.clear();
    buf.append("HTTP/1.1     200 OK");
    statusLine = this.parser.parseStatusLine(buf);
    Assertions.assertEquals(200, statusLine.getStatusCode());
    Assertions.assertEquals("OK", statusLine.getReasonPhrase());
    // this is not strictly valid, but is lenient
    buf.clear();
    buf.append("\nHTTP/1.1 200 OK");
    statusLine = this.parser.parseStatusLine(buf);
    Assertions.assertEquals(200, statusLine.getStatusCode());
    Assertions.assertEquals("OK", statusLine.getReasonPhrase());
    Assertions.assertEquals(HttpVersion.HTTP_1_1, statusLine.getProtocolVersion());
    // this is not strictly valid, but is lenient
    buf.clear();
    buf.append("  HTTP/1.1 200 OK");
    statusLine = this.parser.parseStatusLine(buf);
    Assertions.assertEquals(200, statusLine.getStatusCode());
    Assertions.assertEquals("OK", statusLine.getReasonPhrase());
    Assertions.assertEquals(HttpVersion.HTTP_1_1, statusLine.getProtocolVersion());
}
Also used : CharArrayBuffer(org.apache.hc.core5.util.CharArrayBuffer) Test(org.junit.jupiter.api.Test)

Example 5 with CharArrayBuffer

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

the class TestBasicLineParser method testRLParseFailure.

@Test
public void testRLParseFailure() throws Exception {
    final CharArrayBuffer buf = new CharArrayBuffer(64);
    buf.clear();
    buf.append("    ");
    Assertions.assertThrows(ParseException.class, () -> parser.parseRequestLine(buf));
    buf.clear();
    buf.append("  GET");
    Assertions.assertThrows(ParseException.class, () -> parser.parseRequestLine(buf));
    buf.clear();
    buf.append("GET /stuff");
    Assertions.assertThrows(ParseException.class, () -> parser.parseRequestLine(buf));
    buf.clear();
    buf.append("GET/stuff HTTP/1.1");
    Assertions.assertThrows(ParseException.class, () -> parser.parseRequestLine(buf));
    buf.clear();
    buf.append("GET /stuff HTTP/1.1 Oooooooooooppsie");
    Assertions.assertThrows(ParseException.class, () -> parser.parseRequestLine(buf));
}
Also used : CharArrayBuffer(org.apache.hc.core5.util.CharArrayBuffer) 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