Rest-assured basic usage

Quoted from Rest-assured official website, Testing and validating REST services in Java is harder than in dynamic languages such as Ruby and Groovy. REST Assured brings the simplicity of using these languages into the Java domain.

First of all, add dependencies into mvn pom.xml. The first dependency is the basic one, and the second is for json data processing.

  

io.rest-assured  

rest-assured  

3.2.0  

  

  

io.rest-assured  

json-path  

3.2.0  

  

io.rest-assured

rest-assured

3.2.0

io.rest-assured

json-path

3.2.0

There are two way access uri to RestAssured for calling service. Usercan use the code below to set base uri for global request, so that user can just give partial uri to send request.

The static method given() will return an object of RequestSpecification type, it will contain all global setting for request.

RestAssured.baseURI="http://localhost:8089";  

RequestSpecification uncompletedRequest=given();//here just define a object to accept the return of given()  

RestAssured.baseURI="http://localhost:8089";

RequestSpecification uncompletedRequest=given();//here just define a object to accept the return of given()

For authentication, Rest-assured provide several way to do it. The basic way is shown as below, and can define authentication for all requests also.

given().auth().basic("username", "password"); //will return an object of RequestSpecification type for chain call  

RestAssured.authentication = basic("username", "password");//for all request  

given().auth().basic("username", "password"); //will return an object of RequestSpecification type for chain call

RestAssured.authentication = basic("username", "password");//for all request

(we use preemptive basic authentication for jira api authentication.)

RestAssured.authentication=preemptive().basic(username,password);  

RestAssured.authentication=preemptive().basic(username,password);

If the service needs ssl certification, the code below is necessary.

RestAssured.useRelaxedHttpsValidation();  

RestAssured.useRelaxedHttpsValidation();

Then there are several optional steps preparing for send a request.

//easy to know that this to set contenttype  

uncompletedRequest = uncompletedRequest.contentType(contentType);  

// this is to set path parameters with a map object following the pattern given in uri  

// for example: uri is http://localhost:8089/greeting?name={name}, the pathParams must be {'name': 'Zhang, San'}  

// pathParams has more or less params than the uri pattern given is not allowed.  

uncompletedRequest = uncompletedRequest.pathParams(pathParams);//there is also method pathParam() to set single key-value param.  

//in this way, all key-value pair will be added into path as params  

uncompletedRequest = uncompletedRequest.params(params);////there is also method param() to set single key-value param.  

// as the method shown, this method is to set request body for post and put request.  

uncompletedRequest = uncompletedRequest.body(body);  

//easy to know that this to set contenttype

uncompletedRequest = uncompletedRequest.contentType(contentType);

// this is to set path parameters with a map object following the pattern given in uri

// for example: uri is http://localhost:8089/greeting?name={name}, the pathParams must be {'name': 'Zhang, San'}

// pathParams has more or less params than the uri pattern given is not allowed.

uncompletedRequest = uncompletedRequest.pathParams(pathParams); //there is also method pathParam() to set single key-value param.

//in this way, all key-value pair will be added into path as params

uncompletedRequest = uncompletedRequest.params(params);////there is also method param() to set single key-value param.

// as the method shown, this method is to set request body for post and put request.

uncompletedRequest = uncompletedRequest.body(body);

Next step is to send request.

// four method relevant to for request type: get, post, put and delete.  

// get response for data processing.  

Response response=uncompletedRequest.get(uri);//if BaseURI is set. here we can pass partial uri like "/greeting"  

// four method relevant to for request type: get, post, put and delete.

// get response for data processing.

Response response=uncompletedRequest.get(uri);//if BaseURI is set. here we can pass partial uri like "/greeting"

Rest-assured provide simple way to do data validation.

//{"lotto":{"lottoId":5,"name":"Li, Si"}} just for simple example  

response.then().statusCode(200).body("lotto.lottoId");  

//{"lotto":{"lottoId":5,"name":"Li, Si"}} just for simple example

response.then().statusCode(200).body("lotto.lottoId");


Also we can get many details by calling methods of Response

response.getStatusCode();  

response.getBody();  

response.getCookies();  

response.getBody().jsonPath();//for some un-standard service, this is a good way to analyze the received json data.  

response.getStatusCode();

response.getBody();

response.getCookies();

response.getBody().jsonPath(); //for some un-standard service, this is a good way to analyze the received json data.


For more details, please refer tohttps://github.com/rest-assured/rest-assured/wiki/Usage

你可能感兴趣的:(Rest-assured basic usage)