Permalink
Browse files

Properly set headers on requests.

  • Loading branch information...
1 parent a657c68 commit a898e68f80b1d0212990e57dcc364ea7f4b6e583 @miccolis miccolis committed May 11, 2012
Showing with 52 additions and 0 deletions.
  1. +2 −0 lib/node-get/node-get.js
  2. +50 −0 test/headers.test.js
View
@@ -81,6 +81,7 @@ Get.prototype.request = function(callback) {
return https.request({
host: this.uri_o.hostname,
port: 443,
+ headers: this.headers,
path: this.proxy.hostname ?
this.uri :
((this.uri_o.pathname || '') +
@@ -91,6 +92,7 @@ Get.prototype.request = function(callback) {
return http.request({
port: this.proxy.port || this.uri_o.port || 80,
host: this.proxy.hostname || this.uri_o.hostname,
+ headers: this.headers,
path: this.proxy.hostname ?
this.uri :
((this.uri_o.pathname || '') +
View
@@ -0,0 +1,50 @@
+var assert = require('assert');
+var http = require('http');
+var get = require('..');
+
+var server;
+var port = 50000 + (Math.random() * 10000 | 0);
+before(function(done) {
+ server = http.createServer(function(req, res) {
+ res.writeHead(200, {'Content-Type': 'text/plain'});
+ res.end(JSON.stringify(req.headers));
+ });
+ server.listen(port, done);
+});
+
+describe('headers', function() {
+ it('should use default headers', function(done) {
+ new get({
+ uri: 'http://localhost:' + port
+ }).asString(function(err, result) {
+ assert.ifError(err);
+
+ var headers = JSON.parse(result);
+
+ [
+ ['accept-encoding', 'none'],
+ ['connection', 'close'],
+ ['user-agent', 'curl']
+ ].forEach(function(v) {
+ assert.equal(headers[v[0]], v[1]);
+ });
+ done();
+ });
+ });
+
+ it('should override "connection: keep-alive"', function(done) {
+ new get({
+ uri: 'http://localhost:' + port,
+ headers: { 'Connection': 'keep-alive' }
+ }).asString(function(err, result) {
+ assert.ifError(err);
+ var headers = JSON.parse(result);
+ assert.equal(headers.connection, 'keep-alive');
+ done();
+ });
+ });
+});
+
+after(function() {
+ server.close();
+});

0 comments on commit a898e68

Please sign in to comment.