PageRenderTime 57ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/test/lib-http-proxy-common-test.js

https://gitlab.com/chandanpasunoori/node-http-proxy
JavaScript | 241 lines | 215 code | 25 blank | 1 comment | 1 complexity | b505bd78a58fae950ba8f5bc797feed2 MD5 | raw file
  1. var common = require('../lib/http-proxy/common'),
  2. expect = require('expect.js');
  3. describe('lib/http-proxy/common.js', function () {
  4. describe('#setupOutgoing', function () {
  5. it('should setup the correct headers', function () {
  6. var outgoing = {};
  7. common.setupOutgoing(outgoing,
  8. {
  9. agent : '?',
  10. target: {
  11. host : 'hey',
  12. hostname : 'how',
  13. socketPath: 'are',
  14. port : 'you',
  15. },
  16. headers: {'fizz': 'bang', 'overwritten':true},
  17. localAddress: 'local.address',
  18. },
  19. {
  20. method : 'i',
  21. url : 'am',
  22. headers : {'pro':'xy','overwritten':false}
  23. });
  24. expect(outgoing.host).to.eql('hey');
  25. expect(outgoing.hostname).to.eql('how');
  26. expect(outgoing.socketPath).to.eql('are');
  27. expect(outgoing.port).to.eql('you');
  28. expect(outgoing.agent).to.eql('?');
  29. expect(outgoing.method).to.eql('i');
  30. expect(outgoing.path).to.eql('am');
  31. expect(outgoing.headers.pro).to.eql('xy');
  32. expect(outgoing.headers.fizz).to.eql('bang');
  33. expect(outgoing.headers.overwritten).to.eql(true);
  34. expect(outgoing.localAddress).to.eql('local.address');
  35. });
  36. it('should not override agentless upgrade header', function () {
  37. var outgoing = {};
  38. common.setupOutgoing(outgoing,
  39. {
  40. agent: undefined,
  41. target: {
  42. host : 'hey',
  43. hostname : 'how',
  44. socketPath: 'are',
  45. port : 'you',
  46. },
  47. headers: {'connection': 'upgrade'},
  48. },
  49. {
  50. method : 'i',
  51. url : 'am',
  52. headers : {'pro':'xy','overwritten':false}
  53. });
  54. expect(outgoing.headers.connection).to.eql('upgrade');
  55. });
  56. it('should not override agentless connection: contains upgrade', function () {
  57. var outgoing = {};
  58. common.setupOutgoing(outgoing,
  59. {
  60. agent: undefined,
  61. target: {
  62. host : 'hey',
  63. hostname : 'how',
  64. socketPath: 'are',
  65. port : 'you',
  66. },
  67. headers: {'connection': 'keep-alive, upgrade'}, // this is what Firefox sets
  68. },
  69. {
  70. method : 'i',
  71. url : 'am',
  72. headers : {'pro':'xy','overwritten':false}
  73. });
  74. expect(outgoing.headers.connection).to.eql('keep-alive, upgrade');
  75. });
  76. it('should override agentless connection: contains improper upgrade', function () {
  77. // sanity check on upgrade regex
  78. var outgoing = {};
  79. common.setupOutgoing(outgoing,
  80. {
  81. agent: undefined,
  82. target: {
  83. host : 'hey',
  84. hostname : 'how',
  85. socketPath: 'are',
  86. port : 'you',
  87. },
  88. headers: {'connection': 'keep-alive, not upgrade'},
  89. },
  90. {
  91. method : 'i',
  92. url : 'am',
  93. headers : {'pro':'xy','overwritten':false}
  94. });
  95. expect(outgoing.headers.connection).to.eql('close');
  96. });
  97. it('should override agentless non-upgrade header to close', function () {
  98. var outgoing = {};
  99. common.setupOutgoing(outgoing,
  100. {
  101. agent: undefined,
  102. target: {
  103. host : 'hey',
  104. hostname : 'how',
  105. socketPath: 'are',
  106. port : 'you',
  107. },
  108. headers: {'connection': 'xyz'},
  109. },
  110. {
  111. method : 'i',
  112. url : 'am',
  113. headers : {'pro':'xy','overwritten':false}
  114. });
  115. expect(outgoing.headers.connection).to.eql('close');
  116. });
  117. it('should set the agent to false if none is given', function () {
  118. var outgoing = {};
  119. common.setupOutgoing(outgoing, {target:
  120. 'http://localhost'
  121. }, { url: '/' });
  122. expect(outgoing.agent).to.eql(false);
  123. });
  124. it('set the port according to the protocol', function () {
  125. var outgoing = {};
  126. common.setupOutgoing(outgoing,
  127. {
  128. agent : '?',
  129. target: {
  130. host : 'how',
  131. hostname : 'are',
  132. socketPath: 'you',
  133. protocol: 'https:'
  134. }
  135. },
  136. {
  137. method : 'i',
  138. url : 'am',
  139. headers : {pro:'xy'}
  140. });
  141. expect(outgoing.host).to.eql('how');
  142. expect(outgoing.hostname).to.eql('are');
  143. expect(outgoing.socketPath).to.eql('you');
  144. expect(outgoing.agent).to.eql('?');
  145. expect(outgoing.method).to.eql('i');
  146. expect(outgoing.path).to.eql('am');
  147. expect(outgoing.headers.pro).to.eql('xy');
  148. expect(outgoing.port).to.eql(443);
  149. });
  150. it('should keep the original target path in the outgoing path', function(){
  151. var outgoing = {};
  152. common.setupOutgoing(outgoing, {target:
  153. { path: 'some-path' }
  154. }, { url : 'am' });
  155. expect(outgoing.path).to.eql('some-path/am');
  156. });
  157. it('should keep the original forward path in the outgoing path', function(){
  158. var outgoing = {};
  159. common.setupOutgoing(outgoing, {
  160. target: {},
  161. forward: {
  162. path: 'some-path'
  163. }
  164. }, {
  165. url : 'am'
  166. }, 'forward');
  167. expect(outgoing.path).to.eql('some-path/am');
  168. });
  169. it('should not prepend the target path to the outgoing path with prependPath = false', function () {
  170. var outgoing = {};
  171. common.setupOutgoing(outgoing, {
  172. target: { path: 'hellothere' },
  173. prependPath: false
  174. }, { url: 'hi' });
  175. expect(outgoing.path).to.eql('hi');
  176. })
  177. it('should properly join paths', function () {
  178. var outgoing = {};
  179. common.setupOutgoing(outgoing, {
  180. target: { path: '/forward' },
  181. }, { url: '/static/path' });
  182. expect(outgoing.path).to.eql('/forward/static/path');
  183. })
  184. it('should not modify the query string', function () {
  185. var outgoing = {};
  186. common.setupOutgoing(outgoing, {
  187. target: { path: '/forward' },
  188. }, { url: '/?foo=bar//&target=http://foobar.com/' });
  189. expect(outgoing.path).to.eql('/forward/?foo=bar//&target=http://foobar.com/');
  190. })
  191. });
  192. describe('#setupSocket', function () {
  193. it('should setup a socket', function () {
  194. var socketConfig = {
  195. timeout: null,
  196. nodelay: false,
  197. keepalive: false
  198. },
  199. stubSocket = {
  200. setTimeout: function (num) {
  201. socketConfig.timeout = num;
  202. },
  203. setNoDelay: function (bol) {
  204. socketConfig.nodelay = bol;
  205. },
  206. setKeepAlive: function (bol) {
  207. socketConfig.keepalive = bol;
  208. }
  209. }
  210. returnValue = common.setupSocket(stubSocket);
  211. expect(socketConfig.timeout).to.eql(0);
  212. expect(socketConfig.nodelay).to.eql(true);
  213. expect(socketConfig.keepalive).to.eql(true);
  214. });
  215. });
  216. });