Maitiro Ekutumira Basic Yekusimbisa Musoro muREST-yakavimbiswa

Paunenge uchiita kuyedza kweAPI, dzimwe nguva maAPI kana magumo anochengetedzwa. Izvi zvinoreva kuti iwe unofanirwa kuve wakasimbiswa uye kupihwa mvumo yekuita zvimwe zviito.

Kune nzira dzakawanda dzekuchengetedza APIs, asi imwe nzira yakapusa ndeye kushandisa Basic Kusimbiswa.

Mune ino posvo, isu tichatarisa maitiro ekutumira Basic | munyori musoro mune REST-akavimbiswa.

Kutumira Basic Auth Musoro mune REST-yakavimbiswa

import io.restassured.RestAssured; import io.restassured.http.ContentType; import io.restassured.response.Response; import org.junit.Before; import org.junit.jupiter.api.*; import static io.restassured.RestAssured.given; public class UserScenarios {
private String path;
private String validRequest = '{ ' +

' 'username': 'some-user', ' +

' 'email': 'some-user@email.com', ' +

' 'password': 'Passw0rd123!' }';
@Before
public void setup() {
RestAssured.baseURI = 'http://localhost:8080';
path = '/users';
}
@Test
public void createUser() {
Response response = given()


.auth()


.preemptive()


.basic('required_username', 'required_password')


.header('Accept', ContentType.JSON.getAcceptHeader())


.contentType(ContentType.JSON)


.body(validRequest)


.post(path)


.then().extract().response();

Assertions.assertEquals(201, response.getStatusCode());
}

Mune zvimwe zviitiko, sevha ingangoshandisa nzira yekushora-mhinduro kuratidza kana mushandisi achida kuratidza kuti awane zviwanikwa

Nekutadza, REST-yakavimbiswa inomirira kuti sevha ipikise isati yatumira magwaro uye saka raibhurari inopa rairo yekutanga yatinogona kushandisa:

given()
.auth()
.preemptive()
.basic('required_username', 'required_password')