For anyone looking to know how to configure wiremock server using JSON
The below configuration creates an end point some/test using json
To use wiremock server:
usingFilesUnderDirectory() method will set the directory in which wiremock server needs to search for mappings and __Files folder. so make sure to keep the json file inside root/mappings/<endpoint>.json
the name of the folder inside the root folder should be mapping and __files.
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options;
import com.github.tomakehurst.wiremock.WireMockServer;
WireMockServer wm = new WireMockServer(options().port(2345).usingFilesUnderDirectory("D:/temp/root"));
//Start the server and do something
wm.start();
RequestSpecification httpRequest = RestAssured.given();
Response response = httpRequest.get("http://localhost:2345/some/test");
System.out.println(response.getStatusCode());
//stop the service
wm.stop();
Now keep the sample json under mappings folder under D:/temp/root
Sample Json:
{
"id" : "5158b37b-3141-4b5f-ab45-06375dbbe658",
"request" : {
"url" : "/some/test",
"method" : "GET"
},
"response" : {
"status" : 404,
"body" : "Hello sdsworld!",
"headers" : {
"Content-Type" : "text/plain"
}
},
"uuid" : "5158b37b-3141-4b5f-ab45-06375dbbe658",
"persistent" : true,
"insertionIndex" : 0
}
You can use any HTTP request library in Java (i hope you are using Java) or the language you are using in.
Currently in Java rest-assured and karate are two famous rest api testing utilities and i prefer these to even for intermittent validation as you can use it in future too when more complicated scenarios come up:
Install rest-assured using Maven:
https://github.com/rest-assured/rest-assured/wiki/GettingStarted
IN your case for simple get request only rest-assured is required
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<version>4.3.0</version>
</dependency>
Else for complex JSON parsing add the other required tools as mentioned in the documentation using Maven
import rest assured packages:
import io.restassured.RestAssured;
import io.restassured.http.Method;
import io.restassured.response.Response;
import io.restassured.specification.RequestSpecification;
Now do a simple get:
https://www.toolsqa.com/rest-assured/rest-api-test-using-rest-assured/ (Provides great tutorials).
public int getNumUsers(){
RequestSpecification httpRequest = RestAssured.given();
RequestSpecification httpRequest = RestAssured.given();
Response response = httpRequest.get("http://localhost:4535/some/test");
System.out.println(response.getStatusCode());
}