Nuair a bhios tu a ’dèanamh deuchainn API, uaireannan bidh na APIan no na endpoints air an dìon. Tha seo a ’ciallachadh gum feum thu a bhith air do dhearbhadh agus ùghdarras gus gnìomhan sònraichte a choileanadh.
Tha iomadh dòigh ann air APIan a dhìon, ach is e aon dòigh sìmplidh air an Basic
a chleachdadh Dearbhadh.
Anns an dreuchd seo, bidh sinn a ’coimhead air mar a chuireas tu an Basic
header ùghdar ann an REST-sure.
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());
}
Ann an cuid de chùisean, dh ’fhaodadh frithealaiche uidheamachd freagairt-dùbhlain a chleachdadh gus comharrachadh cuin a dh’ fheumas an neach-cleachdaidh dearbhadh gus faighinn chun ghoireas.
Gu gnàthach, bidh REST-cinnteach a ’feitheamh ris an t-seirbheisiche dùbhlan a chuir mus cuir iad na teisteanasan agus mar sin tha an leabharlann a’ toirt seachad an stiùireadh preemptive as urrainn dhuinn a chleachdadh:
given()
.auth()
.preemptive()
.basic('required_username', 'required_password')