Easy REST API testing library
REST + TESTING = RESTING
Resting
is a simple and light weight library for accessing REST API with little fuss. You can use Resting
as a REST client for consuming REST services in your application or You can use it for end-to-end testing of your REST services. The main features are:
- Simple and Easy to use
- Fluent API
- Convention over Configuration
- Secured API call support(Basic Auth + Token)
- Request builder and JSON File payload support
- State-less and State-full(Cookie) Service invocation
- REST JSON and XML Service support
- BDD Style Fluent API
- Less code, Less Bug
Resting
is a java library, So you can download the resting-x.x.x.jar
and directly consume in your application however we are recommending to use a build system like Maven or Gradle. Please refer to below sections based on your build system:
-
Maven
You can add below dependency to your
pom.xml
:<dependency> <groupId>com.anitechcs</groupId> <artifactId>resting</artifactId> <version>0.0.1</version> </dependency>
-
Gradle
You can add below dependency to your
build.gradle
file:dependencies { compile group: 'com.anitechcs', name: 'resting', version: '0.0.1' } Or in Short dependencies { compile 'com.anitechcs:resting:0.0.1' }
-
Custom (Not recommended)
Download
resting-0.0.1-SNAPSHOT.jar
and add to your classpath along with following dependent library jars:- JUnit
- Log4J2
- Apache HTTP Client
- Json-Simple
Once you have resting in your classpath (Refer #Installation section), you are ready to take it for a spin. You need to get a handle to Resting
instance first:
Resting resting = Resting.getInstance();
Also you can configure few things globally with provided fluent API like below:
Resting resting = Resting
.getInstance()
.enableMetrics() //enable metrics
.enableProcessingHooks() //enables pre and post processing hooks
.globalRequestConfig(globalConfig) //global request configuration
.baseURI("https://jsonplaceholder.typicode.com"); //set base URL
Enjoy calling your REST services through Resting
with less code and easier API:
// Header array
Header[] headers = {
new Header("Content-Type", "application/json"),
new Header("Accept", "application/xml,text/plain,application/json"),
new Header("Connection", "keep-alive")
};
// Single Header
Header header = new Header("Content-Type", "application/json");
RequestConfig config = new RequestConfig();
config.setConnectTimeout(5000);
config.setSocketTimeout(5000);
config.setHeaders(headers);
config.addHeader(header);
config.addHeader(new Header("Content-Type", "application/json"));
config.addHeader("Content-Type", "application/json");
// GET Without Extra Config
RestingResponse res = resting.GET("/posts/1");
JSONObject json = (JSONObject) res.getBody();
// GET Without Extra Config - Multiple records
RestingResponse res = resting.GET("/posts");
JSONArray jsonArr = (JSONArray) res.getBody();
// GET With Extra Config
RestingResponse res = resting.GET("/posts/1", config);
JSONObject json = (JSONObject) res.getBody();
// POST Without Extra Config
RestingResponse res = resting.POST("/posts", inputs);
JSONObject json = (JSONObject) res.getBody();
// POST With Extra Config
RestingResponse res = resting.POST("/posts", inputs, config);
JSONObject json = (JSONObject) res.getBody();
// PUT Without Extra Config
RestingResponse res = resting.PUT("/posts/1", inputs);
JSONObject json = (JSONObject) res.getBody();
// PUT With Extra Config
RestingResponse res = resting.PUT("/posts/1", inputs, config);
JSONObject json = (JSONObject) res.getBody();
// DELETE Without Extra Config
RestingResponse res = resting.DELETE("/posts/1");
JSONObject json = (JSONObject) res.getBody();
// DELETE With Extra Config
RestingResponse res = resting.DELETE("/posts/1", config);
JSONObject json = (JSONObject) res.getBody();
For POST
and PUT
services you need to pass payload to the service. You can see syntax wise we are accepting Object
type which means you can pass your data in any supported format below and Resting
will take care of the rest.
- Map
- JSON String (e.g. {"name": "Tapas", "country": "India"})
- JSONObject
- StringBuffer
- StringBuilder
- File (e.g. new File("/data/input.json"))
- InputStream
- FileInputStream
If you still need more input format support, Please let us know!
Make sure you are importing
RequestConfig
andHeader
fromResting
library instead of Apache HTTP library
We really appriciate your feedback and looking forward to make Resting
better and better. Please raise a defect or pull request if you want to discuss anything with us regarding Resting