Skip to content

Instantly share code, notes, and snippets.

@srinathperera
Created December 9, 2022 06:49
Show Gist options
  • Save srinathperera/ec19674a0014c3fb8a936728146da701 to your computer and use it in GitHub Desktop.
Save srinathperera/ec19674a0014c3fb8a936728146da701 to your computer and use it in GitHub Desktop.
GPT3 Log 1
Q - write a code for a service in ballerina to add two integers x and y and return the result
Answer:
import ballerina/http;
// define the service
service /add {
// define the resource and the method it responds to
resource function add(http:Request request, http:Response response) {
// parse the query parameters "x" and "y"
int x = request.getQueryParam("x");
int y = request.getQueryParam("y");
// compute the sum of x and y
int result = x + y;
// set the response body to the result
response.setPayload(result);
}
}
Q: write code for a test case to test the above service
Answer:
import ballerina/http;
import ballerina/test;
// define the test case
@test:Config {
// specify the endpoint of the service
targetEndpoint: "http://localhost:9090/add"
}
function testAdd() {
// make a GET request to the "add" endpoint
http:Response response = http:get("http://localhost:9090/add?x=1&y=2");
// assert that the response has a 200 OK status code
test:assertEquals(response.statusCode, 200);
// assert that the response body is "3"
test:assertEquals(response.text(), "3");
}
Q: add another function to the test case that sends x = 34 and y=76
Answer:
import ballerina/http;
import ballerina/test;
// define the test case
@test:Config {
// specify the endpoint of the service
targetEndpoint: "http://localhost:9090/add"
}
function testAdd() {
// make a GET request to the "add" endpoint
http:Response response = http:get("http://localhost:9090/add?x=1&y=2");
// assert that the response has a 200 OK status code
test:assertEquals(response.statusCode, 200);
// assert that the response body is "3"
test:assertEquals(response.text(), "3");
// make a second GET request with different query parameters
response = http:get("http://localhost:9090/add?x=34&y=76");
// assert that the response has a 200 OK status code
test:assertEquals(response.statusCode, 200);
// assert that the response body is "110"
test:assertEquals(response.text(), "110");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment