2023-04-06 19:56:46 -06:00
|
|
|
process.env.NODE_ENV = "test";
|
|
|
|
|
|
|
|
import * as assert from "assert";
|
|
|
|
import * as childProcess from "child_process";
|
|
|
|
import * as openapi from "@redocly/openapi-core";
|
|
|
|
import {
|
|
|
|
async,
|
|
|
|
startServer,
|
|
|
|
signup,
|
|
|
|
post,
|
|
|
|
request,
|
|
|
|
simpleGet,
|
|
|
|
port,
|
|
|
|
shutdownServer,
|
|
|
|
} from "./utils.js";
|
2021-03-12 07:10:15 -07:00
|
|
|
|
|
|
|
// Request Accept
|
2023-04-06 19:56:46 -06:00
|
|
|
const ONLY_AP = "application/activity+json";
|
|
|
|
const PREFER_AP = "application/activity+json, */*";
|
|
|
|
const PREFER_HTML = "text/html, */*";
|
|
|
|
const UNSPECIFIED = "*/*";
|
2021-03-12 07:10:15 -07:00
|
|
|
|
|
|
|
// Response Contet-Type
|
2023-04-06 19:56:46 -06:00
|
|
|
const AP = "application/activity+json; charset=utf-8";
|
|
|
|
const JSON = "application/json; charset=utf-8";
|
|
|
|
const HTML = "text/html; charset=utf-8";
|
2021-03-12 07:10:15 -07:00
|
|
|
|
2023-04-06 19:56:46 -06:00
|
|
|
describe("Fetch resource", () => {
|
2021-03-12 07:10:15 -07:00
|
|
|
let p: childProcess.ChildProcess;
|
|
|
|
|
|
|
|
let alice: any;
|
|
|
|
let alicesPost: any;
|
|
|
|
|
2021-06-12 07:40:17 -06:00
|
|
|
before(async () => {
|
|
|
|
p = await startServer();
|
2023-04-06 19:56:46 -06:00
|
|
|
alice = await signup({ username: "alice" });
|
2021-03-12 07:10:15 -07:00
|
|
|
alicesPost = await post(alice, {
|
2023-04-06 19:56:46 -06:00
|
|
|
text: "test",
|
2021-03-12 07:10:15 -07:00
|
|
|
});
|
2021-06-12 07:40:17 -06:00
|
|
|
});
|
2021-03-12 07:10:15 -07:00
|
|
|
|
2021-06-04 23:54:07 -06:00
|
|
|
after(async () => {
|
|
|
|
await shutdownServer(p);
|
2021-03-12 07:10:15 -07:00
|
|
|
});
|
|
|
|
|
2023-04-06 19:56:46 -06:00
|
|
|
describe("Common", () => {
|
|
|
|
it("meta", async(async () => {
|
|
|
|
const res = await request("/meta", {});
|
2021-03-12 07:10:15 -07:00
|
|
|
|
|
|
|
assert.strictEqual(res.status, 200);
|
|
|
|
}));
|
|
|
|
|
2023-04-06 19:56:46 -06:00
|
|
|
it("GET root", async(async () => {
|
|
|
|
const res = await simpleGet("/");
|
2021-03-12 07:10:15 -07:00
|
|
|
assert.strictEqual(res.status, 200);
|
2021-03-13 07:22:54 -07:00
|
|
|
assert.strictEqual(res.type, HTML);
|
2021-03-12 07:10:15 -07:00
|
|
|
}));
|
|
|
|
|
2023-04-06 19:56:46 -06:00
|
|
|
it("GET docs", async(async () => {
|
|
|
|
const res = await simpleGet("/docs/ja-JP/about");
|
2021-03-12 07:10:15 -07:00
|
|
|
assert.strictEqual(res.status, 200);
|
2021-03-13 07:22:54 -07:00
|
|
|
assert.strictEqual(res.type, HTML);
|
2021-03-12 07:10:15 -07:00
|
|
|
}));
|
|
|
|
|
2023-04-06 19:56:46 -06:00
|
|
|
it("GET api-doc", async(async () => {
|
|
|
|
const res = await simpleGet("/api-doc");
|
2021-03-12 07:10:15 -07:00
|
|
|
assert.strictEqual(res.status, 200);
|
2021-03-13 07:22:54 -07:00
|
|
|
assert.strictEqual(res.type, HTML);
|
2021-03-12 07:10:15 -07:00
|
|
|
}));
|
|
|
|
|
2023-04-06 19:56:46 -06:00
|
|
|
it("GET api.json", async(async () => {
|
|
|
|
const res = await simpleGet("/api.json");
|
2021-03-12 07:10:15 -07:00
|
|
|
assert.strictEqual(res.status, 200);
|
2021-03-13 07:22:54 -07:00
|
|
|
assert.strictEqual(res.type, JSON);
|
2021-03-12 07:10:15 -07:00
|
|
|
}));
|
2021-03-12 08:18:46 -07:00
|
|
|
|
2023-04-06 19:56:46 -06:00
|
|
|
it("Validate api.json", async(async () => {
|
2021-05-05 04:02:30 -06:00
|
|
|
const config = await openapi.loadConfig();
|
|
|
|
const result = await openapi.bundle({
|
|
|
|
config,
|
2022-05-21 07:21:41 -06:00
|
|
|
ref: `http://localhost:${port}/api.json`,
|
2021-05-05 04:02:30 -06:00
|
|
|
});
|
|
|
|
|
|
|
|
for (const problem of result.problems) {
|
|
|
|
console.log(`${problem.message} - ${problem.location[0]?.pointer}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.strictEqual(result.problems.length, 0);
|
|
|
|
}));
|
|
|
|
|
2023-04-06 19:56:46 -06:00
|
|
|
it("GET favicon.ico", async(async () => {
|
|
|
|
const res = await simpleGet("/favicon.ico");
|
2021-03-12 08:18:46 -07:00
|
|
|
assert.strictEqual(res.status, 200);
|
2023-04-06 19:56:46 -06:00
|
|
|
assert.strictEqual(res.type, "image/x-icon");
|
2021-03-12 08:18:46 -07:00
|
|
|
}));
|
2021-03-12 21:21:33 -07:00
|
|
|
|
2023-04-06 19:56:46 -06:00
|
|
|
it("GET apple-touch-icon.png", async(async () => {
|
|
|
|
const res = await simpleGet("/apple-touch-icon.png");
|
2021-03-12 21:21:33 -07:00
|
|
|
assert.strictEqual(res.status, 200);
|
2023-04-06 19:56:46 -06:00
|
|
|
assert.strictEqual(res.type, "image/png");
|
2021-03-12 21:21:33 -07:00
|
|
|
}));
|
2021-10-24 06:10:45 -06:00
|
|
|
|
2023-04-06 19:56:46 -06:00
|
|
|
it("GET twemoji svg", async(async () => {
|
|
|
|
const res = await simpleGet("/twemoji/2764.svg");
|
2021-10-24 06:10:45 -06:00
|
|
|
assert.strictEqual(res.status, 200);
|
2023-04-06 19:56:46 -06:00
|
|
|
assert.strictEqual(res.type, "image/svg+xml");
|
2021-10-24 06:10:45 -06:00
|
|
|
}));
|
|
|
|
|
2023-04-06 19:56:46 -06:00
|
|
|
it("GET twemoji svg with hyphen", async(async () => {
|
|
|
|
const res = await simpleGet("/twemoji/2764-fe0f-200d-1f525.svg");
|
2021-10-24 06:10:45 -06:00
|
|
|
assert.strictEqual(res.status, 200);
|
2023-04-06 19:56:46 -06:00
|
|
|
assert.strictEqual(res.type, "image/svg+xml");
|
2021-10-24 06:10:45 -06:00
|
|
|
}));
|
2021-03-12 07:10:15 -07:00
|
|
|
});
|
|
|
|
|
2023-04-06 19:56:46 -06:00
|
|
|
describe("/@:username", () => {
|
|
|
|
it("Only AP => AP", async(async () => {
|
2021-03-12 07:10:15 -07:00
|
|
|
const res = await simpleGet(`/@${alice.username}`, ONLY_AP);
|
|
|
|
assert.strictEqual(res.status, 200);
|
|
|
|
assert.strictEqual(res.type, AP);
|
|
|
|
}));
|
|
|
|
|
2023-04-06 19:56:46 -06:00
|
|
|
it("Prefer AP => AP", async(async () => {
|
2021-03-12 07:10:15 -07:00
|
|
|
const res = await simpleGet(`/@${alice.username}`, PREFER_AP);
|
|
|
|
assert.strictEqual(res.status, 200);
|
|
|
|
assert.strictEqual(res.type, AP);
|
|
|
|
}));
|
|
|
|
|
2023-04-06 19:56:46 -06:00
|
|
|
it("Prefer HTML => HTML", async(async () => {
|
2021-03-12 07:10:15 -07:00
|
|
|
const res = await simpleGet(`/@${alice.username}`, PREFER_HTML);
|
|
|
|
assert.strictEqual(res.status, 200);
|
|
|
|
assert.strictEqual(res.type, HTML);
|
|
|
|
}));
|
|
|
|
|
2023-04-06 19:56:46 -06:00
|
|
|
it("Unspecified => HTML", async(async () => {
|
2021-03-12 07:10:15 -07:00
|
|
|
const res = await simpleGet(`/@${alice.username}`, UNSPECIFIED);
|
|
|
|
assert.strictEqual(res.status, 200);
|
|
|
|
assert.strictEqual(res.type, HTML);
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
|
2023-04-06 19:56:46 -06:00
|
|
|
describe("/users/:id", () => {
|
|
|
|
it("Only AP => AP", async(async () => {
|
2021-03-12 07:10:15 -07:00
|
|
|
const res = await simpleGet(`/users/${alice.id}`, ONLY_AP);
|
|
|
|
assert.strictEqual(res.status, 200);
|
|
|
|
assert.strictEqual(res.type, AP);
|
|
|
|
}));
|
|
|
|
|
2023-04-06 19:56:46 -06:00
|
|
|
it("Prefer AP => AP", async(async () => {
|
2021-03-12 07:10:15 -07:00
|
|
|
const res = await simpleGet(`/users/${alice.id}`, PREFER_AP);
|
|
|
|
assert.strictEqual(res.status, 200);
|
|
|
|
assert.strictEqual(res.type, AP);
|
|
|
|
}));
|
|
|
|
|
2023-04-06 19:56:46 -06:00
|
|
|
it("Prefer HTML => Redirect to /@:username", async(async () => {
|
2021-03-12 07:10:15 -07:00
|
|
|
const res = await simpleGet(`/users/${alice.id}`, PREFER_HTML);
|
|
|
|
assert.strictEqual(res.status, 302);
|
|
|
|
assert.strictEqual(res.location, `/@${alice.username}`);
|
|
|
|
}));
|
|
|
|
|
2023-04-06 19:56:46 -06:00
|
|
|
it("Undecided => HTML", async(async () => {
|
2021-03-12 07:10:15 -07:00
|
|
|
const res = await simpleGet(`/users/${alice.id}`, UNSPECIFIED);
|
|
|
|
assert.strictEqual(res.status, 302);
|
|
|
|
assert.strictEqual(res.location, `/@${alice.username}`);
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
|
2023-04-06 19:56:46 -06:00
|
|
|
describe("/notes/:id", () => {
|
|
|
|
it("Only AP => AP", async(async () => {
|
2021-03-12 07:10:15 -07:00
|
|
|
const res = await simpleGet(`/notes/${alicesPost.id}`, ONLY_AP);
|
|
|
|
assert.strictEqual(res.status, 200);
|
|
|
|
assert.strictEqual(res.type, AP);
|
|
|
|
}));
|
|
|
|
|
2023-04-06 19:56:46 -06:00
|
|
|
it("Prefer AP => AP", async(async () => {
|
2021-03-12 07:10:15 -07:00
|
|
|
const res = await simpleGet(`/notes/${alicesPost.id}`, PREFER_AP);
|
|
|
|
assert.strictEqual(res.status, 200);
|
|
|
|
assert.strictEqual(res.type, AP);
|
|
|
|
}));
|
|
|
|
|
2023-04-06 19:56:46 -06:00
|
|
|
it("Prefer HTML => HTML", async(async () => {
|
2021-03-12 07:10:15 -07:00
|
|
|
const res = await simpleGet(`/notes/${alicesPost.id}`, PREFER_HTML);
|
|
|
|
assert.strictEqual(res.status, 200);
|
|
|
|
assert.strictEqual(res.type, HTML);
|
|
|
|
}));
|
|
|
|
|
2023-04-06 19:56:46 -06:00
|
|
|
it("Unspecified => HTML", async(async () => {
|
2021-03-12 07:10:15 -07:00
|
|
|
const res = await simpleGet(`/notes/${alicesPost.id}`, UNSPECIFIED);
|
|
|
|
assert.strictEqual(res.status, 200);
|
|
|
|
assert.strictEqual(res.type, HTML);
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
|
2023-04-06 19:56:46 -06:00
|
|
|
describe("Feeds", () => {
|
|
|
|
it("RSS", async(async () => {
|
2021-03-12 07:10:15 -07:00
|
|
|
const res = await simpleGet(`/@${alice.username}.rss`, UNSPECIFIED);
|
|
|
|
assert.strictEqual(res.status, 200);
|
2023-04-06 19:56:46 -06:00
|
|
|
assert.strictEqual(res.type, "application/rss+xml; charset=utf-8");
|
2021-03-12 07:10:15 -07:00
|
|
|
}));
|
|
|
|
|
2023-04-06 19:56:46 -06:00
|
|
|
it("ATOM", async(async () => {
|
2021-03-12 07:10:15 -07:00
|
|
|
const res = await simpleGet(`/@${alice.username}.atom`, UNSPECIFIED);
|
|
|
|
assert.strictEqual(res.status, 200);
|
2023-04-06 19:56:46 -06:00
|
|
|
assert.strictEqual(res.type, "application/atom+xml; charset=utf-8");
|
2021-03-12 07:10:15 -07:00
|
|
|
}));
|
|
|
|
|
2023-04-06 19:56:46 -06:00
|
|
|
it("JSON", async(async () => {
|
2021-03-12 07:10:15 -07:00
|
|
|
const res = await simpleGet(`/@${alice.username}.json`, UNSPECIFIED);
|
|
|
|
assert.strictEqual(res.status, 200);
|
2023-04-06 19:56:46 -06:00
|
|
|
assert.strictEqual(res.type, "application/json; charset=utf-8");
|
2021-03-12 07:10:15 -07:00
|
|
|
}));
|
|
|
|
});
|
|
|
|
});
|