Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(exercise 12): test GET request for fail #446 #609

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions exercises/http_json_api_server/problem.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ The JSON response should contain only 'hour', 'minute' and 'second' properties.
}
```

Add second endpoint for the path '/api/unixtime' which accepts the same query string but returns UNIX epoch time in milliseconds (the number of milliseconds since 1 Jan 1970 00:00:00 UTC) under the property 'unixtime'. For example:
Add a second endpoint for the path '/api/unixtime' which accepts the same query string, but returns UNIX epoch time in milliseconds (the number of milliseconds since 1 Jan 1970 00:00:00 UTC) under the property 'unixtime'. For example:

```json
{ "unixtime": 1376136615474 }
Expand All @@ -27,7 +27,8 @@ Your server should listen on the port provided by the first argument to your pro

The `request` object from an HTTP server has a `url` property that you will need to use to *"route"* your requests for the two endpoints.

You can parse the URL and query string using the Node core 'url' module. `url.parse(request.url, true)` will parse content of request.url and provide you with an object with helpful properties.
You can parse the URL and query string using the Node core 'url' module.
`url.parse(request.url, true)` will parse content of `request.url` and provide you with an object with helpful properties.

For example, on the command prompt, type:

Expand Down
63 changes: 36 additions & 27 deletions exercises/http_uppercaserer/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we get rid of the setInterval and setTimeout calls somehow? These make the code complex.

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()
}
}, 3000)

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)
Expand Down
2 changes: 2 additions & 0 deletions exercises/http_uppercaserer/problem.es.md
Original file line number Diff line number Diff line change
@@ -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.

----------------------------------------------------------------------
Expand Down
2 changes: 2 additions & 0 deletions exercises/http_uppercaserer/problem.md
Original file line number Diff line number Diff line change
@@ -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.

----------------------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion exercises/http_uppercaserer/solution/solution.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
"through": "^2.3.8",
"through2": "^2.0.1",
"through2-map": "^3.0.0",
"workshopper-adventure": "^6.0.2",
"workshopper-adventure": "^6.0.3",
"workshopper-exercise": "^3.0.1",
"workshopper-wrappedexec": "~0.1.1"
},
Expand Down
2 changes: 1 addition & 1 deletion test/http_uppercaserer/valid_01.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down