diff --git a/backend/mainServer/src/host/host.controller.spec.ts b/backend/mainServer/src/host/host.controller.spec.ts index 153e0d47..09824c99 100644 --- a/backend/mainServer/src/host/host.controller.spec.ts +++ b/backend/mainServer/src/host/host.controller.spec.ts @@ -31,7 +31,7 @@ describe('HostController', () => { const req = { headers: { - url: 'http://example.com', + host: 'http://example.com', 'content-type': 'application/json', }, body: { @@ -49,4 +49,50 @@ describe('HostController', () => { expect(res.status).toHaveBeenCalledWith(HttpStatus.OK); expect(res.json).toHaveBeenCalledWith({ 'host-data': mockHostData }); }); + + it('should return 400 for invalid request with missing uuid', async () => { + jest.spyOn(service, 'generateStreamKey').mockResolvedValue(undefined); + + const req = { + headers: { + host: 'http://example.com', + 'content-type': 'application/json', + }, + body: { + // uuid가 없는 비정상적인 요청 + }, + } as any; + + const res = { + status: jest.fn().mockReturnThis(), + json: jest.fn(), + } as any; + + await controller.generateStreamKey(req, res); + + expect(res.status).toHaveBeenCalledWith(HttpStatus.BAD_REQUEST); + }); + + it('should return 400 for invalid request with incorrect content-type', async () => { + jest.spyOn(service, 'generateStreamKey').mockResolvedValue(undefined); + + const req = { + headers: { + host: 'http://example.com', + 'content-type': 'text/plain', + }, + body: { + uuid: 'test-uuid', + }, + } as any; + + const res = { + status: jest.fn().mockReturnThis(), + json: jest.fn(), + } as any; + + await controller.generateStreamKey(req, res); + + expect(res.status).toHaveBeenCalledWith(HttpStatus.BAD_REQUEST); + }); }); diff --git a/backend/mainServer/src/host/host.controller.ts b/backend/mainServer/src/host/host.controller.ts index e513d0ae..5f2ba681 100644 --- a/backend/mainServer/src/host/host.controller.ts +++ b/backend/mainServer/src/host/host.controller.ts @@ -14,17 +14,26 @@ export class HostController { const contentType = req.headers['content-type']; const { uuid } = req.body; - if (!host || !contentType || contentType !== 'application/json' || !uuid) { + if (!host || !contentType || !uuid) { throw new HttpException('Bad Request', HttpStatus.BAD_REQUEST); } + if (contentType !== 'application/json') { + throw new HttpException("Content-Type must be application/json", HttpStatus.BAD_REQUEST) + } const hostData = await this.hostService.generateStreamKey(uuid); res.status(HttpStatus.OK).json({ 'host-data': hostData }); } catch (error) { - res.status(HttpStatus.SERVICE_UNAVAILABLE).json({ - statusCode: 503, - message: 'Server logic error', - }); + if (error.status === 400) { + res.status(HttpStatus.BAD_REQUEST).json({ + error: error.response + }) + } + else { + res.status(HttpStatus.INTERNAL_SERVER_ERROR).json({ + error: 'Server logic error', + }); + } } } } \ No newline at end of file