|
| 1 | +// host.controller.spec.ts |
| 2 | +import { Test, TestingModule } from '@nestjs/testing'; |
| 3 | +import { HostController } from './host.controller'; |
| 4 | +import { HostService } from './host.service'; |
| 5 | +import { HttpStatus } from '@nestjs/common'; |
| 6 | +import {describe, expect, jest} from '@jest/globals'; |
| 7 | +import {Request, Response} from 'express'; |
| 8 | + |
| 9 | +describe('HostController', () => { |
| 10 | + let controller: HostController; |
| 11 | + let service: HostService; |
| 12 | + |
| 13 | + beforeEach(async () => { |
| 14 | + const module: TestingModule = await Test.createTestingModule({ |
| 15 | + controllers: [HostController], |
| 16 | + providers: [HostService], |
| 17 | + }).compile(); |
| 18 | + |
| 19 | + controller = module.get<HostController>(HostController); |
| 20 | + service = module.get<HostService>(HostService); |
| 21 | + }); |
| 22 | + |
| 23 | + it('should be defined', () => { |
| 24 | + expect(controller).toBeDefined(); |
| 25 | + }); |
| 26 | + |
| 27 | + it('should return 200 with host data for valid request', async () => { |
| 28 | + const mockUuid = 'test-uuid'; |
| 29 | + const mockHostData = `session-info-for-${mockUuid}`; |
| 30 | + |
| 31 | + jest.spyOn(service, 'generateStreamKey').mockResolvedValue(mockHostData); |
| 32 | + |
| 33 | + const req = { |
| 34 | + headers: { |
| 35 | + host: 'http://example.com', |
| 36 | + 'content-type': 'application/json', |
| 37 | + }, |
| 38 | + body: { |
| 39 | + uuid: mockUuid, |
| 40 | + }, |
| 41 | + } as unknown as Request; |
| 42 | + |
| 43 | + const res = { |
| 44 | + status: jest.fn().mockReturnThis(), |
| 45 | + json: jest.fn(), |
| 46 | + } as unknown as Response; |
| 47 | + |
| 48 | + await controller.generateStreamKey(req, res); |
| 49 | + |
| 50 | + expect(res.status).toHaveBeenCalledWith(HttpStatus.OK); |
| 51 | + expect(res.json).toHaveBeenCalledWith({ 'host-data': mockHostData }); |
| 52 | + }); |
| 53 | + |
| 54 | + it('should return 400 for invalid request with missing uuid', async () => { |
| 55 | + jest.spyOn(service, 'generateStreamKey').mockResolvedValue(undefined); |
| 56 | + |
| 57 | + const req = { |
| 58 | + headers: { |
| 59 | + host: 'http://example.com', |
| 60 | + 'content-type': 'application/json', |
| 61 | + }, |
| 62 | + body: { |
| 63 | + // uuid가 없는 비정상적인 요청 |
| 64 | + }, |
| 65 | + } as unknown as Request; |
| 66 | + |
| 67 | + const res = { |
| 68 | + status: jest.fn().mockReturnThis(), |
| 69 | + json: jest.fn(), |
| 70 | + } as unknown as Response; |
| 71 | + |
| 72 | + await controller.generateStreamKey(req, res); |
| 73 | + |
| 74 | + expect(res.status).toHaveBeenCalledWith(HttpStatus.BAD_REQUEST); |
| 75 | + }); |
| 76 | + |
| 77 | + it('should return 400 for invalid request with incorrect content-type', async () => { |
| 78 | + jest.spyOn(service, 'generateStreamKey').mockResolvedValue(undefined); |
| 79 | + |
| 80 | + const req = { |
| 81 | + headers: { |
| 82 | + host: 'http://example.com', |
| 83 | + 'content-type': 'text/plain', |
| 84 | + }, |
| 85 | + body: { |
| 86 | + uuid: 'test-uuid', |
| 87 | + }, |
| 88 | + } as unknown as Request; |
| 89 | + |
| 90 | + const res = { |
| 91 | + status: jest.fn().mockReturnThis(), |
| 92 | + json: jest.fn(), |
| 93 | + } as unknown as Response; |
| 94 | + |
| 95 | + await controller.generateStreamKey(req, res); |
| 96 | + |
| 97 | + expect(res.status).toHaveBeenCalledWith(HttpStatus.BAD_REQUEST); |
| 98 | + }); |
| 99 | +}); |
0 commit comments