Skip to content

Commit

Permalink
Merge pull request #49 from Marketionist/feature-status-codes
Browse files Browse the repository at this point in the history
Add configurable status codes
  • Loading branch information
Marketionist authored Oct 15, 2023
2 parents 1ba8987 + 3b4de3a commit 9d016c0
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 21 deletions.
43 changes: 29 additions & 14 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ let nodeTestingServer = {
console.log(packageName, 'Served back /post body JSON from the server to the client');

// Print outcoming response CODE
console.log(`\nResponse: ${res.statusCode}`);
console.log(`\nResponse status code: ${res.statusCode}`);
console.log(
'\nResponse data (incoming request body):',
JSON.stringify(JSON.parse(data), null, spacesToIndent)
Expand Down Expand Up @@ -125,7 +125,7 @@ let nodeTestingServer = {
// Show logs if they are enabled in nodeTestingServer.config.logsEnabled
if (nodeTestingServer.config.logsEnabled >= 1) {
// Print outcoming response CODE
console.log(`\nResponse: ${res.statusCode}`);
console.log(`\nResponse status code: ${res.statusCode}`);
console.log(packageName, `Served ${mainPagePath} from the server to the client`);
console.log('\nResponse data:', data);
console.log('========');
Expand Down Expand Up @@ -179,32 +179,38 @@ let nodeTestingServer = {
}

if (supportedFileExtensions.indexOf(fileExtension) === -1) {
res.writeHead(status404, { 'Content-Type': 'text/html' });
res.writeHead(status404, { 'Content-Type': 'text/html', 'Connection': 'close' });
res.end(`<h1>Error 404: ${fileExtension} is not among supported file formats:
${supportedFileExtensions.join(', ')}</h1>`);

// Show logs if they are enabled in nodeTestingServer.config.logsEnabled
if (nodeTestingServer.config.logsEnabled >= 1) {
// Print outcoming response CODE
console.log(`Response: ${res.statusCode}`);
console.log(`Response status code: ${res.statusCode}`);
console.log('========');
}
} else {
fs.exists(filePath, (exists) => {
if (!exists) {
if (typeof nodeTestingServer.config.pages[fileURL] === 'undefined') {
res.writeHead(status404, { 'Content-Type': 'text/html' });
res.writeHead(status404, { 'Content-Type': 'text/html', 'Connection': 'close' });
res.end(`<h1>Error 404: ${fileURL} is not set in nodeTestingServer.config.pages</h1>`);
} else {
// If requested page cannot be found in public/ folder,
// then it will be generated from nodeTestingServer.config.pages
res.writeHead(status200, { 'Content-Type': contentType });
res.end(nodeTestingServer.config.pages[fileURL]);
const pageStatusCode = nodeTestingServer.config.pages[fileURL].pageStatusCode || status200;
const pageBody = nodeTestingServer.config.pages[fileURL].pageBody ||
nodeTestingServer.config.pages[fileURL];

res.writeHead(pageStatusCode, { 'Content-Type': contentType, 'Connection': 'close' });
res.end(pageBody);
// Show logs if they are enabled in nodeTestingServer.config.logsEnabled
if (nodeTestingServer.config.logsEnabled >= 1) {
// Print outcoming response CODE
console.log(`Response: ${res.statusCode}`);
console.log(packageName, `Generated ${fileURL} from nodeTestingServer.config.pages`);
console.log(`\nResponse status code: ${res.statusCode}`);
console.log(`\n${packageName} Generated ${fileURL}` +
'from nodeTestingServer.config.pages');
console.log(`\nResponse data: ${pageBody}`);
console.log('========');
}
if (nodeTestingServer.config.logsEnabled === 2) {
Expand All @@ -216,7 +222,10 @@ let nodeTestingServer = {

return;
}
res.writeHead(status200, { 'Content-Type': contentType });
res.writeHead(
status200,
{ 'Content-Type': contentType, 'Connection': 'close' }
);

let stream = fs.createReadStream(filePath);

Expand All @@ -226,7 +235,7 @@ let nodeTestingServer = {
// Show logs if they are enabled in nodeTestingServer.config.logsEnabled
if (nodeTestingServer.config.logsEnabled >= 1) {
// Print outcoming response CODE
console.log(`\nResponse: ${res.statusCode}`);
console.log(`\nResponse status code: ${res.statusCode}`);
console.log(packageName, `Served ${filePath} from the server to the client`);
console.log('\nResponse data:', data);
console.log('========');
Expand All @@ -243,20 +252,26 @@ let nodeTestingServer = {
});
}
} else {
res.writeHead(status404, { 'Content-Type': 'text/html' });
res.writeHead(
status404,
{ 'Content-Type': 'text/html', 'Connection': 'close' }
);
res.end(`<h1>Error 404: ${req.method} is not supported</h1>`);

// Show logs if they are enabled in nodeTestingServer.config.logsEnabled
if (nodeTestingServer.config.logsEnabled >= 1) {
// Print outcoming response CODE
console.log(`Response: ${res.statusCode}`);
console.log(`Response status code: ${res.statusCode}`);
console.log('========');
}
}
}),

start () {
return this.server.listen(nodeTestingServer.config.port, nodeTestingServer.config.hostname)
return this.server.listen(
nodeTestingServer.config.port,
nodeTestingServer.config.hostname
)
.on('listening', () => console.log(
packageName,
`Server running at http://${nodeTestingServer.config.hostname}:${nodeTestingServer.config.port}/`))
Expand Down
48 changes: 41 additions & 7 deletions tests/page-content-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,22 @@ nodeTestingServer.config = {
<li>Eighth</li>
<li>Ninth</li>
<li>Tenth</li>
</ul>`
</ul>`,
'/test-status-code.html': {
pageStatusCode: 201,
pageBody: `<ul class="items">
<li>First</li>
<li>Second</li>
<li>Third</li>
<li>Fourth</li>
<li>Fifth</li>
<li>Sixth</li>
<li>Seventh</li>
<li>Eighth</li>
<li>Ninth</li>
<li>Tenth</li>
</ul>`
}
}
};

Expand All @@ -34,7 +49,7 @@ nodeTestingServer.config = {
* @param {string} bodyString
* @returns {Promise} response
*/
function createRequest (method, requestUrl, bodyString = '') {
function sendRequest (method, requestUrl, bodyString = '') {
return new Promise((resolve, reject) => {
// Check incoming body string to have proper JSON inside of it
const requestBody = bodyString.length > 0 ? JSON.stringify(JSON.parse(bodyString)) : '';
Expand Down Expand Up @@ -75,7 +90,10 @@ function createRequest (method, requestUrl, bodyString = '') {

console.log(`\nResponse body: ${response}`);
// Resolve after response was finished and all data from response was accumulated
resolve(data);
resolve({
statusCode: res.statusCode,
body: response
});
});
});

Expand Down Expand Up @@ -106,7 +124,8 @@ fixture(
});

test.page(
`http://${nodeTestingServer.config.hostname}:${nodeTestingServer.config.port}/`
`http://${nodeTestingServer.config.hostname}` +
`:${nodeTestingServer.config.port}/`
)(
'should get the h1 text from the /index.html main test server page',
async (t) => {
Expand All @@ -132,12 +151,27 @@ test.page(
test(
'should get the post body string from the /post server page',
async (t) => {
const response = await createRequest(
const response = await sendRequest(
'POST',
`http://${nodeTestingServer.config.hostname}:${nodeTestingServer.config.port}/post`,
`http://${nodeTestingServer.config.hostname}` +
`:${nodeTestingServer.config.port}/post`,
'{ "test1": 1, "test2": "Test text" }'
);

await t.expect(response).contains('Test text');
await t.expect(response.body).contains('Test text');
}
);

test(
'should get the 201 status code from the /test-status-code.html server page',
async (t) => {
const status201 = 201;
const response = await sendRequest(
'GET',
`http://${nodeTestingServer.config.hostname}` +
`:${nodeTestingServer.config.port}/test-status-code.html`
);

await t.expect(response.statusCode === status201).ok();
}
);

0 comments on commit 9d016c0

Please sign in to comment.