Skip to content

Commit

Permalink
Merge pull request kartikk221#310 from k1eu/k1eu/sendStatus
Browse files Browse the repository at this point in the history
feat: send response on calling sendStatus
  • Loading branch information
kartikk221 authored Oct 15, 2024
2 parents d725e14 + 643ff2e commit 831e895
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/components/compatibility/ExpressResponse.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ class ExpressResponse {
}

sendStatus(status_code) {
return this.status(status_code);
return this.status(status_code).send();
}

set(field, value) {
Expand Down
4 changes: 4 additions & 0 deletions tests/components/http/Response.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ router.post(endpoint, async (request, response) => {

// Bind router to webserver
const { TEST_SERVER } = require('../Server.js');
const { test_response_send_status } = require('./scenarios/response_send_status.js');
TEST_SERVER.use(router);

async function test_response_object() {
Expand Down Expand Up @@ -138,6 +139,9 @@ async function test_response_object() {
// Verify the custom HTTP status code and message support
await test_response_custom_status();

// Verify the custom HTTP status code and message support
await test_response_send_status();

// Verify the behavior of the .header() and .cookie() methods
await test_response_headers_behavior();

Expand Down
49 changes: 49 additions & 0 deletions tests/components/http/scenarios/response_send_status.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
const { assert_log } = require('../../../scripts/operators.js');
const { HyperExpress, fetch, server } = require('../../../configuration.js');
const router = new HyperExpress.Router();
const endpoint = '/tests/response';
const scenario_endpoint = '/send-status';
const endpoint_url = server.base + endpoint + scenario_endpoint;

// Create Backend HTTP Route
router.post(scenario_endpoint, async (request, response) => {
const { status } = await request.json();
response.sendStatus(status);
});

// Bind router to webserver
const { TEST_SERVER } = require('../../Server.js');
TEST_SERVER.use(endpoint, router);

async function test_response_send_status() {
const group = 'RESPONSE';
const candidate = 'HyperExpress.Response.statusCode';

[
{
status: 200,
},
{
status: 609,
},
].map(async ({ status }) => {
// Make a request to the server with a status code
const response = await fetch(endpoint_url, {
method: 'POST',
body: JSON.stringify({
status,
}),
});

// Validate the status code on the response
assert_log(
group,
`${candidate} Custom Status Code & Response Test - "HTTP ${status}"`,
() => response.status === status
);
});
}

module.exports = {
test_response_send_status,
};

0 comments on commit 831e895

Please sign in to comment.