Ma tenei panui e whakaatu me pehea te tuku API Tono HTTP ma te whakamahi i te wharepukapuka REST-sure. Tauira taupoki GET
, POST
, PUT
, PATCH
me DELETE
tono.
Ko te tono HTTP GET ka whakamahia hei tiki rauemi mai i te kaituku.
Ma te tauira e whai ake nei e whakamahi te get()
tikanga mai i te wharepukapuka PANUI-pono.
Tauira:
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]'));
} }
Hei tuku i nga taatai paatai me te tono GET, ka whakamahia te queryParam
tikanga:
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]'));
} }
Ko te tono HTTP POST ka whakamahia hei tuku tuhinga, hei hanga rauemi ranei ki runga i te kaituku.
Ki te tuku i tetahi tono POST i runga i te ORA-pono, ka whakamahia te post()
tikanga:
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'));
} }
E pā ana:
Ko te tono PUT e whakahou ana i tetahi rauemi engari me tino utaina nga utunga JSON.
Ki te tuku i tetahi tono PUT i runga i te TINO-pono, ka whakamahia te put()
tikanga:
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'));
} }
Ko te tono PATCH he whakahou i tetahi rauemi engari ko nga mara anake e whakahoutia ana i roto i te utunga:
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'));
} }
E pā ana:
Ko te tono MUTU ka whakamahia hei whakakore i tetahi rauemi mai i te kaituku.
Ki te tuku i tetahi tono MUTU me te REST-sure, ka whakamahia te delete()
tikanga:
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());
} }