BasicLineFormatter implements LineFormatter { @Override public void formatStatusLine(final CharArrayBuffer buffer, final StatusLine statusLine) { Args.notNull(buffer, "Char array buffer"); Args.notNull(statusLine, "Status line"); formatProtocolVersion(buffer, statusLine.getProtocolVersion()); buffer.append(' '); buffer.append(Integer.toString(statusLine.getStatusCode())); buffer.append(' '); final String reasonPhrase = statusLine.getReasonPhrase(); if (reasonPhrase != null) { buffer.append(reasonPhrase); } } 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 testSLFormatting() throws Exception { final CharArrayBuffer buf = new CharArrayBuffer(64); final StatusLine statusline1 = new StatusLine(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK"); this.formatter.formatStatusLine(buf, statusline1); Assert.assertEquals("HTTP/1.1 200 OK", buf.toString()); buf.clear(); final StatusLine statusline2 = new StatusLine(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, null); this.formatter.formatStatusLine(buf, statusline2); Assert.assertEquals("HTTP/1.1 200 ", buf.toString()); }
@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"); try { this.formatter.formatStatusLine(null, statusline); Assert.fail("NullPointerException should habe been thrown"); } catch (final NullPointerException ex) { } try { this.formatter.formatStatusLine(buf, null); Assert.fail("NullPointerException should habe been thrown"); } catch (final NullPointerException ex) { } }