Skip to content

Instantly share code, notes, and snippets.

@yasassri
Created June 4, 2018 08:36
Show Gist options
  • Save yasassri/d7841de3728d718d590159ef5f60977a to your computer and use it in GitHub Desktop.
Save yasassri/d7841de3728d718d590159ef5f60977a to your computer and use it in GitHub Desktop.
import ballerina/http;
import ballerina/io;
boolean isHelloServiceStarted;
// Before function to start the service
function startMock () {
isHelloServiceStarted = test:startServices("mock");
}
// After function to stop the service
function stopMock () {
test:stopServices("mock");
}
@test:Config{
before: "startMock",
after:"stopMock"
}
// This is the test function to test the service
function testService () {
endpoint http:Client httpEndpoint {
url:"http://0.0.0.0:9092"
};
// Check whether the service is started
test:assertTrue(isHelloServiceStarted, msg = "Hello service failed to start");
// Send a GET request to the specified endpoint
var response = httpEndpoint -> get("/hello");
match response {
http:Response resp => {
var jsonRes = resp.getJsonPayload();
json expected = {"Hello":"World"};
test:assertEquals(jsonRes, expected);
}
http:HttpConnectorError err => test:assertFail(msg = "Failed to call the endpoint: " + uri);
}
}
// The service we are going to start and test
endpoint http:Listener helloEP {
port: 9092
};
@http:ServiceConfig {
basePath: "/hello"
}
service<http:Service> HelloServiceMock bind helloEP {
@http:ResourceConfig {
methods:["GET"],
path:"/"
}
getEvents (endpoint caller, http:Request req) {
http:Response res = new;
json j = {"Hello":"World"};
res.setJsonPayload(j);
_ = caller -> respond(res);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment