Намунаҳои боэътимод: ГИРИФТАН, ПОЙДОШТ, ГУЗОШТАН, ДАРОМАД, НЕСТ кардан

Ин паём мефаҳмонад, ки чӣ гуна фиристодани API HTTP дархостҳоро бо истифодаи китобхонаи боэътимод таъмин кунед. Намунаҳо фаро мегиранд GET, POST, PUT, PATCH ва DELETE дархостҳо.



Дархостҳои HTTP API-и боқимонда

Дархостро гиред

Дархости HTTP GET барои гирифтани сервер аз сервер истифода мешавад.

Мисоли зерин бо истифода аз get() метод аз китобхонаи боэътимод.


Мисол:

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]'));
} }

Дархостро бо параметрҳои дархост гиред

Барои фиристодани параметрҳои дархост дар якҷоягӣ бо дархости GET, мо queryParam -ро истифода мебарем усул:


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]'));
} }

Дархости POST

Дархости HTTP POST барои фиристодани маълумот ё эҷоди манбаъ дар сервер истифода мешавад.

Барои фиристодани дархости POST бо эътимоди REST, мо бо истифода аз post() усул:

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'));
} }

Марбут:

Дархостро гузоред

Дархости PUT як манбаъро навсозӣ мекунад, аммо бори пурраи JSON-ро талаб мекунад.


Барои фиристодани дархости PUT бо эътимоди REST, мо бо истифода аз put() усул:

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

Дархости PATCH як манбаъро навсозӣ мекунад, аммо танҳо соҳаҳоеро талаб мекунад, ки дар бори нав таҷдид мешаванд:

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'));
} }

Марбут:

Дархостро нест кунед

Дархости DELETE барои нест кардани манбаъ аз сервер истифода мешавад.


Барои фиристодани дархости ҲУРД бо боварии REST, мо delete() -ро истифода мебарем усул:

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());
} }