|
| 1 | +package com.baeldung.restassured; |
| 2 | + |
| 3 | +import static io.restassured.RestAssured.given; |
| 4 | +import static io.restassured.RestAssured.when; |
| 5 | +import static org.hamcrest.Matchers.greaterThan; |
| 6 | +import static org.hamcrest.Matchers.is; |
| 7 | +import static org.hamcrest.Matchers.lessThan; |
| 8 | +import static org.junit.Assert.assertEquals; |
| 9 | +import io.restassured.RestAssured; |
| 10 | +import io.restassured.http.Cookie; |
| 11 | +import io.restassured.response.Response; |
| 12 | + |
| 13 | +import java.util.concurrent.TimeUnit; |
| 14 | + |
| 15 | +import org.junit.Before; |
| 16 | +import org.junit.Test; |
| 17 | + |
| 18 | +public class RestAssuredAdvancedLiveTest { |
| 19 | + |
| 20 | + @Before |
| 21 | + public void setup(){ |
| 22 | + RestAssured.baseURI = "https://api.github.com"; |
| 23 | + RestAssured.port = 443; |
| 24 | + } |
| 25 | + |
| 26 | + @Test |
| 27 | + public void whenMeasureResponseTime_thenOK(){ |
| 28 | + Response response = RestAssured.get("/users/eugenp"); |
| 29 | + long timeInMS = response.time(); |
| 30 | + long timeInS = response.timeIn(TimeUnit.SECONDS); |
| 31 | + |
| 32 | + assertEquals(timeInS, timeInMS/1000); |
| 33 | + } |
| 34 | + |
| 35 | + @Test |
| 36 | + public void whenValidateResponseTime_thenSuccess(){ |
| 37 | + when().get("/users/eugenp").then().time(lessThan(5000L)); |
| 38 | + } |
| 39 | + |
| 40 | + @Test |
| 41 | + public void whenValidateResponseTimeInSeconds_thenSuccess(){ |
| 42 | + when().get("/users/eugenp").then().time(lessThan(5L),TimeUnit.SECONDS); |
| 43 | + } |
| 44 | + |
| 45 | + //===== parameter |
| 46 | + |
| 47 | + @Test |
| 48 | + public void whenUseQueryParam_thenOK(){ |
| 49 | + given().queryParam("q", "john").when().get("/search/users").then().statusCode(200); |
| 50 | + given().param("q", "john").when().get("/search/users").then().statusCode(200); |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + public void whenUseMultipleQueryParam_thenOK(){ |
| 55 | + int perPage = 20; |
| 56 | + given().queryParam("q", "john").queryParam("per_page",perPage).when().get("/search/users").then().body("items.size()", is(perPage)); |
| 57 | + given().queryParams("q", "john","per_page",perPage).when().get("/search/users").then().body("items.size()", is(perPage)); |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + public void whenUseFormParam_thenSuccess(){ |
| 62 | + given().log().all().formParams("username", "john","password","1234").post("/"); |
| 63 | + given().log().all().params("username", "john","password","1234").post("/"); |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + public void whenUsePathParam_thenOK(){ |
| 68 | + given().pathParam("user", "eugenp").when().get("/users/{user}/repos").then().log().all().statusCode(200); |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + public void whenUseMultiplePathParam_thenOK(){ |
| 73 | + given().log().all().pathParams("owner", "eugenp","repo","tutorials").when().get("/repos/{owner}/{repo}").then().statusCode(200); |
| 74 | + given().log().all().pathParams("owner", "eugenp").when().get("/repos/{owner}/{repo}","tutorials").then().statusCode(200); |
| 75 | + } |
| 76 | + |
| 77 | + //===== header |
| 78 | + |
| 79 | + @Test |
| 80 | + public void whenUseCustomHeader_thenOK(){ |
| 81 | + given().header("User-Agent", "MyAppName").when().get("/users/eugenp").then().statusCode(200); |
| 82 | + } |
| 83 | + |
| 84 | + @Test |
| 85 | + public void whenUseMultipleHeaders_thenOK(){ |
| 86 | + given().header("User-Agent", "MyAppName","Accept-Charset","utf-8").when().get("/users/eugenp").then().statusCode(200); |
| 87 | + } |
| 88 | + |
| 89 | + //======= cookie |
| 90 | + |
| 91 | + @Test |
| 92 | + public void whenUseCookie_thenOK(){ |
| 93 | + given().cookie("session_id", "1234").when().get("/users/eugenp").then().statusCode(200); |
| 94 | + } |
| 95 | + |
| 96 | + @Test |
| 97 | + public void whenUseCookieBuilder_thenOK(){ |
| 98 | + Cookie myCookie = new Cookie.Builder("session_id", "1234").setSecured(true).setComment("session id cookie").build(); |
| 99 | + given().cookie(myCookie).when().get("/users/eugenp").then().statusCode(200); |
| 100 | + } |
| 101 | + |
| 102 | + // ====== request |
| 103 | + |
| 104 | + @Test |
| 105 | + public void whenRequestGet_thenOK(){ |
| 106 | + when().request("GET", "/users/eugenp").then().statusCode(200); |
| 107 | + } |
| 108 | + |
| 109 | + @Test |
| 110 | + public void whenRequestHead_thenOK(){ |
| 111 | + when().request("HEAD", "/users/eugenp").then().statusCode(200); |
| 112 | + } |
| 113 | + |
| 114 | + //======= log |
| 115 | + |
| 116 | + @Test |
| 117 | + public void whenLogRequest_thenOK(){ |
| 118 | + given().log().all().when().get("/users/eugenp").then().statusCode(200); |
| 119 | + } |
| 120 | + |
| 121 | + @Test |
| 122 | + public void whenLogResponse_thenOK(){ |
| 123 | + when().get("/repos/eugenp/tutorials").then().log().body().statusCode(200); |
| 124 | + } |
| 125 | + |
| 126 | + @Test |
| 127 | + public void whenLogResponseIfErrorOccurred_thenSuccess(){ |
| 128 | + when().get("/users/eugenp").then().log().ifError(); |
| 129 | + when().get("/users/eugenp").then().log().ifStatusCodeIsEqualTo(500); |
| 130 | + when().get("/users/eugenp").then().log().ifStatusCodeMatches(greaterThan(200)); |
| 131 | + } |
| 132 | + |
| 133 | + @Test |
| 134 | + public void whenLogOnlyIfValidationFailed_thenSuccess(){ |
| 135 | + when().get("/users/eugenp").then().log().ifValidationFails().statusCode(200); |
| 136 | + given().log().ifValidationFails().when().get("/users/eugenp").then().statusCode(200); |
| 137 | + } |
| 138 | + |
| 139 | +} |
0 commit comments