feat: add mock batch submit endpoint
This commit is contained in:
@@ -12,34 +12,49 @@ export function createMockProtectedApiServer({ port = 4319 } = {}) {
|
||||
return `http://127.0.0.1:${resolvedPort}`;
|
||||
},
|
||||
async start() {
|
||||
server = http.createServer((request, response) => {
|
||||
if (request.url !== "/api/mock/protected") {
|
||||
response.writeHead(404, { "content-type": "application/json" });
|
||||
response.end(JSON.stringify({ ok: false, error: "not-found" }));
|
||||
server = http.createServer(async (request, response) => {
|
||||
if (request.url === "/api/mock/protected") {
|
||||
const authHeader = readBearerToken(request, response);
|
||||
if (!authHeader) {
|
||||
return;
|
||||
}
|
||||
|
||||
response.writeHead(200, { "content-type": "application/json" });
|
||||
response.end(
|
||||
JSON.stringify({
|
||||
ok: true,
|
||||
source: "mock-protected-api",
|
||||
message: "authorized",
|
||||
receivedAuthHeader: authHeader
|
||||
})
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
const authHeader = request.headers.authorization ?? "";
|
||||
const isBearer =
|
||||
typeof authHeader === "string" &&
|
||||
authHeader.startsWith("Bearer ") &&
|
||||
authHeader.length > "Bearer ".length;
|
||||
if (request.url === "/api/mock/batches" && request.method === "POST") {
|
||||
const authHeader = readBearerToken(request, response);
|
||||
if (!authHeader) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!isBearer) {
|
||||
response.writeHead(401, { "content-type": "application/json" });
|
||||
response.end(JSON.stringify({ ok: false, error: "unauthorized" }));
|
||||
const payload = await readJsonBody(request);
|
||||
const authors = Array.isArray(payload?.authors) ? payload.authors : [];
|
||||
response.writeHead(200, { "content-type": "application/json" });
|
||||
response.end(
|
||||
JSON.stringify({
|
||||
ok: true,
|
||||
source: "mock-batch-submit",
|
||||
acceptedCount: authors.length,
|
||||
batchId:
|
||||
typeof payload?.batchId === "string" ? payload.batchId : null,
|
||||
receivedAuthHeader: authHeader
|
||||
})
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
response.writeHead(200, { "content-type": "application/json" });
|
||||
response.end(
|
||||
JSON.stringify({
|
||||
ok: true,
|
||||
source: "mock-protected-api",
|
||||
message: "authorized",
|
||||
receivedAuthHeader: authHeader
|
||||
})
|
||||
);
|
||||
response.writeHead(404, { "content-type": "application/json" });
|
||||
response.end(JSON.stringify({ ok: false, error: "not-found" }));
|
||||
});
|
||||
|
||||
await new Promise((resolve) => {
|
||||
@@ -65,6 +80,36 @@ export function createMockProtectedApiServer({ port = 4319 } = {}) {
|
||||
};
|
||||
}
|
||||
|
||||
function readBearerToken(request, response) {
|
||||
const authHeader = request.headers.authorization ?? "";
|
||||
const isBearer =
|
||||
typeof authHeader === "string" &&
|
||||
authHeader.startsWith("Bearer ") &&
|
||||
authHeader.length > "Bearer ".length;
|
||||
|
||||
if (!isBearer) {
|
||||
response.writeHead(401, { "content-type": "application/json" });
|
||||
response.end(JSON.stringify({ ok: false, error: "unauthorized" }));
|
||||
return null;
|
||||
}
|
||||
|
||||
return authHeader;
|
||||
}
|
||||
|
||||
async function readJsonBody(request) {
|
||||
const chunks = [];
|
||||
|
||||
for await (const chunk of request) {
|
||||
chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk));
|
||||
}
|
||||
|
||||
if (chunks.length === 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return JSON.parse(Buffer.concat(chunks).toString("utf8"));
|
||||
}
|
||||
|
||||
if (import.meta.url === `file://${process.argv[1]}`) {
|
||||
const server = createMockProtectedApiServer();
|
||||
await server.start();
|
||||
|
||||
Reference in New Issue
Block a user