@Test public void greeting_accessible_with_token() throws Exception { AuthenticationRequest authRequest = new AuthenticationRequest("user", "password"); // @formatter:off Response tokenResponse = given() .contentType("application/json") .body(authRequest) .when() .post("/auth/login") .then() .statusCode(HttpStatus.SC_OK) .extract() .response(); // @formatter:on String token = tokenResponse.getBody().jsonPath().getString("token"); logger.debug("Token: {}", token); // @formatter:off given() .header(new Header("X-Auth-Token", token)) .param("name", "jdoe") .when() .get("/hello") .then() .contentType("application/json") .statusCode(HttpStatus.SC_OK) .body("greeting", equalTo("Hello jdoe")); // @formatter:on }
@Test public void byeResource_ok() { Response response = given().expect().response().statusCode(200).when().get("http://localhost:5050/bye?to=foo"); assertThat(response.getBody().asString()).isEqualToIgnoringCase("bye foo from test"); }
public String createWrestler() { String myJson = wrestlerData.getNewWrestler(); Map<String, String> cookies = authorization(); Response response = given() .contentType(ContentType.JSON) .body(myJson) .with() .cookies(cookies) .post("http://streamtv.net.ua/base/php/wrestler/create.php"); String JSONResponseBody = response.getBody().asString(); JsonPath jsonPath = new JsonPath(JSONResponseBody); return jsonPath.get("id").toString(); }
public void waitForSourcesToBeAvailable(String restPath, String... sources) throws InterruptedException { String path = restPath + "sources"; LOGGER.info("Waiting for sources at {}", path); long timeoutLimit = System.currentTimeMillis() + TimeUnit.MINUTES.toMillis(5); boolean available = false; while (!available) { Response response = get(path); String body = response.getBody().asString(); if (StringUtils.isNotBlank(body)) { available = response.getStatusCode() == 200 && body.length() > 0 && !body.contains("false") && response.getBody().jsonPath().getList("id") != null; if (available) { List<Object> ids = response.getBody().jsonPath().getList("id"); for (String source : sources) { if (!ids.contains(source)) { available = false; } } } } if (!available) { if (System.currentTimeMillis() > timeoutLimit) { response.prettyPrint(); fail("Sources at " + path + " did not start in time."); } Thread.sleep(1000); } } LOGGER.info("Sources at {} ready.", path); }
private void checkContentLengthIsNotFiltered() throws Exception { Response response = given() .header("Accept", "application/json") .expect() .response() .body("name", hasItems("Liga BBVA", "Premiere League")) .header("Content-type", containsString("application/json")) .statusCode(200) .when() .get("/proxy/leagues"); String expectedBody = "[{ \"name\": \"Liga BBVA\" }, { \"name\": \"Premiere League\" }]"; Assert.assertEquals(expectedBody, response.getBody().print()); Assert.assertEquals( String.valueOf(expectedBody.length()), response.getHeaders().getValue("Content-Length")); }
@Test public void testCreateChannelThatIsAlreadyKnown() { Mockito.when(mock.getChannel("channel-123")) .thenReturn(createChannel("X.Y", "http://joyn-bpX.de/bp", "channel-123")); Response response = // given() . // when() .post(serverUrl + "?ccid=channel-123"); assertEquals(200 /* OK */, response.getStatusCode()); assertEquals("http://joyn-bpX.de/bp/channels/channel-123", response.getHeader("Location")); assertEquals("http://joyn-bpX.de/bp/channels/channel-123", response.getBody().asString()); assertEquals("X.Y", response.getHeader("bp")); Mockito.verify(mock).getChannel("channel-123"); Mockito.verifyNoMoreInteractions(mock); }
@Test public void testCreateNewChannel() { Mockito.when(mock.getChannel("channel-123")).thenReturn(null); Mockito.when(mock.createChannel("channel-123", null)) .thenReturn(createChannel("0.0", "http://joyn-bp0.de/bp", "channel-123")); Response response = // given() . // when() .post(serverUrl + "?ccid=channel-123"); assertEquals(201 /* Created */, response.getStatusCode()); assertEquals("http://joyn-bp0.de/bp/channels/channel-123", response.getHeader("Location")); assertEquals("http://joyn-bp0.de/bp/channels/channel-123", response.getBody().asString()); assertEquals("0.0", response.getHeader("bp")); Mockito.verify(mock).getChannel("channel-123"); Mockito.verify(mock).createChannel("channel-123", null); Mockito.verifyNoMoreInteractions(mock); }
@Test public void testSaveAuthor() { // Create author with article AuthorDto authorDto = SampleData.createAuthorWithArticle(1); Response postResponse = RestAssured.given().contentType(ContentType.JSON).body(authorDto).when().post("/authors"); postResponse .then() .statusCode(200) .and() .assertThat() .body("id", notNullValue()) .body("articles", notNullValue()); // Get Author from response AuthorDto responseAuthor = postResponse.getBody().as(AuthorDto.class); if (CollectionUtils.isEmpty(responseAuthor.getArticles())) { Assert.fail("Author.articles should not be empty"); } }
public void waitForHttpEndpoint(String path) throws InterruptedException { LOGGER.info("Waiting for {}", path); long timeoutLimit = System.currentTimeMillis() + HTTP_ENDPOINT_TIMEOUT; boolean available = false; while (!available) { Response response = get(path); available = response.getStatusCode() == 200 && response.getBody().print().length() > 0; if (!available) { if (System.currentTimeMillis() > timeoutLimit) { fail( String.format( "%s did not start within %d minutes.", path, TimeUnit.MILLISECONDS.toMinutes(HTTP_ENDPOINT_TIMEOUT))); } Thread.sleep(100); } } LOGGER.info("{} ready.", path); }
@Then("^I expect to see the response:$") public void I_expect_to_see_the_response(String response) throws Throwable { TestCase.assertEquals(normalize(response), normalize(rs.getBody().print())); }
@Test @ApiAuthentication(mode = "auth") public void testJournalTrackReferenceSupportedMethodsNegative_JournalStep() { final JSONObject testPostInvalid = (JSONObject) JOURNAL_STEP_TESTPOST_FOR_EAL.clone(); final JSONObject testGetInvalid = new JSONObject(); final String invalidJournalStepPath = JOURNAL_TRACK_PATH + "/" + JOURNAL_TRACK_EAL.get("id") + "/journalStep"; final String nonExistentUUID = "70b982b0-68d7-11e3-949a-0800200c9a66"; int checkResultCount = 0; // get /journalStep Response checkItemCount = expect() .statusCode(200) .log() .ifError() .contentType("application/json") .when() .get(invalidJournalStepPath); String result = checkItemCount.getBody().jsonPath().getJsonObject("results").toString(); if (StringUtils.isNotBlank(result)) { checkResultCount = Integer.parseInt(result); } else { LOGGER.error("Get all method failed in Negative Test! No results returned."); fail("GET all failed Negative Tests."); } testGetInvalid.put("success", "true"); testGetInvalid.put("message", ""); testGetInvalid.put("results", 0); testGetInvalid.put("rows", new JSONArray()); // get invalid id /journalTrackJournalStep expect() .statusCode(200) .log() .ifError() .contentType("application/json") .body("", equalTo(testGetInvalid)) .when() .get(JOURNAL_TRACK_PATH + "/" + nonExistentUUID + "/journalTrackJournalStep"); // get invalid id /journalStepJournalTrack expect() .statusCode(404) .contentType("application/json") .when() .get(JOURNAL_TRACK_PATH + "/" + nonExistentUUID); testPostInvalid.put("id", nonExistentUUID); // post unassigned uuid name /journalStep expect() .statusCode(404) .given() .contentType("application/json") .body(testPostInvalid) .when() .post(invalidJournalStepPath); // delete /journalStep expect() .statusCode(404) .given() .contentType("application/json") .body(UUID.fromString(nonExistentUUID)) .when() .delete(invalidJournalStepPath); // get all (verify result # is unchanged) /journalStep expect() .statusCode(200) .log() .ifError() .contentType("application/json") .body("results", equalTo(checkResultCount)) .when() .get(invalidJournalStepPath); }