mirror of
https://github.com/ajayyy/SponsorBlockServer.git
synced 2025-12-15 16:07:03 +03:00
Merge pull request #256 from mchangrh/segmentInfo-error
made 400/404 behaviour consistent with API docs
This commit is contained in:
@@ -20,9 +20,7 @@ async function getSegmentsByUUID(UUIDs: SegmentUUID[]): Promise<DBSegment[]> {
|
|||||||
const DBSegments: DBSegment[] = [];
|
const DBSegments: DBSegment[] = [];
|
||||||
for (let UUID of UUIDs) {
|
for (let UUID of UUIDs) {
|
||||||
// if UUID is invalid, skip
|
// if UUID is invalid, skip
|
||||||
if (!isValidSegmentUUID(UUID)) {
|
if (!isValidSegmentUUID(UUID)) continue;
|
||||||
continue;
|
|
||||||
}
|
|
||||||
DBSegments.push(await getSegmentFromDBByUUID(UUID as SegmentUUID));
|
DBSegments.push(await getSegmentFromDBByUUID(UUID as SegmentUUID));
|
||||||
}
|
}
|
||||||
return DBSegments;
|
return DBSegments;
|
||||||
@@ -44,12 +42,14 @@ async function handleGetSegmentInfo(req: Request, res: Response) {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
const DBSegments = await getSegmentsByUUID(UUIDs);
|
const DBSegments = await getSegmentsByUUID(UUIDs);
|
||||||
if (DBSegments === null || DBSegments === undefined) {
|
// all uuids failed lookup
|
||||||
|
if (!DBSegments?.length) {
|
||||||
res.sendStatus(400);
|
res.sendStatus(400);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (DBSegments.length === 0) {
|
// uuids valid but not found
|
||||||
res.sendStatus(404);
|
if (DBSegments[0] === null || DBSegments[0] === undefined) {
|
||||||
|
res.sendStatus(400);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return DBSegments;
|
return DBSegments;
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ import {db} from '../../src/databases/databases';
|
|||||||
import {Done, getbaseURL} from '../utils';
|
import {Done, getbaseURL} from '../utils';
|
||||||
import {getHash} from '../../src/utils/getHash';
|
import {getHash} from '../../src/utils/getHash';
|
||||||
|
|
||||||
|
const ENOENTID = "0000000000000000000000000000000000000000000000000000000000000000"
|
||||||
const upvotedID = "a000000000000000000000000000000000000000000000000000000000000000"
|
const upvotedID = "a000000000000000000000000000000000000000000000000000000000000000"
|
||||||
const downvotedID = "b000000000000000000000000000000000000000000000000000000000000000"
|
const downvotedID = "b000000000000000000000000000000000000000000000000000000000000000"
|
||||||
const lockedupID = "c000000000000000000000000000000000000000000000000000000000000000"
|
const lockedupID = "c000000000000000000000000000000000000000000000000000000000000000"
|
||||||
@@ -214,10 +215,10 @@ describe('getSegmentInfo', () => {
|
|||||||
.catch(err => "Couldn't call endpoint");
|
.catch(err => "Couldn't call endpoint");
|
||||||
});
|
});
|
||||||
|
|
||||||
it('Should return 404 if array passed to UUID', (done: Done) => {
|
it('Should return 400 if array passed to UUID', (done: Done) => {
|
||||||
fetch(getbaseURL() + `/api/segmentInfo?UUID=["${upvotedID}", "${downvotedID}"]`)
|
fetch(getbaseURL() + `/api/segmentInfo?UUID=["${upvotedID}", "${downvotedID}"]`)
|
||||||
.then(res => {
|
.then(res => {
|
||||||
if (res.status !== 404) done("non 404 respone code: " + res.status);
|
if (res.status !== 400) done("non 400 respone code: " + res.status);
|
||||||
else done(); // pass
|
else done(); // pass
|
||||||
})
|
})
|
||||||
.catch(err => ("couldn't call endpoint"));
|
.catch(err => ("couldn't call endpoint"));
|
||||||
@@ -232,19 +233,19 @@ describe('getSegmentInfo', () => {
|
|||||||
.catch(err => ("couldn't call endpoint"));
|
.catch(err => ("couldn't call endpoint"));
|
||||||
});
|
});
|
||||||
|
|
||||||
it('Should return 404 if bad UUID passed', (done: Done) => {
|
it('Should return 400 if bad UUID passed', (done: Done) => {
|
||||||
fetch(getbaseURL() + "/api/segmentInfo?UUID=notarealuuid")
|
fetch(getbaseURL() + "/api/segmentInfo?UUID=notarealuuid")
|
||||||
.then(res => {
|
.then(res => {
|
||||||
if (res.status !== 404) done("non 404 respone code: " + res.status);
|
if (res.status !== 400) done("non 400 respone code: " + res.status);
|
||||||
else done(); // pass
|
else done(); // pass
|
||||||
})
|
})
|
||||||
.catch(err => ("couldn't call endpoint"));
|
.catch(err => ("couldn't call endpoint"));
|
||||||
});
|
});
|
||||||
|
|
||||||
it('Should return 404 if bad UUIDs passed in array', (done: Done) => {
|
it('Should return 400 if bad UUIDs passed in array', (done: Done) => {
|
||||||
fetch(getbaseURL() + `/api/segmentInfo?UUIDs=["notarealuuid", "anotherfakeuuid"]`)
|
fetch(getbaseURL() + `/api/segmentInfo?UUIDs=["notarealuuid", "anotherfakeuuid"]`)
|
||||||
.then(res => {
|
.then(res => {
|
||||||
if (res.status !== 404) done("non 404 respone code: " + res.status);
|
if (res.status !== 400) done("non 400 respone code: " + res.status);
|
||||||
else done(); // pass
|
else done(); // pass
|
||||||
})
|
})
|
||||||
.catch(err => ("couldn't call endpoint"));
|
.catch(err => ("couldn't call endpoint"));
|
||||||
@@ -299,4 +300,13 @@ describe('getSegmentInfo', () => {
|
|||||||
})
|
})
|
||||||
.catch(err => ("couldn't call endpoint"));
|
.catch(err => ("couldn't call endpoint"));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('Should return 400 if UUID not found', (done: Done) => {
|
||||||
|
fetch(getbaseURL() + `/api/segmentInfo?UUID=${ENOENTID}`)
|
||||||
|
.then(res => {
|
||||||
|
if (res.status !== 400) done("non 400 respone code: " + res.status);
|
||||||
|
else done(); // pass
|
||||||
|
})
|
||||||
|
.catch(err => ("couldn't call endpoint"));
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user