diff --git a/package.json b/package.json index 13b688b1..f5702d8b 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "codex.docs", "license": "Apache-2.0", - "version": "2.3.3", + "version": "2.3.4", "type": "module", "bin": { "codex.docs": "dist/backend/app.js" diff --git a/src/backend/routes/api/links.ts b/src/backend/routes/api/links.ts index caca5ed4..f0c95369 100644 --- a/src/backend/routes/api/links.ts +++ b/src/backend/routes/api/links.ts @@ -21,20 +21,18 @@ router.get('/fetchUrl', async (req: Request, res: Response) => { success: 0, }; - if (!req.query.url) { + if (!req.query.url || typeof req.query.url !== 'string') { res.status(400).json(response); return; } - if (typeof req.query.url !== 'string') { - return; - } - try { const linkData = (await ogs({ url: req.query.url })).result; if (!linkData.success) { + res.status(400).json(response); + return; } diff --git a/src/test/rest/links.ts b/src/test/rest/links.ts new file mode 100644 index 00000000..69afbe7e --- /dev/null +++ b/src/test/rest/links.ts @@ -0,0 +1,26 @@ +import chai from 'chai'; +import chaiHTTP from 'chai-http'; +import express from 'express'; +import links from '../../backend/routes/api/links.js'; + +const {expect} = chai; +const app = express(); + +app.use('/api', links); +chai.use(chaiHTTP); + +describe('Links REST: ', () => { + it('Responds 400 when url is missing', async () => { + const res = await chai.request(app).get('/api/fetchUrl'); + + expect(res).to.have.status(400); + expect(res.body).to.deep.equal({ success: 0 }); + }); + + it('Responds 400 when url is not a single string', async () => { + const res = await chai.request(app).get('/api/fetchUrl?url[]=https://example.com'); + + expect(res).to.have.status(400); + expect(res.body).to.deep.equal({ success: 0 }); + }); +});