Skip to content

Commit

Permalink
Convert remaining to fetch
Browse files Browse the repository at this point in the history
  • Loading branch information
mjpsyapse committed Oct 1, 2024
1 parent 10c99ca commit b6eecab
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 122 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"scripts": {
"lint": "jshint bin/ lib/ test/",
"fmt": "pre-commit run --all-files",
"test": "FORCE_COLOR=3 nyc node test/jasmine.js",
"test": "NODE_TLS_REJECT_UNAUTHORIZED=0 FORCE_COLOR=3 nyc node test/jasmine.js",
"coverage-html": "nyc report --reporter=html",
"codecov": "nyc report --reporter=lcov && codecov"
}
Expand Down
62 changes: 28 additions & 34 deletions test/api_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,19 @@ describe("API Tests", function () {
proxy = newProxy;
})
.then(function () {
r = (options = {}) => {
const path = options.path || '';
delete options.path;
return fetch({
r = (path, options) => {
options = options || {};
path = path || '';
const url = `${options.url || apiUrl}${path}`;
delete options.url;
const fetchOptions = {
method: "GET",
headers: {
Authorization: `token ${proxy.authToken}`,
},
url: `${apiUrl}${path}`,
...options,
}).then((res) => {
if (!res.ok) {
throw res;
}
return res.text(); // return body
});
};
return fetch(url, fetchOptions);
};
})
.then(function () {
Expand Down Expand Up @@ -84,9 +81,7 @@ describe("API Tests", function () {
});

it("GET /api/routes fetches the routing table", function (done) {
r()
.then(function (body) {
var reply = JSON.parse(body);
r().then(res => res.json()).then(function (reply) {
var keys = Object.keys(reply);
expect(keys.length).toEqual(1);
expect(keys).toContain("/");
Expand All @@ -100,24 +95,22 @@ describe("API Tests", function () {
proxy
.addRoute(path, { target: url })
.then(function () {
return r({ path });
return r(path);
})
.then(function (body) {
var reply = JSON.parse(body);
.then(res => res.json())
.then(function (reply) {
var keys = Object.keys(reply);
expect(keys).toContain("target");
expect(reply.target).toEqual(url);
})
.catch(done.fail)
.then(done);
});

it("GET /api/routes[/path] fetches a single route (404 if missing)", function (done) {
r({ path: "/path" })
.then((body) => {
done.fail("Expected a 404");
})
.catch((error) => {
expect(error.statusCode).toEqual(404);
r("/path")
.then((res) => {
expect(res.status).toEqual(404);
})
.then(done);
});
Expand All @@ -126,11 +119,11 @@ describe("API Tests", function () {
var port = 8998;
var target = "http://127.0.0.1:" + port;

r({
r("/user/foo", {
method: 'POST',
path: "/user/foo",
body: JSON.stringify({ target: target }),
})
.then(res => res.text())
.then((body) => {
expect(body).toEqual("");
})
Expand All @@ -139,17 +132,18 @@ describe("API Tests", function () {
expect(route.target).toEqual(target);
expect(typeof route.last_activity).toEqual("object");
})
.catch(done.fail)
.then(done);
});

it("POST /api/routes[/foo%20bar] handles URI escapes", function (done) {
var port = 8998;
var target = "http://127.0.0.1:" + port;
r({
r("/user/foo%40bar", {
method: "POST",
path: "/user/foo%40bar",
body: JSON.stringify({ target: target }),
})
.then(res => res.text())
.then((body) => {
expect(body).toEqual("");
})
Expand All @@ -168,10 +162,11 @@ describe("API Tests", function () {
it("POST /api/routes creates a new root route", function (done) {
var port = 8998;
var target = "http://127.0.0.1:" + port;
r({
r('', {
method: "POST",
body: JSON.stringify({ target: target }),
})
.then(res => res.text())
.then((body) => {
expect(body).toEqual("");
return proxy._routes.get("/");
Expand All @@ -192,17 +187,17 @@ describe("API Tests", function () {
.addTarget(proxy, path, port, null, null)
.then(() => proxy._routes.get(path))
.then((route) => expect(route.target).toEqual(target))
.then(() => r.del(apiUrl + path))
.then(() => r(path, { url: apiUrl, method: "DELETE" }))
.then(res => res.text())
.then((body) => expect(body).toEqual(""))
.then(() => proxy._routes.get(path))
.then((deletedRoute) => expect(deletedRoute).toBe(undefined))
.then(done);
});

it("GET /api/routes?inactiveSince= with bad value returns a 400", function (done) {
r({ path: "?inactiveSince=endoftheuniverse" })
.then(() => done.fail("Expected 400"))
.catch((err) => expect(err.statusCode).toEqual(400))
r("?inactiveSince=endoftheuniverse")
.then((res) => expect(res.status).toEqual(400))
.then(done);
});

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

Expand Down
16 changes: 9 additions & 7 deletions test/cli_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,8 @@ describe("CLI Tests", function () {
})
);
done();
}).catch(err => {
done.fail(err);
});
});
});
Expand All @@ -173,12 +175,12 @@ describe("CLI Tests", function () {
];
executeCLI(execCmd, args).then((cliProcess) => {
childProcess = cliProcess;
fetch(redirectUrl)
fetch(redirectUrl, { redirect: 'manual'})
.then((res) => {
expect(res.statusCode).toEqual(301);
expect(res.response.headers.location).toContain(SSLproxyUrl);
expect(res.status).toEqual(301);
expect(res.headers.get('location')).toContain(SSLproxyUrl);
});
fetch({ url: redirectUrl, redirect: 'follow' }).then(res => res.json()).then((body) => {
fetch(redirectUrl, { redirect: 'follow' }).then(res => res.json()).then((body) => {
expect(body).toEqual(
jasmine.objectContaining({
name: "default",
Expand Down Expand Up @@ -209,10 +211,10 @@ describe("CLI Tests", function () {
];
executeCLI(execCmd, args).then((cliProcess) => {
childProcess = cliProcess;
fetch(redirectUrl)
fetch(redirectUrl, { redirect: 'manual'})
.then((res) => {
expect(res.statusCode).toEqual(301);
expect(res.response.headers.location).toContain(redirectToUrl);
expect(res.status).toEqual(301);
expect(res.headers.get('location')).toContain(redirectToUrl);
done();
});
});
Expand Down
Loading

0 comments on commit b6eecab

Please sign in to comment.