diff --git a/src/index.spec.ts b/src/index.spec.ts new file mode 100644 index 0000000..e952519 --- /dev/null +++ b/src/index.spec.ts @@ -0,0 +1,46 @@ +import request from 'supertest'; +import express, { Express, Request, Response } from 'express'; +import httpProxy from 'express-http-proxy'; +import cors from 'cors'; +import { getHost, getUrl } from './utils'; +import dotenv from 'dotenv'; + +dotenv.config(); + +// Mock das funções utilitárias +jest.mock('./utils', () => ({ + getHost: jest.fn().mockReturnValue('http://example.com'), + getUrl: jest.fn().mockReturnValue('/mocked-path'), +})); + +// Configuração do servidor para testes +const createServer = (): Express => { + const app: Express = express(); + app.use(cors()); + + app.get('/', (req: Request, res: Response) => { + res.send('Express + TypeScript Server'); + }); + + app.use('/*', httpProxy(jest.requireMock('./utils').getHost, { + proxyReqPathResolver(req) { + return jest.requireMock('./utils').getUrl(req); + } + })); + + return app; +}; + +describe('Express Server', () => { + let app: Express; + + beforeEach(() => { + app = createServer(); + }); + + it('should return 200 OK for GET /', async () => { + const response = await request(app).get('/'); + expect(response.status).toBe(200); + expect(response.text).toBe('Express + TypeScript Server'); + }); +}); diff --git a/src/utils.spec.ts b/src/utils.spec.ts index c9daebe..3652a7e 100644 --- a/src/utils.spec.ts +++ b/src/utils.spec.ts @@ -11,6 +11,11 @@ const videoMockedRequest = { url: '/api' } as Request +const adminMockedRequest = { + baseUrl: '/adminservice', + url: '/api' +} as Request + const noSuportedMockedRequest = { baseUrl: '/noService', url: '/?all=true' @@ -26,6 +31,12 @@ describe('Should test get url complement', () => { const url = getHost(videoMockedRequest) expect(url).toEqual('http://localhost:8002') }) + + it('Should return admin api url', () => { + const url = getHost(adminMockedRequest) + expect(url).toEqual('http://localhost:8080') + }) + it('Should return null api url', () => { const url = getHost(noSuportedMockedRequest) expect(url).toEqual('')