Created
July 30, 2013 20:59
-
-
Save aseemk/6116887 to your computer and use it in GitHub Desktop.
Testing `Expect: 100-continue` support in Node.js and HTTPie.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ 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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ 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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# 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