UNPKG

4.06 kBJavaScriptView Raw
1
2/**
3 * Module dependencies.
4 */
5
6var net = require('net');
7var url = require('url');
8var assert = require('assert');
9var request = require('superagent');
10
11// extend with .proxy()
12require('../')(request);
13
14describe('superagent-proxy', function () {
15
16 this.slow(5000);
17 this.timeout(10000);
18
19 var httpLink = 'http://jsonip.com/';
20 var httpsLink = 'https://graph.facebook.com/tootallnate';
21
22 describe('superagent.Request#proxy()', function () {
23 it('should be a function', function () {
24 assert.equal('function', typeof request.Request.prototype.proxy);
25 });
26 it('should accept a "string" proxy URI', function () {
27 var req = request.get('http://foo.com');
28 req.proxy('http://example.com');
29 });
30 it('should accept an options "object" with proxy info', function () {
31 var req = request.get('http://foo.com');
32 req.proxy({
33 protocol: 'https',
34 host: 'proxy.org',
35 port: 8080
36 });
37 });
38 it('should throw on an options "object" without "protocol"', function () {
39 var req = request.get('http://foo.com');
40 try {
41 req.proxy({
42 host: 'proxy.org',
43 port: 8080
44 });
45 assert(false, 'should be unreachable');
46 } catch (e) {
47 assert.equal('TypeError', e.name);
48 assert(/\bhttp\b/.test(e.message));
49 assert(/\bhttps\b/.test(e.message));
50 assert(/\bsocks\b/.test(e.message));
51 }
52 });
53 });
54
55 describe('http: - HTTP proxy', function () {
56 var proxy = process.env.HTTP_PROXY || process.env.http_proxy || 'http://10.1.10.200:3128';
57
58 it('should work against an HTTP endpoint', function (done) {
59 request
60 .get(httpLink)
61 .proxy(proxy)
62 .end(function (res) {
63 var data = res.body;
64 assert('ip' in data);
65 var ips = data.ip.split(/\,\s*/g);
66 assert(ips.length >= 1);
67 ips.forEach(function (ip) {
68 assert(net.isIP(ip));
69 });
70 done();
71 });
72 });
73
74 it('should work against an HTTPS endpoint', function (done) {
75 request
76 .get(httpsLink)
77 .proxy(proxy)
78 .end(function (res) {
79 var data = JSON.parse(res.text);
80 assert.equal('tootallnate', data.username);
81 done();
82 });
83 });
84 });
85
86 describe('https: - HTTPS proxy', function () {
87 var proxy = process.env.HTTPS_PROXY || process.env.https_proxy || 'https://10.1.10.200:3130';
88
89 it('should work against an HTTP endpoint', function (done) {
90 var p = url.parse(proxy);
91 p.rejectUnauthorized = false;
92
93 request
94 .get(httpLink)
95 .proxy(p)
96 .end(function (res) {
97 var data = res.body;
98 assert('ip' in data);
99 var ips = data.ip.split(/\,\s*/g);
100 assert(ips.length >= 1);
101 ips.forEach(function (ip) {
102 assert(net.isIP(ip));
103 });
104 done();
105 });
106 });
107
108 it('should work against an HTTPS endpoint', function (done) {
109 var p = url.parse(proxy);
110 p.rejectUnauthorized = false;
111
112 request
113 .get(httpsLink)
114 .proxy(p)
115 .end(function (res) {
116 var data = JSON.parse(res.text);
117 assert.equal('tootallnate', data.username);
118 done();
119 });
120 });
121 });
122
123 describe('socks: - SOCKS proxy', function () {
124 var proxy = process.env.SOCKS_PROXY || process.env.socks_proxy || 'socks://127.0.0.1:9050';
125
126 it('should work against an HTTP endpoint', function (done) {
127 request
128 .get(httpLink)
129 .proxy(proxy)
130 .end(function (res) {
131 var data = res.body;
132 assert('ip' in data);
133 var ips = data.ip.split(/\,\s*/g);
134 assert(ips.length >= 1);
135 ips.forEach(function (ip) {
136 assert(net.isIP(ip));
137 });
138 done();
139 });
140 });
141
142 it('should work against an HTTPS endpoint', function (done) {
143 request
144 .get(httpsLink)
145 .proxy(proxy)
146 .end(function (res) {
147 var data = JSON.parse(res.text);
148 assert.equal('tootallnate', data.username);
149 done();
150 });
151 });
152 });
153
154});