diff --git a/index.js b/index.js
index d62b78a..ade763e 100644
--- a/index.js
+++ b/index.js
@@ -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)
@@ -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('========');
@@ -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(`
Error 404: ${fileExtension} is not among supported file formats:
${supportedFileExtensions.join(', ')}
`);
// 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(`Error 404: ${fileURL} is not set in nodeTestingServer.config.pages
`);
} 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) {
@@ -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);
@@ -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('========');
@@ -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(`Error 404: ${req.method} is not supported
`);
// 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}/`))
diff --git a/tests/page-content-tests.js b/tests/page-content-tests.js
index 5aa26be..0373f70 100644
--- a/tests/page-content-tests.js
+++ b/tests/page-content-tests.js
@@ -23,7 +23,22 @@ nodeTestingServer.config = {
Eighth
Ninth
Tenth
- `
+ `,
+ '/test-status-code.html': {
+ pageStatusCode: 201,
+ pageBody: `
+ - First
+ - Second
+ - Third
+ - Fourth
+ - Fifth
+ - Sixth
+ - Seventh
+ - Eighth
+ - Ninth
+ - Tenth
+
`
+ }
}
};
@@ -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)) : '';
@@ -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
+ });
});
});
@@ -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) => {
@@ -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();
}
);