parse repeated parameters

This commit is contained in:
Michael C 2021-06-21 00:33:48 -04:00
parent 1770608525
commit dd74dd3b1b
No known key found for this signature in database
GPG key ID: FFB04FB3B878B7B4
4 changed files with 56 additions and 5 deletions

View file

@ -29,10 +29,12 @@ async function getSegmentsByUUID(UUIDs: SegmentUUID[]): Promise<DBSegment[]> {
async function handleGetSegmentInfo(req: Request, res: Response) { async function handleGetSegmentInfo(req: Request, res: Response) {
// If using params instead of JSON, only one UUID can be pulled // If using params instead of JSON, only one UUID can be pulled
let UUIDs = req.query.UUIDs let UUIDs = req.query.UUIDs
? JSON.parse(req.query.UUIDs as string) ? JSON.parse(req.query.UUIDs as string)
: req.query.UUID : req.query.UUID
? [req.query.UUID] ? Array.isArray(req.query.UUID)
: null; ? req.query.UUID
: [req.query.UUID]
: null;
// deduplicate with set // deduplicate with set
UUIDs = [ ...new Set(UUIDs)]; UUIDs = [ ...new Set(UUIDs)];
// if more than 10 entries, slice // if more than 10 entries, slice

View file

@ -268,7 +268,9 @@ async function handleGetSegments(req: Request, res: Response): Promise<Segment[]
const categories = req.query.categories const categories = req.query.categories
? JSON.parse(req.query.categories as string) ? JSON.parse(req.query.categories as string)
: req.query.category : req.query.category
? [req.query.category] ? Array.isArray(req.query.category)
? req.query.category
: [req.query.category]
: ['sponsor']; : ['sponsor'];
if (!Array.isArray(categories)) { if (!Array.isArray(categories)) {
res.status(400).send("Categories parameter does not match format requirements."); res.status(400).send("Categories parameter does not match format requirements.");

View file

@ -309,4 +309,22 @@ describe('getSegmentInfo', () => {
}) })
.catch(err => ("couldn't call endpoint")); .catch(err => ("couldn't call endpoint"));
}); });
it('Should be able to retreive multiple segments with multiple parameters', (done: Done) => {
fetch(getbaseURL() + `/api/segmentInfo?UUID=${upvotedID}&UUID=${downvotedID}`)
.then(async res => {
if (res.status !== 200) done("Status code was: " + res.status);
else {
const data = await res.json();
if (data.length === 2 &&
(data[0].videoID === "upvoted" && data[0].votes === 2) &&
(data[1].videoID === "downvoted" && data[1].votes === -2)) {
done();
} else {
done("Received incorrect body: " + (await res.text()));
}
}
})
.catch(err => "Couldn't call endpoint");
});
}); });

View file

@ -264,4 +264,33 @@ describe('getSkipSegments', () => {
.catch(err => ("Couldn't call endpoint")); .catch(err => ("Couldn't call endpoint"));
}); });
it('Should be able to get multiple categories with repeating parameters', (done: Done) => {
fetch(getbaseURL() + "/api/skipSegments?videoID=testtesttest&category=sponsor&category=intro")
.then(async res => {
if (res.status !== 200) done("Status code was: " + res.status);
else {
const body = await res.text();
const data = JSON.parse(body);
if (data.length === 2) {
let success = true;
for (const segment of data) {
if ((segment.segment[0] !== 20 || segment.segment[1] !== 33
|| segment.category !== "intro" || segment.UUID !== "1-uuid-2") &&
(segment.segment[0] !== 1 || segment.segment[1] !== 11
|| segment.category !== "sponsor" || segment.UUID !== "1-uuid-0")) {
success = false;
break;
}
}
if (success) done();
else done("Received incorrect body: " + body);
} else {
done("Received incorrect body: " + body);
}
}
})
.catch(err => ("Couldn't call endpoint"));
});
}); });