http_test.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. // Licensed to the Software Freedom Conservancy (SFC) under one
  2. // or more contributor license agreements. See the NOTICE file
  3. // distributed with this work for additional information
  4. // regarding copyright ownership. The SFC licenses this file
  5. // to you under the Apache License, Version 2.0 (the
  6. // "License"); you may not use this file except in compliance
  7. // with the License. You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing,
  12. // software distributed under the License is distributed on an
  13. // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  14. // KIND, either express or implied. See the License for the
  15. // specific language governing permissions and limitations
  16. // under the License.
  17. 'use strict';
  18. var assert = require('assert'),
  19. http = require('http'),
  20. url = require('url');
  21. var HttpClient = require('../../http').HttpClient,
  22. HttpRequest = require('../../lib/http').Request,
  23. HttpResponse = require('../../lib/http').Response,
  24. Server = require('../../lib/test/httpserver').Server;
  25. describe('HttpClient', function() {
  26. this.timeout(4 * 1000);
  27. var server = new Server(function(req, res) {
  28. let parsedUrl = url.parse(req.url);
  29. if (req.method == 'GET' && req.url == '/echo') {
  30. res.writeHead(200, req.headers);
  31. res.end();
  32. } else if (req.method == 'GET' && req.url == '/redirect') {
  33. res.writeHead(303, {'Location': server.url('/hello')});
  34. res.end();
  35. } else if (req.method == 'GET' && req.url == '/hello') {
  36. res.writeHead(200, {'content-type': 'text/plain'});
  37. res.end('hello, world!');
  38. } else if (req.method == 'GET' && req.url == '/chunked') {
  39. res.writeHead(200, {
  40. 'content-type': 'text/html; charset=utf-8',
  41. 'transfer-encoding': 'chunked'
  42. });
  43. res.write('<!DOCTYPE html>');
  44. setTimeout(() => res.end('<h1>Hello, world!</h1>'), 20);
  45. } else if (req.method == 'GET' && req.url == '/badredirect') {
  46. res.writeHead(303, {});
  47. res.end();
  48. } else if (req.method == 'GET' && req.url == '/protected') {
  49. var denyAccess = function() {
  50. res.writeHead(401, {'WWW-Authenticate': 'Basic realm="test"'});
  51. res.end('Access denied');
  52. };
  53. var basicAuthRegExp = /^\s*basic\s+([a-z0-9\-\._~\+\/]+)=*\s*$/i
  54. var auth = req.headers.authorization;
  55. var match = basicAuthRegExp.exec(auth || '');
  56. if (!match) {
  57. denyAccess();
  58. return;
  59. }
  60. var userNameAndPass = new Buffer(match[1], 'base64').toString();
  61. var parts = userNameAndPass.split(':', 2);
  62. if (parts[0] !== 'genie' && parts[1] !== 'bottle') {
  63. denyAccess();
  64. return;
  65. }
  66. res.writeHead(200, {'content-type': 'text/plain'});
  67. res.end('Access granted!');
  68. } else if (req.method == 'GET'
  69. && parsedUrl.pathname
  70. && parsedUrl.pathname.endsWith('/proxy')) {
  71. let headers = Object.assign({}, req.headers);
  72. headers['x-proxy-request-uri'] = req.url;
  73. res.writeHead(200, headers);
  74. res.end();
  75. } else if (req.method == 'GET'
  76. && parsedUrl.pathname
  77. && parsedUrl.pathname.endsWith('/proxy/redirect')) {
  78. let path = `/proxy${parsedUrl.search || ''}${parsedUrl.hash || ''}`;
  79. res.writeHead(303, {'Location': path});
  80. res.end();
  81. } else {
  82. res.writeHead(404, {});
  83. res.end();
  84. }
  85. });
  86. before(function() {
  87. return server.start();
  88. });
  89. after(function() {
  90. return server.stop();
  91. });
  92. it('can send a basic HTTP request', function() {
  93. var request = new HttpRequest('GET', '/echo');
  94. request.headers.set('Foo', 'Bar');
  95. var agent = new http.Agent();
  96. agent.maxSockets = 1; // Only making 1 request.
  97. var client = new HttpClient(server.url(), agent);
  98. return client.send(request).then(function(response) {
  99. assert.equal(200, response.status);
  100. assert.equal(response.headers.get('content-length'), '0');
  101. assert.equal(response.headers.get('connection'), 'keep-alive');
  102. assert.equal(response.headers.get('host'), server.host());
  103. assert.equal(request.headers.get('Foo'), 'Bar');
  104. assert.equal(
  105. request.headers.get('Accept'), 'application/json; charset=utf-8');
  106. });
  107. });
  108. it('handles chunked responses', function() {
  109. let request = new HttpRequest('GET', '/chunked');
  110. let client = new HttpClient(server.url());
  111. return client.send(request).then(response => {
  112. assert.equal(200, response.status);
  113. assert.equal(response.body, '<!DOCTYPE html><h1>Hello, world!</h1>');
  114. });
  115. });
  116. it('can use basic auth', function() {
  117. var parsed = url.parse(server.url());
  118. parsed.auth = 'genie:bottle';
  119. var client = new HttpClient(url.format(parsed));
  120. var request = new HttpRequest('GET', '/protected');
  121. return client.send(request).then(function(response) {
  122. assert.equal(200, response.status);
  123. assert.equal(response.headers.get('content-type'), 'text/plain');
  124. assert.equal(response.body, 'Access granted!');
  125. });
  126. });
  127. it('fails requests missing required basic auth', function() {
  128. var client = new HttpClient(server.url());
  129. var request = new HttpRequest('GET', '/protected');
  130. return client.send(request).then(function(response) {
  131. assert.equal(401, response.status);
  132. assert.equal(response.body, 'Access denied');
  133. });
  134. });
  135. it('automatically follows redirects', function() {
  136. var request = new HttpRequest('GET', '/redirect');
  137. var client = new HttpClient(server.url());
  138. return client.send(request).then(function(response) {
  139. assert.equal(200, response.status);
  140. assert.equal(response.headers.get('content-type'), 'text/plain');
  141. assert.equal(response.body, 'hello, world!');
  142. });
  143. });
  144. it('handles malformed redirect responses', function() {
  145. var request = new HttpRequest('GET', '/badredirect');
  146. var client = new HttpClient(server.url());
  147. return client.send(request).then(assert.fail, function(err) {
  148. assert.ok(/Failed to parse "Location"/.test(err.message),
  149. 'Not the expected error: ' + err.message);
  150. });
  151. });
  152. describe('with proxy', function() {
  153. it('sends request to proxy with absolute URI', function() {
  154. var request = new HttpRequest('GET', '/proxy');
  155. var client = new HttpClient(
  156. 'http://another.server.com', undefined, server.url());
  157. return client.send(request).then(function(response) {
  158. assert.equal(200, response.status);
  159. assert.equal(response.headers.get('host'), 'another.server.com');
  160. assert.equal(
  161. response.headers.get('x-proxy-request-uri'),
  162. 'http://another.server.com/proxy');
  163. });
  164. });
  165. it('uses proxy when following redirects', function() {
  166. var request = new HttpRequest('GET', '/proxy/redirect');
  167. var client = new HttpClient(
  168. 'http://another.server.com', undefined, server.url());
  169. return client.send(request).then(function(response) {
  170. assert.equal(200, response.status);
  171. assert.equal(response.headers.get('host'), 'another.server.com');
  172. assert.equal(
  173. response.headers.get('x-proxy-request-uri'),
  174. 'http://another.server.com/proxy');
  175. });
  176. });
  177. it('includes search and hash in redirect URI', function() {
  178. var request = new HttpRequest('GET', '/proxy/redirect?foo#bar');
  179. var client = new HttpClient(
  180. 'http://another.server.com', undefined, server.url());
  181. return client.send(request).then(function(response) {
  182. assert.equal(200, response.status);
  183. assert.equal(response.headers.get('host'), 'another.server.com');
  184. assert.equal(
  185. response.headers.get('x-proxy-request-uri'),
  186. 'http://another.server.com/proxy?foo#bar');
  187. });
  188. });
  189. });
  190. });