@Test
  public void UpdateIfMatch() {
    EntityTag entityTag = target("books").path(book1_id).request().get().getEntityTag();

    HashMap<String, Object> updates = new HashMap<String, Object>();
    updates.put("author", "updatedAuthor");
    Entity<HashMap<String, Object>> updateEntity =
        Entity.entity(updates, MediaType.APPLICATION_JSON);
    Response updateResponse =
        target("books")
            .path(book1_id)
            .request()
            .header("If-Match", entityTag)
            .build("PATCH", updateEntity)
            .invoke();

    assertEquals(200, updateResponse.getStatus());

    Response updateResponse2 =
        target("books")
            .path(book1_id)
            .request()
            .header("If-Match", entityTag)
            .build("PATCH", updateEntity)
            .invoke();

    assertEquals(412, updateResponse2.getStatus());
  }
 @Test
 public void AddBookNoTitle() {
   Response response = addBook("author1", null, new Date(), "1234");
   assertEquals(400, response.getStatus());
   String message = response.readEntity(String.class);
   assertTrue(message.contains("title is a required field"));
 }
  @Test
  public void BookNotFoundWithMessage() {
    Response response = target("books").path("1").request().get();
    assertEquals(404, response.getStatus());

    String message = response.readEntity(String.class);
    assertTrue(message.contains("Book 1 is not found"));
  }
  @Test
  public void ContentNegotiationExtensions() {
    Response xmlResponse = target("books").path(book1_id + ".xml").request().get();
    assertEquals(MediaType.APPLICATION_XML, xmlResponse.getHeaderString("Content-Type"));

    Response jsonResponse = target("books").path(book1_id + ".json").request().get();
    assertEquals(MediaType.APPLICATION_JSON, jsonResponse.getHeaderString("Content-Type"));
  }
  @Test
  public void PoweredByHeader() {
    Response response = target("books").path(book1_id).request().get();
    assertEquals("Pluralsight", response.getHeaderString("X-Powered-By"));

    Response response2 = target("books").request().get();
    assertNull(response2.getHeaderString("X-Powered-By"));
  }
Esempio n. 6
0
 @Test
 public void shouldBeAbleToConvertToHTTPProtocolString() throws Exception {
   Response response = new Response(200, "OK");
   response.setBody("This is the body");
   String HTTPString = response.toString();
   String expected = "HTTP/1.1 200 OK\r\nConnection: close\r\n\r\nThis is the body";
   assertEquals(expected, HTTPString);
 }
  @Test
  public void testAddExtraField() {
    Response response = addBook("author", "title", new Date(), "1111", "hello world");
    assertEquals(200, response.getStatus());

    HashMap<String, Object> book = toHashMap(response);
    assertNotNull(book.get("id"));
    assertEquals(book.get("extra1"), "hello world");
  }
  @Test
  public void BookEntityTagNotModified() {
    EntityTag entityTag = target("books").path(book1_id).request().get().getEntityTag();
    assertNotNull(entityTag);

    Response response =
        target("books").path(book1_id).request().header("If-None-Match", entityTag).get();
    assertEquals(304, response.getStatus());
  }
Esempio n. 9
0
 @Test
 public void shouldWorkWithQueryStringParams() {
   Request request =
       new Request(Verb.GET, "http://stackoverflow.com/search?q=oauth+authentication&more=stuff");
   assertEquals(2, request.getQueryStringParams().size());
   assertEquals("http://stackoverflow.com/search", request.getSanitizedUrl());
   Response response = request.send();
   assertEquals(200, response.getCode());
   assertTrue(response.getBody().length() > 0);
 }
  @Test
  public void testAddBook() throws Exception {
    Date thisDate = new Date();

    Response response = addBook("author", "title", thisDate, "3456");
    assertEquals(200, response.getStatus());

    HashMap<String, Object> responseBook = toHashMap(response);
    assertNotNull(responseBook.get("id"));
    assertEquals("title", responseBook.get("title"));
    assertEquals("author", responseBook.get("author"));

    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
    assertEquals(thisDate, dateFormat.parse((String) responseBook.get("published")));
    assertEquals("3456", responseBook.get("isbn"));
  }
  @Test
  public void PatchMethodOverride() {
    HashMap<String, Object> updates = new HashMap<String, Object>();
    updates.put("author", "updatedAuthor");
    Entity<HashMap<String, Object>> updateEntity =
        Entity.entity(updates, MediaType.APPLICATION_JSON);
    Response updateResponse =
        target("books").path(book1_id).queryParam("_method", "PATCH").request().post(updateEntity);

    assertEquals(200, updateResponse.getStatus());

    Response getResponse = target("books").path(book1_id).request().get();
    HashMap<String, Object> getResponseMap = toHashMap(getResponse);

    assertEquals("updatedAuthor", getResponseMap.get("author"));
  }
  @Test
  public void UpdateBookExtra() {
    HashMap<String, Object> updates = new HashMap<String, Object>();
    updates.put("hello", "world");
    Entity<HashMap<String, Object>> updateEntity =
        Entity.entity(updates, MediaType.APPLICATION_JSON);
    Response updateResponse =
        target("books").path(book1_id).request().build("PATCH", updateEntity).invoke();

    assertEquals(200, updateResponse.getStatus());

    Response getResponse = target("books").path(book1_id).request().get();
    HashMap<String, Object> getResponseMap = toHashMap(getResponse);

    assertEquals("world", getResponseMap.get("hello"));
  }
Esempio n. 13
0
 @Test
 public void shouldBeAbleToChangeTheHeaderForResponse() throws Exception {
   Response response = new Response(200, "OK");
   response.setHeader("Connection", "keep-alive");
   response.setHeader("foo", "bar");
   assertEquals("keep-alive", response.getHeader("Connection"));
   assertEquals("bar", response.getHeader("foo"));
   assertEquals("foo: bar\nConnection: keep-alive", response.toHTTPHeaderString());
 }
 @Test
 public void AddBookNoBook() {
   Response response = target("books").request().post(null);
   assertEquals(400, response.getStatus());
 }
 protected HashMap<String, Object> toHashMap(Response response) {
   return (response.readEntity(new GenericType<HashMap<String, Object>>() {}));
 }
Esempio n. 16
0
  @Test
  public void createTransactionReturnsOk() {
    // create "cars" transaction
    Response response =
        client
            .path("/transactionservice/transaction/10")
            .request()
            .put(Entity.json(new Transaction("cars", new BigDecimal(5000), 0l)));

    assertEquals(200, response.getStatus());
    assertEquals(
        TransactionService.OperationResult.OK,
        response.readEntity(TransactionService.OperationResult.class));

    // create "shopping" transaction
    response =
        client
            .path("/transactionservice/transaction/11")
            .request()
            .put(Entity.json(new Transaction("shopping", new BigDecimal(10000), 10l)));

    assertEquals(200, response.getStatus());
    assertEquals(
        TransactionService.OperationResult.OK,
        response.readEntity(TransactionService.OperationResult.class));

    // get "cars" transactions
    response = client.path("/transactionservice/type/cars").request().get();

    assertEquals(200, response.getStatus());
    @SuppressWarnings("unchecked")
    List<Integer> ids = response.readEntity(List.class);
    assertEquals(1, ids.size());
    assertEquals(10, ids.get(0).intValue());

    // get "sum" for transaction 10
    response = client.path("/transactionservice/sum/10").request().get();

    assertEquals(200, response.getStatus());
    AggregatorService.Sum sum = response.readEntity(AggregatorService.Sum.class);
    assertEquals(15000, sum.getSum().intValue());

    // get "sum" for transaction 11
    response = client.path("/transactionservice/sum/11").request().get();

    assertEquals(200, response.getStatus());
    sum = response.readEntity(AggregatorService.Sum.class);
    assertEquals(10000, sum.getSum().intValue());
  }
Esempio n. 17
0
 @Test
 public void shouldReturn200WhenRequestIsOk() {
   Response response = new Request(Verb.GET, "http://www.google.com").send();
   assertEquals(200, response.getCode());
   assertTrue(response.getBody().length() > 0);
 }
Esempio n. 18
0
 @Test
 public void shouldReturn404ForUnexistentUrls() {
   Response response = new Request(Verb.GET, "http://xkcd.com/404").send();
   assertEquals(404, response.getCode());
 }
Esempio n. 19
0
 @Test
 public void shouldFollowRedirects() {
   Response response = new Request(Verb.GET, "http://bit.ly/whatsup").send();
   assertEquals(200, response.getCode());
   assertTrue(response.getBody().length() > 0);
 }
Esempio n. 20
0
 @Test
 public void redirectShouldHaveFullLocationHeader() {
   Response res = Response.redirectTo("/somepath");
   assertThat(res.getHeader("Location"), equalTo("http://test.host:8080/somepath"));
 }