diff --git a/API-Project/src/controllers/steps.controller.ts b/API-Project/src/controllers/steps.controller.ts index 347896a..ce777ef 100644 --- a/API-Project/src/controllers/steps.controller.ts +++ b/API-Project/src/controllers/steps.controller.ts @@ -8,16 +8,17 @@ const steps_gw = new StepsGateway() StepsController.get('/:id', async (req, res) => { - const id = String(req.params.id); + const id = Number(req.params.id); if (!Number.isInteger(id)) { - throw new Exceptions.BadRequestException('id invalid !'); + res.status(400).send('invalid parameter or no parameter') + return } try { const steps = await steps_gw.getForRecipes(Number(id)) - if (steps == null) { + if (steps.length == 0) { res.status(404).send('not found') } else { diff --git a/API-Project/src/server.ts b/API-Project/src/server.ts index acbe17e..c5febc4 100644 --- a/API-Project/src/server.ts +++ b/API-Project/src/server.ts @@ -28,7 +28,6 @@ export const startServer = (port_to_use: number) => { return app.listen(port_to_use, () => console.log(`App listening on PORT ${port}`)); }; -// Exécutez le serveur uniquement si le module est le point d'entrée principal if (require.main === module) { startServer(port); } diff --git a/API-Project/src/types/recipes.ts b/API-Project/src/types/recipes.ts index 1661444..74c807e 100644 --- a/API-Project/src/types/recipes.ts +++ b/API-Project/src/types/recipes.ts @@ -25,12 +25,4 @@ export class Recipe implements IRecipe { this.ingredients = ingredients this.steps = steps } - - addStep(newStep: string) { - this.steps.push(newStep) - } - - addIngredient(newIngredient: IIngredient) { - this.ingredients.push(newIngredient) - } } \ No newline at end of file diff --git a/API-Project/tests/steps.spec.ts b/API-Project/tests/steps.spec.ts new file mode 100644 index 0000000..64f1193 --- /dev/null +++ b/API-Project/tests/steps.spec.ts @@ -0,0 +1,33 @@ +import request from 'supertest'; +import app, { startServer } from '../src/server'; +import { Server, IncomingMessage, ServerResponse } from 'http'; + + +describe('GET /steps', () => { + let server: Server; + const port = 3003 + + beforeAll(() => { + server = startServer(port); + }); + + afterAll((done) => { + server.close(done); + }); + + // /steps/:id + it('should return a 200 status code', async () => { + const response = await request(app).get('/steps/4444'); + expect(response.status).toBe(200); + }); + + it('should return a 400 status code', async () => { + const response = await request(app).get('/steps/hey'); + expect(response.status).toBe(400); + }); + + it('should return a 404 status code', async () => { + const response = await request(app).get('/steps/1'); + expect(response.status).toBe(404); + }); +}); \ No newline at end of file