Šiame įraše paaiškinta, kaip siųsti API HTTP užklausas naudojant „REST“ užtikrintą biblioteką. Pavyzdžiai apima GET
, POST
, PUT
, PATCH
ir DELETE
prašymus.
HTTP GET užklausa naudojama norint gauti išteklių iš serverio.
Šiame pavyzdyje naudojamas get()
metodas iš „REST“ užtikrintos bibliotekos.
Pavyzdys:
import io.restassured.RestAssured; import io.restassured.http.ContentType; import io.restassured.response.Response; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; import static io.restassured.RestAssured.given; public class RestAssuredRequests {
@BeforeAll
public static void setup() {
RestAssured.baseURI = 'https://jsonplaceholder.typicode.com';
}
@Test
public void getRequest() {
Response response = given()
.contentType(ContentType.JSON)
.when()
.get('/posts')
.then()
.extract().response();
Assertions.assertEquals(200, response.statusCode());
Assertions.assertEquals('qui est esse', response.jsonPath().getString('title[1]'));
} }
Norėdami siųsti užklausos parametrus kartu su GET užklausa, naudojame queryParam
metodas:
import io.restassured.RestAssured; import io.restassured.http.ContentType; import io.restassured.response.Response; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; import static io.restassured.RestAssured.given; public class RestAssuredRequests {
@BeforeAll
public static void setup() {
RestAssured.baseURI = 'https://jsonplaceholder.typicode.com';
}
@Test
public void getRequestWithQueryParam() {
Response response = given()
.contentType(ContentType.JSON)
.param('postId', '2')
.when()
.get('/comments')
.then()
.extract().response();
Assertions.assertEquals(200, response.statusCode());
Assertions.assertEquals('Meghan_Littel@rene.us', response.jsonPath().getString('email[3]'));
} }
HTTP POST užklausa naudojama duomenims skelbti arba ištekliui sukurti serveryje.
Norėdami išsiųsti POST užklausą „REST-assured“, naudojame post()
metodas:
import io.restassured.RestAssured; import io.restassured.response.Response; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; import static io.restassured.RestAssured.given; public class RestAssuredRequests {
private static String requestBody = '{
' +
' 'title': 'foo',
' +
' 'body': 'bar',
' +
' 'userId': '1'
}';
@BeforeAll
public static void setup() {
RestAssured.baseURI = 'https://jsonplaceholder.typicode.com';
}
@Test
public void postRequest() {
Response response = given()
.header('Content-type', 'application/json')
.and()
.body(requestBody)
.when()
.post('/posts')
.then()
.extract().response();
Assertions.assertEquals(201, response.statusCode());
Assertions.assertEquals('foo', response.jsonPath().getString('title'));
Assertions.assertEquals('bar', response.jsonPath().getString('body'));
Assertions.assertEquals('1', response.jsonPath().getString('userId'));
Assertions.assertEquals('101', response.jsonPath().getString('id'));
} }
Susijęs:
PUT užklausa atnaujina išteklius, tačiau reikalinga visa JSON naudingoji apkrova.
Norėdami išsiųsti PUT užklausą „REST-assured“, naudojame put()
metodas:
import io.restassured.RestAssured; import io.restassured.response.Response; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; import static io.restassured.RestAssured.given; public class RestAssuredRequests {
private static String requestBody = '{
' +
' 'title': 'foo',
' +
' 'body': 'baz',
' +
' 'userId': '1',
' +
' 'id': '1'
}';
@BeforeAll
public static void setup() {
RestAssured.baseURI = 'https://jsonplaceholder.typicode.com';
}
@Test
public void putRequest() {
Response response = given()
.header('Content-type', 'application/json')
.and()
.body(requestBody)
.when()
.put('/posts/1')
.then()
.extract().response();
Assertions.assertEquals(200, response.statusCode());
Assertions.assertEquals('foo', response.jsonPath().getString('title'));
Assertions.assertEquals('baz', response.jsonPath().getString('body'));
Assertions.assertEquals('1', response.jsonPath().getString('userId'));
Assertions.assertEquals('1', response.jsonPath().getString('id'));
} }
PATCH užklausa atnaujina šaltinį, tačiau reikalingas tik tas laukas (-ai), kurie yra atnaujinami naudingojoje apkrovoje:
import io.restassured.RestAssured; import io.restassured.response.Response; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; import static io.restassured.RestAssured.given; public class RestAssuredRequests {
private static String requestBody = '{
' +
' 'title': 'bax'
}';
@BeforeAll
public static void setup() {
RestAssured.baseURI = 'https://jsonplaceholder.typicode.com';
}
@Test
public void patchRequest() {
Response response = given()
.header('Content-type', 'application/json')
.and()
.body(requestBody)
.when()
.patch('/posts/1')
.then()
.extract().response();
Assertions.assertEquals(200, response.statusCode());
Assertions.assertEquals('bax', response.jsonPath().getString('title'));
Assertions.assertEquals('1', response.jsonPath().getString('userId'));
Assertions.assertEquals('1', response.jsonPath().getString('id'));
} }
Susijęs:
„DELETE“ užklausa naudojama ištrinant išteklius iš serverio.
Norėdami išsiųsti „DELETE“ užklausą „REST-assured“, naudojame delete()
metodas:
import io.restassured.RestAssured; import io.restassured.response.Response; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; import static io.restassured.RestAssured.given; public class RestAssuredRequests {
@BeforeAll
public static void setup() {
RestAssured.baseURI = 'https://jsonplaceholder.typicode.com';
}
@Test
public void deleteRequest() {
Response response = given()
.header('Content-type', 'application/json')
.when()
.delete('/posts/1')
.then()
.extract().response();
Assertions.assertEquals(200, response.statusCode());
} }