Skip to content

Commit b6eecab

Browse files
committed
Convert remaining to fetch
1 parent 10c99ca commit b6eecab

File tree

4 files changed

+125
-122
lines changed

4 files changed

+125
-122
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
"scripts": {
3939
"lint": "jshint bin/ lib/ test/",
4040
"fmt": "pre-commit run --all-files",
41-
"test": "FORCE_COLOR=3 nyc node test/jasmine.js",
41+
"test": "NODE_TLS_REJECT_UNAUTHORIZED=0 FORCE_COLOR=3 nyc node test/jasmine.js",
4242
"coverage-html": "nyc report --reporter=html",
4343
"codecov": "nyc report --reporter=lcov && codecov"
4444
}

test/api_spec.js

Lines changed: 28 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -22,22 +22,19 @@ describe("API Tests", function () {
2222
proxy = newProxy;
2323
})
2424
.then(function () {
25-
r = (options = {}) => {
26-
const path = options.path || '';
27-
delete options.path;
28-
return fetch({
25+
r = (path, options) => {
26+
options = options || {};
27+
path = path || '';
28+
const url = `${options.url || apiUrl}${path}`;
29+
delete options.url;
30+
const fetchOptions = {
2931
method: "GET",
3032
headers: {
3133
Authorization: `token ${proxy.authToken}`,
3234
},
33-
url: `${apiUrl}${path}`,
3435
...options,
35-
}).then((res) => {
36-
if (!res.ok) {
37-
throw res;
38-
}
39-
return res.text(); // return body
40-
});
36+
};
37+
return fetch(url, fetchOptions);
4138
};
4239
})
4340
.then(function () {
@@ -84,9 +81,7 @@ describe("API Tests", function () {
8481
});
8582

8683
it("GET /api/routes fetches the routing table", function (done) {
87-
r()
88-
.then(function (body) {
89-
var reply = JSON.parse(body);
84+
r().then(res => res.json()).then(function (reply) {
9085
var keys = Object.keys(reply);
9186
expect(keys.length).toEqual(1);
9287
expect(keys).toContain("/");
@@ -100,24 +95,22 @@ describe("API Tests", function () {
10095
proxy
10196
.addRoute(path, { target: url })
10297
.then(function () {
103-
return r({ path });
98+
return r(path);
10499
})
105-
.then(function (body) {
106-
var reply = JSON.parse(body);
100+
.then(res => res.json())
101+
.then(function (reply) {
107102
var keys = Object.keys(reply);
108103
expect(keys).toContain("target");
109104
expect(reply.target).toEqual(url);
110105
})
106+
.catch(done.fail)
111107
.then(done);
112108
});
113109

114110
it("GET /api/routes[/path] fetches a single route (404 if missing)", function (done) {
115-
r({ path: "/path" })
116-
.then((body) => {
117-
done.fail("Expected a 404");
118-
})
119-
.catch((error) => {
120-
expect(error.statusCode).toEqual(404);
111+
r("/path")
112+
.then((res) => {
113+
expect(res.status).toEqual(404);
121114
})
122115
.then(done);
123116
});
@@ -126,11 +119,11 @@ describe("API Tests", function () {
126119
var port = 8998;
127120
var target = "http://127.0.0.1:" + port;
128121

129-
r({
122+
r("/user/foo", {
130123
method: 'POST',
131-
path: "/user/foo",
132124
body: JSON.stringify({ target: target }),
133125
})
126+
.then(res => res.text())
134127
.then((body) => {
135128
expect(body).toEqual("");
136129
})
@@ -139,17 +132,18 @@ describe("API Tests", function () {
139132
expect(route.target).toEqual(target);
140133
expect(typeof route.last_activity).toEqual("object");
141134
})
135+
.catch(done.fail)
142136
.then(done);
143137
});
144138

145139
it("POST /api/routes[/foo%20bar] handles URI escapes", function (done) {
146140
var port = 8998;
147141
var target = "http://127.0.0.1:" + port;
148-
r({
142+
r("/user/foo%40bar", {
149143
method: "POST",
150-
path: "/user/foo%40bar",
151144
body: JSON.stringify({ target: target }),
152145
})
146+
.then(res => res.text())
153147
.then((body) => {
154148
expect(body).toEqual("");
155149
})
@@ -168,10 +162,11 @@ describe("API Tests", function () {
168162
it("POST /api/routes creates a new root route", function (done) {
169163
var port = 8998;
170164
var target = "http://127.0.0.1:" + port;
171-
r({
165+
r('', {
172166
method: "POST",
173167
body: JSON.stringify({ target: target }),
174168
})
169+
.then(res => res.text())
175170
.then((body) => {
176171
expect(body).toEqual("");
177172
return proxy._routes.get("/");
@@ -192,17 +187,17 @@ describe("API Tests", function () {
192187
.addTarget(proxy, path, port, null, null)
193188
.then(() => proxy._routes.get(path))
194189
.then((route) => expect(route.target).toEqual(target))
195-
.then(() => r.del(apiUrl + path))
190+
.then(() => r(path, { url: apiUrl, method: "DELETE" }))
191+
.then(res => res.text())
196192
.then((body) => expect(body).toEqual(""))
197193
.then(() => proxy._routes.get(path))
198194
.then((deletedRoute) => expect(deletedRoute).toBe(undefined))
199195
.then(done);
200196
});
201197

202198
it("GET /api/routes?inactiveSince= with bad value returns a 400", function (done) {
203-
r({ path: "?inactiveSince=endoftheuniverse" })
204-
.then(() => done.fail("Expected 400"))
205-
.catch((err) => expect(err.statusCode).toEqual(400))
199+
r("?inactiveSince=endoftheuniverse")
200+
.then((res) => expect(res.status).toEqual(400))
206201
.then(done);
207202
});
208203

@@ -240,8 +235,7 @@ describe("API Tests", function () {
240235
var seen = 0;
241236
var doReq = function (i) {
242237
var t = tests[i];
243-
return r({ path: "?inactiveSince=" + t.since.toISOString() }).then(function (body) {
244-
var routes = JSON.parse(body);
238+
return r("?inactiveSince=" + t.since.toISOString()).then(res => res.json()).then(function (routes) {
245239
var routeKeys = Object.keys(routes);
246240
var expectedKeys = Object.keys(t.expected);
247241

test/cli_spec.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,8 @@ describe("CLI Tests", function () {
151151
})
152152
);
153153
done();
154+
}).catch(err => {
155+
done.fail(err);
154156
});
155157
});
156158
});
@@ -173,12 +175,12 @@ describe("CLI Tests", function () {
173175
];
174176
executeCLI(execCmd, args).then((cliProcess) => {
175177
childProcess = cliProcess;
176-
fetch(redirectUrl)
178+
fetch(redirectUrl, { redirect: 'manual'})
177179
.then((res) => {
178-
expect(res.statusCode).toEqual(301);
179-
expect(res.response.headers.location).toContain(SSLproxyUrl);
180+
expect(res.status).toEqual(301);
181+
expect(res.headers.get('location')).toContain(SSLproxyUrl);
180182
});
181-
fetch({ url: redirectUrl, redirect: 'follow' }).then(res => res.json()).then((body) => {
183+
fetch(redirectUrl, { redirect: 'follow' }).then(res => res.json()).then((body) => {
182184
expect(body).toEqual(
183185
jasmine.objectContaining({
184186
name: "default",
@@ -209,10 +211,10 @@ describe("CLI Tests", function () {
209211
];
210212
executeCLI(execCmd, args).then((cliProcess) => {
211213
childProcess = cliProcess;
212-
fetch(redirectUrl)
214+
fetch(redirectUrl, { redirect: 'manual'})
213215
.then((res) => {
214-
expect(res.statusCode).toEqual(301);
215-
expect(res.response.headers.location).toContain(redirectToUrl);
216+
expect(res.status).toEqual(301);
217+
expect(res.headers.get('location')).toContain(redirectToUrl);
216218
done();
217219
});
218220
});

0 commit comments

Comments
 (0)