HttpRequestEncoder extends HttpObjectEncoder<HttpRequest> { @Override protected void encodeInitialLine(ByteBuf buf, HttpRequest request) throws Exception { ByteBufUtil.copy(request.method().asciiName(), buf); String uri = request.uri(); if (uri.isEmpty()) { ByteBufUtil.writeMediumBE(buf, SPACE_SLASH_AND_SPACE_MEDIUM); } else { CharSequence uriCharSequence = uri; boolean needSlash = false; int start = uri.indexOf(": if (start != -1 && uri.charAt(0) != SLASH) { start += 3; int index = uri.indexOf(QUESTION_MARK, start); if (index == -1) { if (uri.lastIndexOf(SLASH) < start) { needSlash = true; } } else { if (uri.lastIndexOf(SLASH, index) < start) { uriCharSequence = new StringBuilder(uri).insert(index, SLASH); } } } buf.writeByte(SP).writeCharSequence(uriCharSequence, CharsetUtil.UTF_8); if (needSlash) { ByteBufUtil.writeShortBE(buf, SLASH_AND_SPACE_SHORT); } else { buf.writeByte(SP); } } request.protocolVersion().encode(buf); ByteBufUtil.writeShortBE(buf, CRLF_SHORT); }  @Override boolean acceptOutboundMessage(Object msg);  }
@Test public void testUriWithoutPath() throws Exception { for (ByteBuf buffer : getBuffers()) { HttpRequestEncoder encoder = new HttpRequestEncoder(); encoder.encodeInitialLine(buffer, new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "http: String req = buffer.toString(Charset.forName("US-ASCII")); assertEquals("GET http: buffer.release(); } }
@Test public void testUriWithoutPath2() throws Exception { for (ByteBuf buffer : getBuffers()) { HttpRequestEncoder encoder = new HttpRequestEncoder(); encoder.encodeInitialLine(buffer, new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "http: String req = buffer.toString(Charset.forName("US-ASCII")); assertEquals("GET http: buffer.release(); } }
@Test public void testUriWithEmptyPath() throws Exception { for (ByteBuf buffer : getBuffers()) { HttpRequestEncoder encoder = new HttpRequestEncoder(); encoder.encodeInitialLine(buffer, new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "http: String req = buffer.toString(Charset.forName("US-ASCII")); assertEquals("GET http: buffer.release(); } }
@Test public void testUriWithPath() throws Exception { for (ByteBuf buffer : getBuffers()) { HttpRequestEncoder encoder = new HttpRequestEncoder(); encoder.encodeInitialLine(buffer, new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "http: String req = buffer.toString(Charset.forName("US-ASCII")); assertEquals("GET http: buffer.release(); } }
@Test public void testAbsPath() throws Exception { for (ByteBuf buffer : getBuffers()) { HttpRequestEncoder encoder = new HttpRequestEncoder(); encoder.encodeInitialLine(buffer, new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/")); String req = buffer.toString(Charset.forName("US-ASCII")); assertEquals("GET / HTTP/1.1\r\n", req); buffer.release(); } }
@Test public void testEmptyAbsPath() throws Exception { for (ByteBuf buffer : getBuffers()) { HttpRequestEncoder encoder = new HttpRequestEncoder(); encoder.encodeInitialLine(buffer, new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "")); String req = buffer.toString(Charset.forName("US-ASCII")); assertEquals("GET / HTTP/1.1\r\n", req); buffer.release(); } }
@Test public void testQueryStringPath() throws Exception { for (ByteBuf buffer : getBuffers()) { HttpRequestEncoder encoder = new HttpRequestEncoder(); encoder.encodeInitialLine(buffer, new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/?url=http: String req = buffer.toString(Charset.forName("US-ASCII")); assertEquals("GET /?url=http: buffer.release(); } }