From 756bc291deb480becb8b5fd4449c9268baa089ed Mon Sep 17 00:00:00 2001 From: hoijui Date: Mon, 19 Feb 2018 00:38:10 +0100 Subject: [PATCH] fix(exercise 12): test GET request for fail #446 This is still missing most of the translations for the doc change. --- exercises/http_uppercaserer/exercise.js | 63 +++++++++++-------- exercises/http_uppercaserer/problem.es.md | 2 + exercises/http_uppercaserer/problem.md | 2 + .../http_uppercaserer/solution/solution.js | 2 +- exercises/juggling_async/exercise.js | 2 +- test/http_uppercaserer/valid_01.js | 2 +- 6 files changed, 43 insertions(+), 30 deletions(-) diff --git a/exercises/http_uppercaserer/exercise.js b/exercises/http_uppercaserer/exercise.js index 69dfc567..73aa32c8 100644 --- a/exercises/http_uppercaserer/exercise.js +++ b/exercises/http_uppercaserer/exercise.js @@ -54,34 +54,43 @@ function query (mode) { function connect (port, stream) { var input = through2() - var count = 0 - var iv var url = 'http://localhost:' + port - var req - - // TODO: test GET requests for #fail - req = input.pipe(hyperquest.post(url) - .on('error', function (err) { - exercise.emit( - 'fail' - , exercise.__('fail.connection', {address: url, message: err.message}) - ) - })) - - req.pipe(stream) - setTimeout(function () { - stream.unpipe(req) - stream.end() - }, 5000) - - iv = setInterval(function () { - input.write(words[count].trim() + '\n') - - if (++count === words.length) { - clearInterval(iv) - input.end() - } - }, 50) + var methods = ['post', 'get'] + + var mi = 0 + var miv = setInterval(function () { + var count = 0 + // test POST request for #success + // or GET request for #fail + var req = input.pipe(hyperquest[methods[mi]](url) + .on('error', function (err) { + exercise.emit( + 'fail' + , exercise.__('fail.connection', {address: url, message: err.message}) + ) + })) + + req.pipe(stream) + setTimeout(function () { + stream.unpipe(req) + if (++mi === methods.length) { + stream.end() + } + }, 5000) + + var iv = setInterval(function () { + input.write(words[count].trim() + '\n') + + if (++count === words.length) { + clearInterval(iv) + + if (++mi === methods.length) { + clearInterval(miv) + input.end() + } + } + }, 50) + }, 600) } connect(this.submissionPort, this.submissionStdout) diff --git a/exercises/http_uppercaserer/problem.es.md b/exercises/http_uppercaserer/problem.es.md index c98a92f4..7959a0f2 100644 --- a/exercises/http_uppercaserer/problem.es.md +++ b/exercises/http_uppercaserer/problem.es.md @@ -1,5 +1,7 @@ Escribe un **servidor** HTTP que reciba sólo peticiones POST y convierta los caracteres del cuerpo de la petición a mayúsculas y lo devuelva al cliente. +Peticiones GET hay que dar les una respuesta vacia. + El servidor deberá escuchar en un puerto cuyo número será el primer argumento del programa. ---------------------------------------------------------------------- diff --git a/exercises/http_uppercaserer/problem.md b/exercises/http_uppercaserer/problem.md index 5e627d1b..364860a3 100644 --- a/exercises/http_uppercaserer/problem.md +++ b/exercises/http_uppercaserer/problem.md @@ -1,5 +1,7 @@ Write an HTTP **server** that receives only POST requests and converts incoming POST body characters to upper-case and returns it to the client. +GET requests have to be answered with an empty response. + Your server should listen on the port provided by the first argument to your program. ---------------------------------------------------------------------- diff --git a/exercises/http_uppercaserer/solution/solution.js b/exercises/http_uppercaserer/solution/solution.js index 02243efa..bb40df93 100644 --- a/exercises/http_uppercaserer/solution/solution.js +++ b/exercises/http_uppercaserer/solution/solution.js @@ -3,7 +3,7 @@ const map = require('through2-map') const server = http.createServer(function (req, res) { if (req.method !== 'POST') { - return res.end('send me a POST\n') + return res.end() } req.pipe(map(function (chunk) { diff --git a/exercises/juggling_async/exercise.js b/exercises/juggling_async/exercise.js index 9fde5aec..65f65e71 100644 --- a/exercises/juggling_async/exercise.js +++ b/exercises/juggling_async/exercise.js @@ -6,7 +6,7 @@ var comparestdout = require('workshopper-exercise/comparestdout') var bogan = require('boganipsum') var after = require('after') - // three separate chunks of words to spit out +// three separate chunks of words to spit out var words = [ bogan({ paragraphs: 1, sentenceMax: 1 }).split(' '), bogan({ paragraphs: 1, sentenceMax: 1 }).split(' '), diff --git a/test/http_uppercaserer/valid_01.js b/test/http_uppercaserer/valid_01.js index d8e081f6..f23d4274 100644 --- a/test/http_uppercaserer/valid_01.js +++ b/test/http_uppercaserer/valid_01.js @@ -1,6 +1,6 @@ require('http').createServer(function (req, res) { if (req.method !== 'POST') { - return res.end('POST only!\n') + return res.end() } req.pipe(require('through2-map')(function (chunk) {