Skip to content

Instantly share code, notes, and snippets.

@aseemk
Created July 30, 2013 20:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aseemk/6116887 to your computer and use it in GitHub Desktop.
Save aseemk/6116887 to your computer and use it in GitHub Desktop.
Testing `Expect: 100-continue` support in Node.js and HTTPie.
$ http -v PUT localhost:5100 Expect:100-continue foo=bar
PUT / HTTP/1.1
Accept-Encoding: identity, deflate, compress, gzip
Accept: application/json
User-Agent: HTTPie/0.2.5
Host: localhost:5100
Expect: 100-continue
Content-Type: application/json; charset=utf-8
{
"foo": "bar"
}
HTTP/1.1 400 Bad Request
Date: Tue, 30 Jul 2013 20:57:22 GMT
Connection: close
Transfer-Encoding: chunked
Sorry bro.
$ coffee server
Listening on port 5100...
GOT EXPECT: { host: 'localhost:5100',
'content-length': '14',
'accept-encoding': 'identity, deflate, compress, gzip',
'content-type': 'application/json; charset=utf-8',
expect: '100-continue',
accept: 'application/json',
'user-agent': 'HTTPie/0.2.5' } null
CUTTING OFF!
Got req data: {"foo": "bar"}
Request finished.
WTF? Should have been cut off.
#
# Node.js server to test Expect: 100-continue requests.
#
http = require 'http'
server = http.createServer()
server.on 'checkContinue', (req, res) ->
continued = Math.random() < 0.5
readRequest 'GOT EXPECT:', req, ->
if continued
res.end 'Pleasure doing business.\n'
else
console.log 'WTF? Should have been cut off.\n'
if continued
console.log 'Letting continue...\n'
res.writeContinue()
else
console.log 'CUTTING OFF!\n'
res.writeHead 400
res.end 'Sorry bro.\n'
server.on 'request', (req, res) ->
readRequest 'Got request:', req, ->
res.end 'Word.\n'
readRequest = (msg, req, cb) ->
req.setEncoding 'utf8'
console.log msg, req.headers, req.read()
req.on 'data', (chunk) ->
console.log 'Got req data:', chunk
req.on 'end', (chunk) ->
console.log 'Request finished.\n'
cb?()
server.listen 5100
console.log 'Listening on port 5100...'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment