Skip to content

Commit 10c99ca

Browse files
committed
Refactor requests
1 parent 9e3d6ba commit 10c99ca

File tree

3 files changed

+43
-55
lines changed

3 files changed

+43
-55
lines changed

test/api_spec.js

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
var util = require("../lib/testutil");
55
var extend = require("util")._extend;
6-
var request = require("request-promise-native");
76
var log = require("winston");
87
// disable logging during tests
98
log.remove(log.transports.Console);
@@ -23,12 +22,23 @@ describe("API Tests", function () {
2322
proxy = newProxy;
2423
})
2524
.then(function () {
26-
r = request.defaults({
27-
method: "GET",
28-
headers: { Authorization: "token " + proxy.authToken },
29-
port: apiPort,
30-
url: apiUrl,
31-
});
25+
r = (options = {}) => {
26+
const path = options.path || '';
27+
delete options.path;
28+
return fetch({
29+
method: "GET",
30+
headers: {
31+
Authorization: `token ${proxy.authToken}`,
32+
},
33+
url: `${apiUrl}${path}`,
34+
...options,
35+
}).then((res) => {
36+
if (!res.ok) {
37+
throw res;
38+
}
39+
return res.text(); // return body
40+
});
41+
};
3242
})
3343
.then(function () {
3444
callback();
@@ -74,7 +84,7 @@ describe("API Tests", function () {
7484
});
7585

7686
it("GET /api/routes fetches the routing table", function (done) {
77-
r(apiUrl)
87+
r()
7888
.then(function (body) {
7989
var reply = JSON.parse(body);
8090
var keys = Object.keys(reply);
@@ -90,7 +100,7 @@ describe("API Tests", function () {
90100
proxy
91101
.addRoute(path, { target: url })
92102
.then(function () {
93-
return r(apiUrl + path);
103+
return r({ path });
94104
})
95105
.then(function (body) {
96106
var reply = JSON.parse(body);
@@ -102,7 +112,7 @@ describe("API Tests", function () {
102112
});
103113

104114
it("GET /api/routes[/path] fetches a single route (404 if missing)", function (done) {
105-
r(apiUrl + "/path")
115+
r({ path: "/path" })
106116
.then((body) => {
107117
done.fail("Expected a 404");
108118
})
@@ -116,8 +126,9 @@ describe("API Tests", function () {
116126
var port = 8998;
117127
var target = "http://127.0.0.1:" + port;
118128

119-
r.post({
120-
url: apiUrl + "/user/foo",
129+
r({
130+
method: 'POST',
131+
path: "/user/foo",
121132
body: JSON.stringify({ target: target }),
122133
})
123134
.then((body) => {
@@ -134,8 +145,9 @@ describe("API Tests", function () {
134145
it("POST /api/routes[/foo%20bar] handles URI escapes", function (done) {
135146
var port = 8998;
136147
var target = "http://127.0.0.1:" + port;
137-
r.post({
138-
url: apiUrl + "/user/foo%40bar",
148+
r({
149+
method: "POST",
150+
path: "/user/foo%40bar",
139151
body: JSON.stringify({ target: target }),
140152
})
141153
.then((body) => {
@@ -156,8 +168,8 @@ describe("API Tests", function () {
156168
it("POST /api/routes creates a new root route", function (done) {
157169
var port = 8998;
158170
var target = "http://127.0.0.1:" + port;
159-
r.post({
160-
url: apiUrl,
171+
r({
172+
method: "POST",
161173
body: JSON.stringify({ target: target }),
162174
})
163175
.then((body) => {
@@ -188,7 +200,7 @@ describe("API Tests", function () {
188200
});
189201

190202
it("GET /api/routes?inactiveSince= with bad value returns a 400", function (done) {
191-
r.get(apiUrl + "?inactiveSince=endoftheuniverse")
203+
r({ path: "?inactiveSince=endoftheuniverse" })
192204
.then(() => done.fail("Expected 400"))
193205
.catch((err) => expect(err.statusCode).toEqual(400))
194206
.then(done);
@@ -228,7 +240,7 @@ describe("API Tests", function () {
228240
var seen = 0;
229241
var doReq = function (i) {
230242
var t = tests[i];
231-
return r.get(apiUrl + "?inactiveSince=" + t.since.toISOString()).then(function (body) {
243+
return r({ path: "?inactiveSince=" + t.since.toISOString() }).then(function (body) {
232244
var routes = JSON.parse(body);
233245
var routeKeys = Object.keys(routes);
234246
var expectedKeys = Object.keys(t.expected);

test/cli_spec.js

Lines changed: 12 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
const http = require("http");
55
var spawn = require("child_process").spawn;
6-
var request = require("request-promise-native");
76

87
// utility functions
98
function executeCLI(execCmd = "bin/configurable-http-proxy", args = []) {
@@ -97,12 +96,6 @@ describe("CLI Tests", function () {
9796
var redirectUrl = "http://127.0.0.1:" + redirectPort;
9897
var redirectToUrl = "https://127.0.0.1:" + redirectToPort;
9998

100-
var r = request.defaults({
101-
method: "GET",
102-
//url: proxyUrl,
103-
followRedirect: false,
104-
strictSSL: false,
105-
});
10699

107100
beforeEach(function (callback) {
108101
childProcess = null;
@@ -125,8 +118,7 @@ describe("CLI Tests", function () {
125118
var args = ["--ip", "127.0.0.1", "--port", port, "--default-target", testUrl];
126119
executeCLI(execCmd, args).then((cliProcess) => {
127120
childProcess = cliProcess;
128-
r(proxyUrl).then((body) => {
129-
body = JSON.parse(body);
121+
fetch(proxyUrl).then((res) => res.json()).then((body) => {
130122
expect(body).toEqual(
131123
jasmine.objectContaining({
132124
name: "default",
@@ -152,8 +144,7 @@ describe("CLI Tests", function () {
152144
];
153145
executeCLI(execCmd, args).then((cliProcess) => {
154146
childProcess = cliProcess;
155-
r(SSLproxyUrl).then((body) => {
156-
body = JSON.parse(body);
147+
fetch(SSLproxyUrl).then((res) => res.json()).then((body) => {
157148
expect(body).toEqual(
158149
jasmine.objectContaining({
159150
name: "default",
@@ -182,16 +173,12 @@ describe("CLI Tests", function () {
182173
];
183174
executeCLI(execCmd, args).then((cliProcess) => {
184175
childProcess = cliProcess;
185-
r(redirectUrl)
186-
.then(() => {
187-
fail("A 301 redirect should have been thrown.");
188-
})
189-
.catch((requestError) => {
190-
expect(requestError.statusCode).toEqual(301);
191-
expect(requestError.response.headers.location).toContain(SSLproxyUrl);
176+
fetch(redirectUrl)
177+
.then((res) => {
178+
expect(res.statusCode).toEqual(301);
179+
expect(res.response.headers.location).toContain(SSLproxyUrl);
192180
});
193-
r({ url: redirectUrl, followRedirect: true }).then((body) => {
194-
body = JSON.parse(body);
181+
fetch({ url: redirectUrl, redirect: 'follow' }).then(res => res.json()).then((body) => {
195182
expect(body).toEqual(
196183
jasmine.objectContaining({
197184
name: "default",
@@ -222,13 +209,10 @@ describe("CLI Tests", function () {
222209
];
223210
executeCLI(execCmd, args).then((cliProcess) => {
224211
childProcess = cliProcess;
225-
r(redirectUrl)
226-
.then(() => {
227-
fail("A 301 redirect should have been thrown.");
228-
})
229-
.catch((requestError) => {
230-
expect(requestError.statusCode).toEqual(301);
231-
expect(requestError.response.headers.location).toContain(redirectToUrl);
212+
fetch(redirectUrl)
213+
.then((res) => {
214+
expect(res.statusCode).toEqual(301);
215+
expect(res.response.headers.location).toContain(redirectToUrl);
232216
done();
233217
});
234218
});
@@ -253,8 +237,7 @@ describe("CLI Tests", function () {
253237
];
254238
executeCLI(execCmd, args).then((cliProcess) => {
255239
childProcess = cliProcess;
256-
r(SSLproxyUrl).then((body) => {
257-
body = JSON.parse(body);
240+
fetch(SSLproxyUrl).then(res => res.json()).then((body) => {
258241
expect(body.headers).toEqual(
259242
jasmine.objectContaining({
260243
k1: "v1",

test/proxy_spec.js

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
var http = require("http");
55
var path = require("path");
66
var util = require("../lib/testutil");
7-
var request = require("request-promise-native");
87
var WebSocket = require("ws");
98

109
var ConfigurableProxy = require("../lib/configproxy").ConfigurableProxy;
@@ -19,11 +18,6 @@ describe("Proxy Tests", function () {
1918
var hostTest = "test.localhost.jovyan.org";
2019
var hostUrl = "http://" + hostTest + ":" + port;
2120

22-
var r = request.defaults({
23-
method: "GET",
24-
url: proxyUrl,
25-
followRedirect: false,
26-
});
2721

2822
beforeEach(function (callback) {
2923
util.setupProxy(port).then(function (newProxy) {
@@ -37,8 +31,7 @@ describe("Proxy Tests", function () {
3731
});
3832

3933
it("basic HTTP request", function (done) {
40-
r(proxyUrl).then((body) => {
41-
body = JSON.parse(body);
34+
fetch(proxyUrl).then(res => res.json()).then((body) => {
4235
expect(body).toEqual(
4336
jasmine.objectContaining({
4437
path: "/",

0 commit comments

Comments
 (0)