You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

test.js 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /**
  2. * Module dependencies.
  3. */
  4. var pac = require('../');
  5. var assert = require('assert');
  6. describe('FindProxyForURL', function () {
  7. it('should return `undefined` by default', function (done) {
  8. var FindProxyForURL = pac(
  9. 'function FindProxyForURL (url, host) {' +
  10. ' /* noop */' +
  11. '}'
  12. );
  13. FindProxyForURL('http://foo.com/', 'foo.com', function (err, res) {
  14. if (err) return done(err);
  15. assert.strictEqual(undefined, res);
  16. done();
  17. });
  18. });
  19. it('should return the value that gets returned', function (done) {
  20. var FindProxyForURL = pac(
  21. 'function FindProxyForURL (url, host) {' +
  22. ' return { foo: "bar" };' +
  23. '}'
  24. );
  25. FindProxyForURL('http://foo.com/', 'foo.com', function (err, res) {
  26. if (err) return done(err);
  27. assert.deepEqual({ foo: 'bar' }, res);
  28. done();
  29. });
  30. });
  31. describe('official docs Example #1', function () {
  32. var FindProxyForURL = pac(
  33. 'function FindProxyForURL(url, host) {' +
  34. ' if (isPlainHostName(host) ||' +
  35. ' dnsDomainIs(host, ".netscape.com"))' +
  36. ' return "DIRECT";' +
  37. ' else' +
  38. ' return "PROXY w3proxy.netscape.com:8080; DIRECT";' +
  39. '}'
  40. );
  41. it('should return "DIRECT" for "localhost"', function (done) {
  42. FindProxyForURL('http://localhost/hello', 'localhost', function (err, res) {
  43. if (err) return done(err);
  44. assert.equal('DIRECT', res);
  45. done();
  46. });
  47. });
  48. it('should return "DIRECT" for "foo.netscape.com"', function (done) {
  49. FindProxyForURL('http://foo.netscape.com/', 'foo.netscape.com', function (err, res) {
  50. if (err) return done(err);
  51. assert.equal('DIRECT', res);
  52. done();
  53. });
  54. });
  55. it('should return "PROXY …" for "google.com"', function (done) {
  56. FindProxyForURL('http://google.com/t', 'google.com', function (err, res) {
  57. if (err) return done(err);
  58. assert.equal('PROXY w3proxy.netscape.com:8080; DIRECT', res);
  59. done();
  60. });
  61. });
  62. });
  63. describe('official docs Example #1b', function () {
  64. var FindProxyForURL = pac(
  65. 'function FindProxyForURL(url, host)' +
  66. '{' +
  67. ' if ((isPlainHostName(host) ||' +
  68. ' dnsDomainIs(host, ".netscape.com")) &&' +
  69. ' !localHostOrDomainIs(host, "www.netscape.com") &&' +
  70. ' !localHostOrDomainIs(host, "merchant.netscape.com"))' +
  71. ' return "DIRECT";' +
  72. ' else' +
  73. ' return "PROXY w3proxy.netscape.com:8080; DIRECT";' +
  74. '}'
  75. );
  76. it('should return "DIRECT" for "localhost"', function (done) {
  77. FindProxyForURL('http://localhost/hello', 'localhost', function (err, res) {
  78. if (err) return done(err);
  79. assert.equal('DIRECT', res);
  80. done();
  81. });
  82. });
  83. it('should return "DIRECT" for "foo.netscape.com"', function (done) {
  84. FindProxyForURL('http://foo.netscape.com/', 'foo.netscape.com', function (err, res) {
  85. if (err) return done(err);
  86. assert.equal('DIRECT', res);
  87. done();
  88. });
  89. });
  90. it('should return "PROXY …" for "www.netscape.com"', function (done) {
  91. FindProxyForURL('http://www.netscape.com/', 'www.netscape.com', function (err, res) {
  92. if (err) return done(err);
  93. assert.equal('PROXY w3proxy.netscape.com:8080; DIRECT', res);
  94. done();
  95. });
  96. });
  97. it('should return "PROXY …" for "merchant.netscape.com"', function (done) {
  98. FindProxyForURL('http://merchant.netscape.com/', 'merchant.netscape.com', function (err, res) {
  99. if (err) return done(err);
  100. assert.equal('PROXY w3proxy.netscape.com:8080; DIRECT', res);
  101. done();
  102. });
  103. });
  104. });
  105. describe('official docs Example #5', function () {
  106. var FindProxyForURL = pac(
  107. 'function FindProxyForURL(url, host)' +
  108. '{' +
  109. ' if (url.substring(0, 5) == "http:") {' +
  110. ' return "PROXY http-proxy.mydomain.com:8080";' +
  111. ' }' +
  112. ' else if (url.substring(0, 4) == "ftp:") {' +
  113. ' return "PROXY ftp-proxy.mydomain.com:8080";' +
  114. ' }' +
  115. ' else if (url.substring(0, 7) == "gopher:") {' +
  116. ' return "PROXY gopher-proxy.mydomain.com:8080";' +
  117. ' }' +
  118. ' else if (url.substring(0, 6) == "https:" ||' +
  119. ' url.substring(0, 6) == "snews:") {' +
  120. ' return "PROXY security-proxy.mydomain.com:8080";' +
  121. ' }' +
  122. ' else {' +
  123. ' return "DIRECT";' +
  124. ' }' +
  125. '}'
  126. );
  127. it('should return "DIRECT" for "foo://netscape.com"', function (done) {
  128. FindProxyForURL('foo://netscape.com/hello', 'netscape.com', function (err, res) {
  129. if (err) return done(err);
  130. assert.equal('DIRECT', res);
  131. done();
  132. });
  133. });
  134. it('should return "PROXY http…" for "http://netscape.com"', function (done) {
  135. FindProxyForURL('http://netscape.com/hello', 'netscape.com', function (err, res) {
  136. if (err) return done(err);
  137. assert.equal('PROXY http-proxy.mydomain.com:8080', res);
  138. done();
  139. });
  140. });
  141. it('should return "PROXY ftp…" for "ftp://netscape.com"', function (done) {
  142. FindProxyForURL('ftp://netscape.com/hello', 'netscape.com', function (err, res) {
  143. if (err) return done(err);
  144. assert.equal('PROXY ftp-proxy.mydomain.com:8080', res);
  145. done();
  146. });
  147. });
  148. it('should return "PROXY gopher…" for "gopher://netscape.com"', function (done) {
  149. FindProxyForURL('gopher://netscape.com/hello', 'netscape.com', function (err, res) {
  150. if (err) return done(err);
  151. assert.equal('PROXY gopher-proxy.mydomain.com:8080', res);
  152. done();
  153. });
  154. });
  155. it('should return "PROXY security…" for "https://netscape.com"', function (done) {
  156. FindProxyForURL('https://netscape.com/hello', 'netscape.com', function (err, res) {
  157. if (err) return done(err);
  158. assert.equal('PROXY security-proxy.mydomain.com:8080', res);
  159. done();
  160. });
  161. });
  162. it('should return "PROXY security…" for "snews://netscape.com"', function (done) {
  163. FindProxyForURL('snews://netscape.com/hello', 'netscape.com', function (err, res) {
  164. if (err) return done(err);
  165. assert.equal('PROXY security-proxy.mydomain.com:8080', res);
  166. done();
  167. });
  168. });
  169. });
  170. describe('GitHub issue #3', function () {
  171. var FindProxyForURL = pac(
  172. 'function FindProxyForURL(url, host) {\n' +
  173. ' if (isHostInAnySubnet(host, ["10.1.2.0", "10.1.3.0"], "255.255.255.0")) {\n' +
  174. ' return "HTTPS proxy.example.com";\n' +
  175. ' }\n' +
  176. '\n' +
  177. ' if (isHostInAnySubnet(host, ["10.2.2.0", "10.2.3.0"], "255.255.255.0")) {\n' +
  178. ' return "HTTPS proxy.example.com";\n' +
  179. ' }\n' +
  180. '\n' +
  181. ' // Everything else, go direct:\n' +
  182. ' return "DIRECT";\n' +
  183. '}\n' +
  184. '\n' +
  185. '// Checks if the single host is within a list of subnets using the single mask.\n' +
  186. 'function isHostInAnySubnet(host, subnets, mask) {\n' +
  187. ' var subnets_length = subnets.length;\n' +
  188. ' for (i = 0; i < subnets_length; i++) {\n' +
  189. ' if (isInNet(host, subnets[i], mask)) {\n' +
  190. ' return true;\n' +
  191. ' }\n' +
  192. ' }\n' +
  193. '}\n'
  194. );
  195. it('should return "HTTPS proxy.example.com" for "http://10.1.2.3/bar.html"', function (done) {
  196. FindProxyForURL('http://10.1.2.3/bar.html', '10.1.2.3', function (err, res) {
  197. if (err) return done(err);
  198. assert.equal('HTTPS proxy.example.com', res);
  199. done();
  200. });
  201. });
  202. it('should return "DIRECT" for "http://foo.com/bar.html"', function (done) {
  203. FindProxyForURL('http://foo.com/bar.html', 'foo.com', function (err, res) {
  204. if (err) return done(err);
  205. assert.equal('DIRECT', res);
  206. done();
  207. });
  208. });
  209. });
  210. });