HVILDAN fullviss dæmi: FÁ, POST, PUT, PATCH, DELETE

Þessi færsla útskýrir hvernig á að senda API HTTP beiðnir með REST-fullvissu bókasafni. Dæmi ná yfir GET, POST, PUT, PATCH og DELETE beiðnir.



RÚSTVARNAR HTTP API beiðnir

FÁ beiðni

HTTP GET beiðnin er notuð til að sækja auðlind frá netþjóni.

Eftirfarandi dæmi notar get() aðferð frá REST-tryggðu bókasafni.


Dæmi:

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

FÁÐU beiðni með fyrirspurnapörum

Til að senda fyrirspurnarbreytur ásamt GET beiðninni notum við queryParam aðferð:


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 beiðni

HTTP POST beiðni er notuð til að senda gögn eða búa til auðlind á netþjóninum.



Til að senda POST beiðni í REST-fullvissu notum við post() aðferð:

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

Tengt:

PUT Beiðni

PUT beiðnin uppfærir auðlind en krefst fulls JSON álags.


Til að senda PUT beiðni í REST-fullvissu notum við put() aðferð:

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 Beiðni

PATCH beiðnin uppfærir auðlind en krefst eingöngu reitanna / svæðanna sem verið er að uppfæra í farminum:

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

Tengt:

EYÐA beiðni

DELETE beiðnin er notuð til að eyða auðlind af netþjóni.


Til að senda DELETE beiðni með REST-fullvissu notum við delete() aðferð:

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