mirror of
https://github.com/ajayyy/SponsorBlockServer.git
synced 2025-12-07 12:07:07 +03:00
Merge pull request #209 from MRuy/fix/valid-json-for-categories-param
Fix getSkipSegmentsByHash requires valid json
This commit is contained in:
@@ -10,11 +10,23 @@ export async function getSkipSegmentsByHash(req: Request, res: Response) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const categories: Category[] = req.query.categories
|
let categories: Category[] = [];
|
||||||
? JSON.parse(req.query.categories as string)
|
try {
|
||||||
: req.query.category
|
categories = req.query.categories
|
||||||
? [req.query.category]
|
? JSON.parse(req.query.categories as string)
|
||||||
: ['sponsor'];
|
: req.query.category
|
||||||
|
? [req.query.category]
|
||||||
|
: ["sponsor"];
|
||||||
|
if (!Array.isArray(categories)) {
|
||||||
|
return res.status(400).send("Categories parameter does not match format requirements.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch(error) {
|
||||||
|
return res.status(400).send("Bad parameter: categories (invalid JSON)");
|
||||||
|
}
|
||||||
|
|
||||||
|
// filter out none string elements, only flat array with strings is valid
|
||||||
|
categories = categories.filter((item: any) => typeof item === "string");
|
||||||
|
|
||||||
// Get all video id's that match hash prefix
|
// Get all video id's that match hash prefix
|
||||||
const segments = getSegmentsByHash(req, hashPrefix, categories);
|
const segments = getSegmentsByHash(req, hashPrefix, categories);
|
||||||
|
|||||||
@@ -96,10 +96,10 @@ describe('getSegmentsByHash', () => {
|
|||||||
.catch(err => done("Couldn't call endpoint"));
|
.catch(err => done("Couldn't call endpoint"));
|
||||||
});
|
});
|
||||||
|
|
||||||
it('Should return 500 for bad format categories', (done: Done) => { // should probably be 400
|
it('Should return 400 for bad format categories', (done: Done) => {
|
||||||
fetch(getbaseURL() + '/api/skipSegments/?categories=shilling')
|
fetch(getbaseURL() + '/api/skipSegments/fdaf?categories=shilling')
|
||||||
.then(res => {
|
.then(res => {
|
||||||
if (res.status !== 500) done("expected 500 got " + res.status);
|
if (res.status !== 400) done("expected 400 got " + res.status);
|
||||||
else done(); // pass
|
else done(); // pass
|
||||||
})
|
})
|
||||||
.catch(err => done("Couldn't call endpoint"));
|
.catch(err => done("Couldn't call endpoint"));
|
||||||
|
|||||||
Reference in New Issue
Block a user