Skip to content

Commit

Permalink
Add service test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
TharmiganK committed May 14, 2024
1 parent d75bf4c commit 8ed3431
Showing 1 changed file with 57 additions and 0 deletions.
57 changes: 57 additions & 0 deletions ballerina-tests/http-misc-tests/tests/http_status_code_test.bal
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,16 @@ service /differentStatusCodes on httpStatusCodeListenerEP {
}
return {body: "Authorization Required"};
}

resource function get default/[int statusCode]() returns http:DefaultStatusCodeResponse {
if statusCode == 204 {
return {status: new (204)};
}
return {
body: "Default Response",
status: new (statusCode)
};
}
}

//Test ballerina ok() function with entity body
Expand Down Expand Up @@ -782,3 +792,50 @@ function testNetworkAuthenticationRequired() {
test:assertFail(msg = "Found unexpected output type: " + response.message());
}
}

@test:Config {}
function testDefaultStatusCodeResponse() {
http:Response|error response = httpStatusCodeClient->get("/differentStatusCodes/default/204");
if response is http:Response {
test:assertEquals(response.statusCode, 204, msg = "Found unexpected output");
test:assertEquals(response.reasonPhrase, "No Content", msg = "Found unexpected output");
} else {
test:assertFail(msg = "Found unexpected output type: " + response.message());
}

response = httpStatusCodeClient->get("/differentStatusCodes/default/201");
if response is http:Response {
test:assertEquals(response.statusCode, 201, msg = "Found unexpected output");
test:assertEquals(response.reasonPhrase, "Created", msg = "Found unexpected output");
common:assertTextPayload(response.getTextPayload(), "Default Response");
} else {
test:assertFail(msg = "Found unexpected output type: " + response.message());
}

response = httpStatusCodeClient->get("/differentStatusCodes/default/404");
if response is http:Response {
test:assertEquals(response.statusCode, 404, msg = "Found unexpected output");
test:assertEquals(response.reasonPhrase, "Not Found", msg = "Found unexpected output");
common:assertTextPayload(response.getTextPayload(), "Default Response");
} else {
test:assertFail(msg = "Found unexpected output type: " + response.message());
}

response = httpStatusCodeClient->get("/differentStatusCodes/default/500");
if response is http:Response {
test:assertEquals(response.statusCode, 500, msg = "Found unexpected output");
test:assertEquals(response.reasonPhrase, "Internal Server Error", msg = "Found unexpected output");
common:assertTextPayload(response.getTextPayload(), "Default Response");
} else {
test:assertFail(msg = "Found unexpected output type: " + response.message());
}

response = httpStatusCodeClient->get("/differentStatusCodes/default/600");
if response is http:Response {
test:assertEquals(response.statusCode, 600, msg = "Found unexpected output");
test:assertEquals(response.reasonPhrase, "Unknown Status (600)", msg = "Found unexpected output");
common:assertTextPayload(response.getTextPayload(), "Default Response");
} else {
test:assertFail(msg = "Found unexpected output type: " + response.message());
}
}

0 comments on commit 8ed3431

Please sign in to comment.