Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

passing test with the http module as a client

  • Loading branch information...
commit 9a85ed12525f68150f0ff491e8a8a52ccbf9a9f7 1 parent 8f5a24b
@substack authored
Showing with 46 additions and 0 deletions.
  1. +46 −0 test/http_ok.js
View
46 test/http_ok.js
@@ -0,0 +1,46 @@
+var test = require('tap').test;
+var prehost = require('../');
+var http = require('http');
+
+test('connect with http', function (t) {
+ t.plan(3);
+ var port = Math.floor(Math.random() * Math.pow(2,16) - 1e4) + 1e4;
+ var s = prehost(function (err, req) {
+ if (err) t.fail(err);
+
+ t.equal(req.host, 'localhost:' + port);
+ req.stream.write([
+ 'HTTP/1.1 200 200 OK',
+ 'Content-Type: text/plain',
+ 'Connection: close',
+ '',
+ 'oh hello'
+ ].join('\r\n'));
+ req.stream.end();
+ });
+
+ s.listen(port, function () {
+ var opts = {
+ method : 'GET',
+ host : 'localhost',
+ port : port,
+ path : '/'
+ };
+ var req = http.request(opts, function (res) {
+ t.equal(res.headers['content-type'], 'text/plain');
+
+ var data = '';
+ res.on('data', function (buf) {
+ data += buf.toString();
+ });
+
+ res.on('end', function () {
+ t.equal(data, 'oh hello');
+ s.close();
+ t.end();
+ });
+ });
+
+ req.end();
+ });
+});
Please sign in to comment.
Something went wrong with that request. Please try again.