Ifiweranṣẹ yii ṣalaye bii o ṣe le firanṣẹ Awọn ibeere HTTP API ni lilo ile-ikawe ti o ni idaniloju REST. Awọn apẹẹrẹ bo GET
, POST
, PUT
, PATCH
ati DELETE
awọn ibeere.
Ibere HTTP GET ni a lo lati gba orisun lati ọdọ olupin kan.
Apẹẹrẹ atẹle n lo get()
ọna lati ile-ikawe ti o ni idaniloju TI.
Apẹẹrẹ:
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]'));
} }
Lati firanṣẹ awọn ipilẹ ibeere pẹlu ibeere GET, a lo queryParam
ọna:
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]'));
} }
Ti lo HTTP POST ibeere lati firanṣẹ data tabi ṣẹda orisun kan lori olupin kan.
Lati firanṣẹ ibeere POST ni idaniloju-isinmi, a lo post()
ọna:
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'));
} }
Jẹmọ:
Ibeere PUT ṣe imudojuiwọn orisun kan ṣugbọn nilo kikun isanwo JSON.
Lati firanṣẹ ibeere PUT ni idaniloju-isinmi, a lo put()
ọna:
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'));
} }
Ibeere PATCH ṣe imudojuiwọn orisun kan ṣugbọn o nilo aaye (s) nikan eyiti o ti ni imudojuiwọn ni ẹwọn isanwo:
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'));
} }
Jẹmọ:
A lo ibeere PIPẸ lati paarẹ orisun kan lati ọdọ olupin kan.
Lati firanṣẹ ibeere PIPẸ ni idaniloju-isinmi, a lo awọn delete()
ọna:
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());
} }