org.webbitserver.stub
Class StubHttpResponse

java.lang.Object
  extended by org.webbitserver.stub.StubHttpResponse
All Implemented Interfaces:
HttpResponse

public class StubHttpResponse
extends Object
implements HttpResponse

Implementation of HttpResponse that is easy to construct manually, and inspect results. Useful for testing.


Constructor Summary
StubHttpResponse()
           
 
Method Summary
 Charset charset()
          Current Charset used to encode to response as.
 StubHttpResponse charset(Charset charset)
          For text based responses, sets the Charset to encode the response as.
 StubHttpResponse chunked()
          Turns the response into a chunked response

after this method is called, HttpResponse.write(String) should be used to send chunks

 boolean containsHeader(String name)
          Test to see if this response has a header of the specified name
 StubHttpResponse content(byte[] content)
          Write binary based content back to the client.
 StubHttpResponse content(ByteBuffer buffer)
          Write binary based content back to the client.
 StubHttpResponse content(String content)
          Write text based content back to the client.
 byte[] contents()
           
 String contentsString()
           
 StubHttpResponse cookie(HttpCookie httpCookie)
          Adds a cookie
 StubHttpResponse cookie(String name, String value)
           
 List<HttpCookie> cookies()
           
 StubHttpResponse end()
          Marks the response as ended.
 boolean ended()
           
 Throwable error()
           
 StubHttpResponse error(Throwable error)
          Marks the response as erroneous.
 String header(String name)
           
 StubHttpResponse header(String name, Date value)
          Adds a Date (RFC 1123 format) HTTP header.
 StubHttpResponse header(String name, long value)
          Adds a numeric HTTP header.
 StubHttpResponse header(String name, String value)
          Adds an HTTP header.
 int status()
          Retrieve HTTP status code that this response is going to return.
 StubHttpResponse status(int status)
          Sets the HTTP status code.
 String toString()
           
 StubHttpResponse write(String content)
          Writes immediately to the client without closing the connection.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

StubHttpResponse

public StubHttpResponse()
Method Detail

charset

public StubHttpResponse charset(Charset charset)
Description copied from interface: HttpResponse
For text based responses, sets the Charset to encode the response as.

If not set, defaults to UTF8.

Specified by:
charset in interface HttpResponse

charset

public Charset charset()
Description copied from interface: HttpResponse
Current Charset used to encode to response as.

Specified by:
charset in interface HttpResponse
See Also:
HttpResponse.charset(Charset)

chunked

public StubHttpResponse chunked()
Description copied from interface: HttpResponse
Turns the response into a chunked response

after this method is called, HttpResponse.write(String) should be used to send chunks

Specified by:
chunked in interface HttpResponse

status

public StubHttpResponse status(int status)
Description copied from interface: HttpResponse
Sets the HTTP status code.

Defaults to 200 (OK).

Specified by:
status in interface HttpResponse

status

public int status()
Description copied from interface: HttpResponse
Retrieve HTTP status code that this response is going to return.

Specified by:
status in interface HttpResponse
See Also:
HttpResponse.status(int)

header

public StubHttpResponse header(String name,
                               String value)
Description copied from interface: HttpResponse
Adds an HTTP header. Multiple HTTP headers can be added with the same name.

Specified by:
header in interface HttpResponse

header

public StubHttpResponse header(String name,
                               long value)
Description copied from interface: HttpResponse
Adds a numeric HTTP header. Multiple HTTP headers can be added with the same name.

Specified by:
header in interface HttpResponse

header

public StubHttpResponse header(String name,
                               Date value)
Description copied from interface: HttpResponse
Adds a Date (RFC 1123 format) HTTP header. Multiple HTTP headers can be added with the same name.

Specified by:
header in interface HttpResponse

cookie

public StubHttpResponse cookie(HttpCookie httpCookie)
Description copied from interface: HttpResponse
Adds a cookie

Specified by:
cookie in interface HttpResponse
Parameters:
httpCookie - the cookie

cookie

public StubHttpResponse cookie(String name,
                               String value)

header

public String header(String name)

containsHeader

public boolean containsHeader(String name)
Description copied from interface: HttpResponse
Test to see if this response has a header of the specified name

Specified by:
containsHeader in interface HttpResponse

content

public StubHttpResponse content(String content)
Description copied from interface: HttpResponse
Write text based content back to the client.

Specified by:
content in interface HttpResponse
See Also:
HttpResponse.charset(Charset), HttpResponse.content(byte[])

write

public StubHttpResponse write(String content)
Description copied from interface: HttpResponse
Writes immediately to the client without closing the connection. (The HttpResponse.content(String) method caches content until HttpResponse.end() is called()

TODO: Make content write immediately instead and remove this method?

Specified by:
write in interface HttpResponse
Parameters:
content - what to write

content

public StubHttpResponse content(byte[] content)
Description copied from interface: HttpResponse
Write binary based content back to the client.

Specified by:
content in interface HttpResponse
See Also:
HttpResponse.content(String)

content

public StubHttpResponse content(ByteBuffer buffer)
Description copied from interface: HttpResponse
Write binary based content back to the client.

Specified by:
content in interface HttpResponse
See Also:
HttpResponse.content(String)

contents

public byte[] contents()

contentsString

public String contentsString()

error

public StubHttpResponse error(Throwable error)
Description copied from interface: HttpResponse
Marks the response as erroneous. The error shall be displayed to the user (500 SERVER ERROR) and the connection closed.

Every response should have either HttpResponse.end() or HttpResponse.error(Throwable) called. No more operations should be performed on a response after these.

Specified by:
error in interface HttpResponse

error

public Throwable error()

end

public StubHttpResponse end()
Description copied from interface: HttpResponse
Marks the response as ended. At this point any remaining data shall be flushed and the connection closed.

Every response should have either HttpResponse.end() or HttpResponse.error(Throwable) called. No more operations should be performed on a response after these.

Specified by:
end in interface HttpResponse

ended

public boolean ended()

cookies

public List<HttpCookie> cookies()

toString

public String toString()
Overrides:
toString in class Object


Copyright © 2013. All Rights Reserved.