ਇਹ ਪੋਸਟ ਦੱਸਦੀ ਹੈ ਕਿ REST- ਭਰੋਸਾ ਵਾਲੀ ਲਾਇਬ੍ਰੇਰੀ ਦੀ ਵਰਤੋਂ ਕਰਦੇ ਹੋਏ API HTTP ਬੇਨਤੀਆਂ ਨੂੰ ਕਿਵੇਂ ਭੇਜਣਾ ਹੈ. ਉਦਾਹਰਣਾਂ ਦੇ ਕਵਰ GET
, POST
, PUT
, PATCH
ਅਤੇ DELETE
ਬੇਨਤੀਆਂ.
HTTP GET ਬੇਨਤੀ ਸਰਵਰ ਤੋਂ ਸਰੋਤ ਲਿਆਉਣ ਲਈ ਵਰਤੀ ਜਾਂਦੀ ਹੈ.
ਹੇਠ ਦਿੱਤੀ ਉਦਾਹਰਨ | _ _ _ _ | ਵਰਤਦੀ ਹੈ REST- ਭਰੋਸਾ ਵਾਲੀ ਲਾਇਬ੍ਰੇਰੀ ਤੋਂ methodੰਗ.
ਉਦਾਹਰਣ:
get()
ਜੀ.ਈ.ਟੀ. ਬੇਨਤੀ ਦੇ ਨਾਲ ਕਿ queryਰੀ ਪੈਰਾਮੀਟਰ ਭੇਜਣ ਲਈ, ਅਸੀਂ | _ _ _ _ | ਵਿਧੀ:
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]'));
} }
HTTP POST ਬੇਨਤੀ ਡੇਟਾ ਪੋਸਟ ਕਰਨ ਜਾਂ ਸਰਵਰ ਤੇ ਇੱਕ ਸਰੋਤ ਬਣਾਉਣ ਲਈ ਵਰਤੀ ਜਾਂਦੀ ਹੈ.
ਰੈਸਟ-ਭਰੋਸੇ ਵਿੱਚ ਇੱਕ ਪੋਸਟ ਬੇਨਤੀ ਭੇਜਣ ਲਈ, ਅਸੀਂ | _ _ _ _ | ਵਰਤਦੇ ਹਾਂ ਵਿਧੀ:
queryParam
ਸੰਬੰਧਿਤ:
PUT ਬੇਨਤੀ ਇੱਕ ਸਰੋਤ ਨੂੰ ਅਪਡੇਟ ਕਰਦੀ ਹੈ ਪਰ ਪੂਰਾ JSON ਪੇਲੋਡ ਲੋੜੀਂਦਾ ਹੈ.
ਆਰਾਮ-ਭਰੋਸੇ ਵਿੱਚ ਇੱਕ PUT ਬੇਨਤੀ ਭੇਜਣ ਲਈ, ਅਸੀਂ | _ _ _ _ | ਵਰਤਦੇ ਹਾਂ ਵਿਧੀ:
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()
ਸੰਬੰਧਿਤ:
ਡਿਲੀਟ ਬੇਨਤੀ ਸਰਵਰ ਤੋਂ ਸਰੋਤ ਨੂੰ ਮਿਟਾਉਣ ਲਈ ਵਰਤੀ ਜਾਂਦੀ ਹੈ.
ਆਰਾਮ-ਭਰੋਸੇ ਵਿੱਚ ਇੱਕ ਡੀਲੀਟ ਬੇਨਤੀ ਭੇਜਣ ਲਈ, ਅਸੀਂ | _ _ _ _ | ਵਰਤਦੇ ਹਾਂ ਵਿਧੀ:
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'));
} }