BasicLineFormatter implements LineFormatter { @Override public void formatHeader(final CharArrayBuffer buffer, final Header header) { Args.notNull(buffer, "Char array buffer"); Args.notNull(header, "Header"); buffer.append(header.getName()); buffer.append(": "); final String value = header.getValue(); if (value != null) { buffer.ensureCapacity(buffer.length() + value.length()); for (int valueIndex = 0; valueIndex < value.length(); valueIndex++) { char valueChar = value.charAt(valueIndex); if (valueChar == '\r' || valueChar == '\n' || valueChar == '\f' || valueChar == 0x0b) { valueChar = ' '; } buffer.append(valueChar); } } } BasicLineFormatter(); @Override void formatRequestLine(final CharArrayBuffer buffer, final RequestLine reqline); @Override void formatStatusLine(final CharArrayBuffer buffer, final StatusLine statusLine); @Override void formatHeader(final CharArrayBuffer buffer, final Header header); final static BasicLineFormatter INSTANCE; }
@Test public void testHeaderFormatting() throws Exception { final CharArrayBuffer buf = new CharArrayBuffer(64); final Header header1 = new BasicHeader("name", "value"); this.formatter.formatHeader(buf, header1); Assert.assertEquals("name: value", buf.toString()); buf.clear(); final Header header2 = new BasicHeader("name", null); this.formatter.formatHeader(buf, header2); Assert.assertEquals("name: ", buf.toString()); }
@Test public void testHeaderFormattingInvalidInput() throws Exception { final CharArrayBuffer buf = new CharArrayBuffer(64); final Header header = new BasicHeader("name", "value"); try { this.formatter.formatHeader(null, header); Assert.fail("NullPointerException should habe been thrown"); } catch (final NullPointerException ex) { } try { this.formatter.formatHeader(buf, null); Assert.fail("NullPointerException should habe been thrown"); } catch (final NullPointerException ex) { } }
@Test public void testHeaderFormattingRequestSplitting() throws Exception { final CharArrayBuffer buf = new CharArrayBuffer(64); final Header header = new BasicHeader("Host", "apache.org\r\nOops: oops"); formatter.formatHeader(buf, header); final String s = buf.toString(); Assert.assertFalse(s.contains("\n")); Assert.assertEquals("Host: apache.org Oops: oops", s); }