injector_tests.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. 'use strict';
  2. var should = require('should');
  3. var connect = require('connect');
  4. var request = require('request');
  5. var injector = require('./../lib/connect-injector');
  6. var httpProxy = require('http-proxy');
  7. var zlib = require('zlib');
  8. describe('connect-injector', function() {
  9. it('does not mess with normal requests', function(done) {
  10. var rewriter = injector(function() {
  11. return false;
  12. }, function() {
  13. done('Should never be called');
  14. });
  15. var app = connect().use(rewriter).use(function(req, res) {
  16. res.writeHead(200, { 'Content-Type': 'text/plain' });
  17. res.end('Hello World\n');
  18. });
  19. var server = app.listen(9999).on('listening', function() {
  20. request('http://localhost:9999', function(error, response, body) {
  21. should.not.exist(error);
  22. response.headers['content-type'].should.equal('text/plain');
  23. body.should.equal('Hello World\n');
  24. server.close(done);
  25. });
  26. });
  27. });
  28. it('does not mess with empty responses', function(done) {
  29. var rewriter = injector(function() {
  30. return false;
  31. }, function() {
  32. done('Should never be called');
  33. });
  34. var app = connect().use(rewriter).use(function(req, res) {
  35. res.writeHead(204);
  36. res.end();
  37. });
  38. var server = app.listen(9999).on('listening', function() {
  39. request('http://localhost:9999', function(error, response, body) {
  40. should.not.exist(error);
  41. should.not.exist(body);
  42. response.statusCode.should.equal(204);
  43. server.close(done);
  44. });
  45. });
  46. });
  47. it('works with empty responses that are rewritten', function(done) {
  48. var rewriter = injector(function() {
  49. return true;
  50. }, function() {
  51. done('Should never be called');
  52. });
  53. var app = connect().use(rewriter).use(function(req, res) {
  54. res.writeHead(304);
  55. res.end();
  56. });
  57. var server = app.listen(9999).on('listening', function() {
  58. request('http://localhost:9999', function(error, response, body) {
  59. should.not.exist(error);
  60. should.not.exist(body);
  61. response.statusCode.should.equal(304);
  62. server.close(done);
  63. });
  64. });
  65. });
  66. it('does some basic rewriting', function(done) {
  67. var REWRITTEN = 'Hello People';
  68. var rewriter = injector(function(req, res) {
  69. res.getHeader('content-type').should.equal('text/plain');
  70. return true;
  71. }, function(data, req, res, callback) {
  72. callback(null, REWRITTEN);
  73. });
  74. var app = connect().use(rewriter).use(function(req, res) {
  75. res.writeHead(200, { 'Content-Type': 'text/plain' });
  76. res.end('Hello World\n');
  77. });
  78. var server = app.listen(9999).on('listening', function() {
  79. request('http://localhost:9999', function(error, response, body) {
  80. should.not.exist(error);
  81. response.headers['content-type'].should.equal('text/plain');
  82. body.should.equal(REWRITTEN);
  83. server.close(done);
  84. });
  85. });
  86. });
  87. it('generates jsonp', function(done) {
  88. var PLAIN = 'Hello World\n';
  89. var OBJ = { hello: 'world' };
  90. var jsonp = injector(function(req, res) {
  91. var isJSON = res.getHeader('content-type').indexOf('application/json') === 0;
  92. return isJSON && req.query.callback;
  93. }, function(data, req, res, callback) {
  94. callback(null, req.query.callback + '(' + data.toString() + ')');
  95. });
  96. var app = connect().use(connect.query()).use(jsonp)
  97. .use('/plain', function(req, res) {
  98. res.writeHead(200, { 'Content-Type': 'text/plain' });
  99. res.end(PLAIN);
  100. }).use('/jsonp', function(req, res) {
  101. res.writeHead(200, { 'Content-Type': 'application/json' });
  102. res.end(JSON.stringify(OBJ));
  103. });
  104. var server = app.listen(9999).on('listening', function() {
  105. // Plain request
  106. request('http://localhost:9999/plain', function(error, response, body) {
  107. should.not.exist(error);
  108. response.headers['content-type'].should.equal('text/plain');
  109. body.should.equal(PLAIN);
  110. // Normal JSON request
  111. request('http://localhost:9999/jsonp', function(error, response, body) {
  112. should.not.exist(error);
  113. response.headers['content-type'].should.equal('application/json');
  114. body.should.equal(JSON.stringify(OBJ));
  115. // JSONP request
  116. request('http://localhost:9999/jsonp?callback=stuff', function(error, response, body) {
  117. should.not.exist(error);
  118. response.headers['content-type'].should.equal('application/json');
  119. body.should.equal('stuff(' + JSON.stringify(OBJ) + ')');
  120. server.close();
  121. done();
  122. });
  123. });
  124. });
  125. });
  126. });
  127. it('allows more than one injector', function(done) {
  128. var OBJ = { hello: 'people' };
  129. var REWRITTEN = 'Re-written stuff';
  130. var rewriter = injector(function(req, res) {
  131. return res.getHeader('content-type').indexOf('text/plain') === 0;
  132. }, function(data, req, res, callback) {
  133. callback(null, REWRITTEN);
  134. });
  135. var jsonp = injector(function(req, res) {
  136. var isJSON = res.getHeader('content-type').indexOf('application/json') === 0;
  137. return isJSON && req.query.callback;
  138. }, function(data, req, res, callback) {
  139. callback(null, req.query.callback + '(' + data.toString() + ')');
  140. });
  141. var app = connect().use(connect.query()).use(jsonp).use(rewriter)
  142. .use('/plain', function(req, res) {
  143. res.writeHead(200, { 'Content-Type': 'text/plain' });
  144. res.end('Hello world\n');
  145. }).use('/jsonp', function(req, res) {
  146. res.writeHead(200, { 'Content-Type': 'application/json' });
  147. res.end(JSON.stringify(OBJ));
  148. });
  149. var server = app.listen(9999).on('listening', function() {
  150. // Plain request
  151. request('http://localhost:9999/plain', function(error, response, body) {
  152. should.not.exist(error);
  153. response.headers['content-type'].should.equal('text/plain');
  154. body.should.equal(REWRITTEN);
  155. // JSONP request
  156. request('http://localhost:9999/jsonp?callback=stuff', function(error, response, body) {
  157. should.not.exist(error);
  158. response.headers['content-type'].should.equal('application/json');
  159. body.should.equal('stuff(' + JSON.stringify(OBJ) + ')');
  160. server.close();
  161. done();
  162. });
  163. });
  164. });
  165. });
  166. it('chains injectors', function(done) {
  167. var first = injector(function() {
  168. return true;
  169. }, function(data, req, res, callback) {
  170. callback(null, data.toString() + ' first');
  171. });
  172. var second = injector(function() {
  173. return true;
  174. }, function(data, req, res, callback) {
  175. callback(null, data.toString() + ' second');
  176. });
  177. var app = connect().use(first).use(second).use(function(req, res) {
  178. res.writeHead(200, { 'Content-Type': 'text/plain' });
  179. res.end('Hello');
  180. });
  181. var server = app.listen(9999).on('listening', function() {
  182. var expected = 'Hello first second';
  183. request('http://localhost:9999', function(error, response, body) {
  184. should.not.exist(error);
  185. response.headers['content-type'].should.equal('text/plain');
  186. body.should.equal(expected);
  187. server.close(done);
  188. });
  189. });
  190. });
  191. it('handles longer content properly', function(done) {
  192. var inject = injector(function(req, res) {
  193. return res.getHeader('content-type').indexOf('text/html') === 0;
  194. }, function(data, req, res, callback) {
  195. callback(null, data.toString().replace('</body>', '__injected__</body>'));
  196. });
  197. var app = connect().use(inject).use(connect.static(__dirname));
  198. var server = app.listen(9999).on('listening', function() {
  199. request('http://localhost:9999/dummycontent.html', function(error, response, body) {
  200. should.not.exist(error);
  201. response.headers['content-type'].indexOf('text/html').should.equal(0);
  202. body.indexOf('__injected__').should.not.equal(-1);
  203. request('http://localhost:9999/injector_tests.js', function(error, response, body) {
  204. should.not.exist(error);
  205. response.headers['content-type'].indexOf('application/javascript').should.equal(0);
  206. server.close(done);
  207. });
  208. });
  209. });
  210. });
  211. it('works with http-proxy', function(done) {
  212. var proxy = httpProxy.createProxyServer();
  213. var inject = injector(function(req, res) {
  214. return res.getHeader('content-type').indexOf('text/html') === 0;
  215. }, function(data, req, res, callback) {
  216. callback(null, data.toString().replace('</body>', '__injected__</body>'));
  217. });
  218. var proxyMiddleware = function(req, res) {
  219. proxy.web(req, res, {
  220. target: 'http://localhost:8878'
  221. });
  222. };
  223. var app = connect().use(connect.static(__dirname));
  224. var server = app.listen(8878).on('listening', function() {
  225. var proxyApp = connect().use(inject).use(proxyMiddleware);
  226. var proxyServer = proxyApp.listen(7787).on('listening', function() {
  227. request('http://localhost:7787/dummycontent.html', function(error, response, body) {
  228. should.not.exist(error);
  229. response.headers['content-type'].indexOf('text/html').should.equal(0);
  230. body.indexOf('__injected__').should.not.equal(-1);
  231. proxyServer.close(function() {
  232. server.close(done);
  233. });
  234. });
  235. });
  236. });
  237. });
  238. it('works with http-proxy and gzipped content', function(done) {
  239. var proxy = httpProxy.createProxyServer();
  240. var inject = injector(function(req, res) {
  241. return res.getHeader('content-type').indexOf('text/html') === 0;
  242. }, function(data, req, res, callback) {
  243. callback(null, data.toString().replace('</body>', '__injected__</body>'));
  244. });
  245. var proxyMiddleware = function(req, res) {
  246. req.headers.host = 'daffl.github.io';
  247. proxy.web(req, res, {
  248. target: 'http://daffl.github.io'
  249. });
  250. };
  251. var proxyApp = connect().use(inject).use(proxyMiddleware);
  252. var proxyServer = proxyApp.listen(9989).on('listening', function() {
  253. var gunzip = zlib.createGunzip();
  254. request({
  255. url: 'http://localhost:9989/connect-injector/dummycontent.html',
  256. headers: {
  257. 'Accept-Encoding': 'gzip, deflate'
  258. }
  259. }).pipe(gunzip);
  260. var body = "";
  261. gunzip.on('data', function(data) {
  262. body += data.toString();
  263. });
  264. gunzip.on('end', function() {
  265. body.indexOf('__injected__').should.not.equal(-1);
  266. proxyServer.close(done);
  267. });
  268. });
  269. });
  270. });