@Test
  public void testSerialize() throws Exception {
    byte[] bodyBytes = "{'test': 'this is a value'}".getBytes();
    InputStream body = new ByteArrayInputStream(bodyBytes);
    HttpResponse response =
        HttpResponse.status(HttpStatus.OK)
            .header("Some-Special-Header", "test")
            .header("Content-Type", "application/json")
            .body(body);

    OutputStream os = new ByteArrayOutputStream();
    responseSerializer.serialize(response, os);

    String expected =
        "HTTP/1\\.1 200 OK\\r\\n"
            + "content-type: application/json\\r\\n"
            + "some-special-header: test\\r\\n"
            + "Date: .*\\r\\n"
            + "Content-Length: "
            + bodyBytes.length
            + "\\r\\n"
            + "\\r\\n"
            + "\\{'test': 'this is a value'\\}";

    Assert.assertTrue(os.toString().matches(expected));
  }
Пример #2
0
 @Override
 public void badMessage(int status, String reason) {
   HttpExchange exchange = connection.getExchange();
   HttpResponse response = exchange.getResponse();
   response.status(status).reason(reason);
   failAndClose(new HttpResponseException("HTTP protocol violation: bad response", response));
 }
Пример #3
0
 public HttpResponse asResponse(String bodyMessage) {
   HttpResponse response = new HttpResponse();
   response.status(this);
   if (bodyMessage != null) {
     response.body(bodyMessage);
   }
   return response;
 }
Пример #4
0
 public HttpResponse applyTo(HttpResponse httpResponse) {
   httpResponse.status(this);
   return httpResponse;
 }