mirror of
https://github.com/ajayyy/SponsorBlockServer.git
synced 2024-11-10 01:02:30 +01:00
Merge pull request #256 from mchangrh/segmentInfo-error
made 400/404 behaviour consistent with API docs
This commit is contained in:
commit
fb68bd46c8
2 changed files with 22 additions and 12 deletions
|
@ -20,9 +20,7 @@ async function getSegmentsByUUID(UUIDs: SegmentUUID[]): Promise<DBSegment[]> {
|
|||
const DBSegments: DBSegment[] = [];
|
||||
for (let UUID of UUIDs) {
|
||||
// if UUID is invalid, skip
|
||||
if (!isValidSegmentUUID(UUID)) {
|
||||
continue;
|
||||
}
|
||||
if (!isValidSegmentUUID(UUID)) continue;
|
||||
DBSegments.push(await getSegmentFromDBByUUID(UUID as SegmentUUID));
|
||||
}
|
||||
return DBSegments;
|
||||
|
@ -44,12 +42,14 @@ async function handleGetSegmentInfo(req: Request, res: Response) {
|
|||
return false;
|
||||
}
|
||||
const DBSegments = await getSegmentsByUUID(UUIDs);
|
||||
if (DBSegments === null || DBSegments === undefined) {
|
||||
// all uuids failed lookup
|
||||
if (!DBSegments?.length) {
|
||||
res.sendStatus(400);
|
||||
return false;
|
||||
}
|
||||
if (DBSegments.length === 0) {
|
||||
res.sendStatus(404);
|
||||
// uuids valid but not found
|
||||
if (DBSegments[0] === null || DBSegments[0] === undefined) {
|
||||
res.sendStatus(400);
|
||||
return false;
|
||||
}
|
||||
return DBSegments;
|
||||
|
|
|
@ -3,6 +3,7 @@ import {db} from '../../src/databases/databases';
|
|||
import {Done, getbaseURL} from '../utils';
|
||||
import {getHash} from '../../src/utils/getHash';
|
||||
|
||||
const ENOENTID = "0000000000000000000000000000000000000000000000000000000000000000"
|
||||
const upvotedID = "a000000000000000000000000000000000000000000000000000000000000000"
|
||||
const downvotedID = "b000000000000000000000000000000000000000000000000000000000000000"
|
||||
const lockedupID = "c000000000000000000000000000000000000000000000000000000000000000"
|
||||
|
@ -214,10 +215,10 @@ describe('getSegmentInfo', () => {
|
|||
.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}"]`)
|
||||
.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
|
||||
})
|
||||
.catch(err => ("couldn't call endpoint"));
|
||||
|
@ -232,19 +233,19 @@ describe('getSegmentInfo', () => {
|
|||
.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")
|
||||
.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
|
||||
})
|
||||
.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"]`)
|
||||
.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
|
||||
})
|
||||
.catch(err => ("couldn't call endpoint"));
|
||||
|
@ -299,4 +300,13 @@ describe('getSegmentInfo', () => {
|
|||
})
|
||||
.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"));
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Reference in a new issue