Basics - aivancioglo/RestTest GitHub Wiki

Read this after getting started guide.

Contents

Simple HTTP request

Execute a simple request using the Endpoint class:

import com.github.aivancioglo.resttest.http.Endpoint;
new Endpoint().get("google.com");

Or make a static request using RestTest class:

import com.github.aivancioglo.resttest.http.RestTest;
RestTest.get("google.com");

NOTE: The http protocol is used by default for each request. If you want to use any other protocol, make a request using the desired one. For example, request with https protocol will look like: get("https://google.com"). You can use other request methods like POST, PATCH, DELETE etc.:

post("test.com");
patch("test.com");
delete("test.com");
...

You can save your response in a variable of RestTest Response data type.

import com.github.aivancioglo.resttest.http.Response;
import com.github.aivancioglo.resttest.http.RestTest.get;
Response response = get("google.com");

Setters

Use setters to make more complex requests. For example, you can adjust your request by setting headers, content type, query parameters or any RestTest setters to your request:

import com.github.aivancioglo.resttest.http.Response;
import com.github.aivancioglo.resttest.http.RestTest.get;

import static io.restassured.http.ContentType.JSON;
import static com.github.aivancioglo.resttest.setters.Setters.header;
import static com.github.aivancioglo.resttest.setters.Setters.contentType;
import static com.github.aivancioglo.resttest.setters.Setters.queryParam;
Response response = get("google.com",
            header("name", "value"),
            contentType(JSON),
            queryParam("key", "value"));

RestTest setters:

Setter Class Description
param Setters Parameter that will be sent with the request
queryParam Setters Query parameter that will be sent with the request
jsonParam Setters Json parameter that will be sent with the request as body parameter
formParam Setters Form parameter that will be sent with the request
contentType Setters Content type of your request
cookie Setters Cookie that will be sent with the request
header Setters Header that will be sent with the request
body Setters Body that will be sent with the request
protocol Setters Protocol of your request. It can override your previous protocol
host Setters Host of your request. It can override your previous host
path Setters Path of your request. It can override your previous path
port Setters Port of your request. It can override your previous port
pathParam Setters Path parameters are used to improve readability of the request path.Can be used multiple times
multiPart Setters Use this to upload your multi-part value
redirect Setters Setting redirection property
accept Setters Specifies the accept header of the request
urlEncodingEnabled Setters Defining if url encoding is enabled. By default false
basicAuth Setters HTTP basic authentication
oauth1 Setters Setting all OAuth 1.0 authentication settings.
oauth2 Setters Setting all OAuth 2.0 authentication settings.
noAuth Setters Clearing all authentication settings
consumerKey OAuth1 Setting consumer key for OAuth 1.0
consumerSecret OAuth1 Setting consumer secret for OAuth 1.0
token OAuth1 Setting token for OAuth 1.0
tokenSecret OAuth1 Setting token secret for OAuth 1.0
token OAuth2 Setting token for OAuth 2.0
baseUri Setters Set baseUri of request
parser Setters Register a content-type to be parsed using a predefined parser
defaultParser Setters Register a default predefined parser that will be used if no other parser (registered or pre-defined) matches the response content-type

Assertions

Assert your response data using assertThat(...) method:

import com.github.aivancioglo.resttest.http.Response;
import com.github.aivancioglo.resttest.http.RestTest.get;

import static com.github.aivancioglo.resttest.http.StatusCode.OK;
Response response = get("google.com");

response.assertThat(OK);

Use JSON Schemas to validate your response:

response.assertThat(OK, "resources/yourSchema.json")

JSON schema validation is very important part of API testing. Read more about JSON schema to understand all capabilities.

Verifiers

Use Verifiers for more flexible assertions and testing. Example of how verifiers are used:

import com.github.aivancioglo.resttest.http.Response;
import com.github.aivancioglo.resttest.http.RestTest.get;

import static com.github.aivancioglo.resttest.http.StatusCode.OK;
import static com.github.aivancioglo.resttest.verifiers.Verifiers.path;
import static com.github.aivancioglo.resttest.verifiers.Verifiers.body;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.containsString;
Response response = get("google.com");

response.assertThat(OK, "yourSchema.json",
            path("This", is("Sparta")),
            body(containsString("Sparta")));

RestTest verifiers:

Verifier Class Description
statusCode Verifiers Verifies the status code of the response
jsonSchema Verifiers Verifies the response body using JSON schema validation
path Verifiers Verifies the response body path
body Verifiers Verifies the response body
contentTypeIs Verifiers Verifies response body content type
cookie Setters Verifies your response cookie
header Verifiers Verifies if response header exists
headers Verifiers Verifies if response headers exist
time Verifiers Verify the response time (in milliseconds)

Logging

Logging is helpful when you need to know the request and response details. Example:

import com.github.aivancioglo.resttest.http.RestTest.get;
get("google.com").log(); // same as .log(LogType.ALL)

The log() method will print your response and request. By default LogType is equal to ALL, which means that all the request and response details will be printed. But, you can also log only request or only response details. Example:

import com.github.aivancioglo.resttest.http.RestTest.get;

import static com.github.aivancioglo.resttest.logger.LogType.REQUEST;
import static com.github.aivancioglo.resttest.logger.LogType.RESPONSE;
get("google.com").log(REQUEST);
get("google.com").log(RESPONSE);

The log() method can be called after assertions:

response.assertThat(OK).log();

By default, your response and request will be printed if one of the assertions fail. This can be changed in the RestTest property file.

Repeater

The Repeater functionality will rerun your failed test until it is passed. Example:

import static com.github.aivancioglo.resttest.http.RestTest.*;
repeat()
      .timeout(5)   // 5 sec
      .every(2.5)   // 2 sec 500 mls
      .tries(10)    // max 10 tries
      .until(() -> get("google.com").assertThat(OK));

// same as:

repeat(5, 2.5, 10, () -> get("google.com").assertThat(OK));

NOTE: Methods every(...) and timeout(...) are overloaded and receive values only in seconds. Almost all of the repeater methods are optional.

Example:

repeat()
    .until(() -> get("google.com").assertThat(OK));

// same as:

repeat(() -> get("google.com").assertThat(OK));

RestTest Properties

All possible settings of RestTest should be setted in property file. Create resttest.properties in your resources. You can set the following properties:

Property Type Default Description
log_if_failed boolean true Log response/request if any assert is failed
log_all boolean false Log all responses/requests even everything is passed
soft_assertions boolean false false - test execution will stop on first failure, true - all asserts will be executed
use_relaxed_https_validation boolean true SSL sertificat validation
connection_timeout integer 20000 Connection timeout in mls
socket_timeout integer 60000 Socket timeout in mls
content_type string application/json Default content type of all your requests. Example: text/plain, application/xml, etc...
protocol string http Default protocol for all requests
host string (none) Default host for all requests
decoder_charset string (none) Specifying the default charset of the content in the response that's assumed if no charset is explicitly specified in the response
encoder_charset string (none) Specifying the default charset for the body/content in your request
baseUri string (none) Default baseUri for all requests