Page MenuHomeSealhub

server.test.js
No OneTemporary

Authored By
Unknown
Size
3 KB
Referenced Files
None
Subscribers
None

server.test.js

const axios = require("axios");
const locreq = require("locreq")(__dirname);
const FormData = require("form-data");
const assert = require("assert");
const { anyone, noone } = locreq("index.js").AccessStrategies;
describe("http", () => {
let base_url;
before(() => {
base_url = `http://localhost:${TestApp.ConfigManager.get("http.port")}`;
});
it("should provide a /health endpoint", () => {
return axios.get(`${base_url}/health`);
});
it("should add a header with app version", () =>
axios
.get(`${base_url}/health`)
.then(response =>
assert.equal(response.headers["x-sealious-app-version"], "0.0.0-test")
));
describe("router", () => {
describe("addRoute", () => {
before(() => {
TestApp.Server.addRoute({
method: "post",
path: "/test/echo",
access_strategy: anyone,
handler: ctx => (ctx.body = ctx.params),
});
TestApp.Server.addRoute({
method: "get",
path: "/test/forbidden",
access_strategy: noone,
handler: ctx => {},
});
});
it("should parse nested props both from query params and from the response body (multipart/form-data)", () => {
const form = new FormData();
form.append("field", "value");
form.append("nested.field", "deep-value");
form.append("array[]", "");
form.append("array[0]", "zero");
return axios
.post(`${base_url}/test/echo?array[1]=one`, form, {
headers: form.getHeaders(),
})
.then(response =>
assert.deepEqual(response.data, {
field: "value",
nested: { field: "deep-value" },
array: ["zero", "one"],
})
);
});
it("should parse nested props both from query params and from the response body (json)", () =>
axios
.post(
`${base_url}/test/echo?array[1]=one`,
{
field: "value",
nested: { field: "deep-value" },
array: ["zero"],
},
{
headers: { "Content-type": "application/json" },
}
)
.then(response => {
assert.deepEqual(response.data, {
field: "value",
nested: { field: "deep-value" },
array: ["zero", "one"],
});
}));
it("should throw a 401 if the access strategy for given path denies access", () =>
axios
.get(`${base_url}/test/forbidden`)
.then(() => {
throw new Error("It didn't throw an error");
})
.catch(error => {
assert.equal(error.response.status, 401);
assert.equal(error.response.data, "Noone can view this resource");
}));
});
describe("addStaticRoute", () => {
before(() => {
TestApp.Server.addStaticRoute("/test/static/", __dirname);
TestApp.Server.addStaticRoute("/test/forbidden/", __dirname, noone);
});
it("should throw if the given path is not absolute", () =>
assert.throws(() => TestApp.Server.addStaticRoute("/test/", "test")));
it("should serve files relative to the root of the application when given a relative path", () => {
return axios.get(`${base_url}/test/static/server.test.js`);
});
it("should obey given access strategy", () => {
return axios
.get(`${base_url}/test/forbidden/server.test.js`)
.catch(error => assert.equal(error.response.status, 401));
});
});
});
});

File Metadata

Mime Type
application/javascript
Expires
Fri, Nov 27, 17:25 (1 d, 15 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
1229237
Default Alt Text
server.test.js (3 KB)

Event Timeline