From 975e015f1d8360e3ec518d78b8cd5e6668af03df Mon Sep 17 00:00:00 2001 From: jpdamon Date: Mon, 4 Jan 2016 18:20:23 -0500 Subject: [PATCH] added verification of content-type to http_file_server --- exercises/http_file_server/exercise.js | 16 ++++++++++++++++ exercises/http_file_server/problem.md | 2 ++ i18n/en.json | 5 ++++- 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/exercises/http_file_server/exercise.js b/exercises/http_file_server/exercise.js index 5af6c759..f2da55e6 100644 --- a/exercises/http_file_server/exercise.js +++ b/exercises/http_file_server/exercise.js @@ -91,6 +91,22 @@ function query (mode) { , exercise.__('fail.connection', {address: url, message: err.message}) ) }) + .on('response', function(res) { + if(res.statusCode != 200) { + exercise.emit( + 'fail', + exercise.__('fail.status_code', {status: res.statusCode})) + } + if(res.headers['content-type'] === undefined) { + exercise.emit( + 'fail', + exercise.__('fail.missing_content_type')) + } else if(res.headers['content-type'] != 'text/plain') { + exercise.emit( + 'fail', + exercise.__('fail.content_type', {header: res.headers['content-type']})) + } + }) req.pipe(stream) setTimeout(function () { stream.unpipe(req) diff --git a/exercises/http_file_server/problem.md b/exercises/http_file_server/problem.md index a2356e1f..491f6540 100644 --- a/exercises/http_file_server/problem.md +++ b/exercises/http_file_server/problem.md @@ -17,6 +17,8 @@ function callback (request, response) { /* ... */ } Where the two arguments are objects representing the HTTP request and the corresponding response for this request. `request` is used to fetch properties, such as the header and query-string from the request while `response` is for sending data to the client, both headers and body. +When sending the HTTP response, it is important to specify what kind of data you're sending back. This is declared in the 'Content-Type' header. For this exercise we'll be sending plain text, so the content type will be 'text/plain'. Look at `response.writeHead()` in the `http` module to learn how to do this. + Both `request` and `response` are also Node streams! Which means that you can use the streaming abstractions to send and receive data if they suit your use-case. `http.createServer()` also returns an instance of your `server`. You must call `server.listen(portNumber)` to start listening on a particular port. diff --git a/i18n/en.json b/i18n/en.json index c432500d..0d91e098 100644 --- a/i18n/en.json +++ b/i18n/en.json @@ -56,7 +56,10 @@ }, "HTTP FILE SERVER": { "fail": { - "no_createReadStream": "Used fs method other than fs.createReadStream(): {{{method}}}" + "no_createReadStream": "Used fs method other than fs.createReadStream(): {{{method}}}", + "status_code": "Status code of HTTP response was incorrect: {{{status}}}", + "missing_content_type": "`Content-Type` header was not found in the response", + "content_type": "`Content-Type` header was not `text/plain`: {{{header}}}" } } }