From f9b963a7ab63abb5d47b45e4f50291da4f0c33e3 Mon Sep 17 00:00:00 2001 From: victorleaoo Date: Sat, 27 Jul 2024 14:36:15 -0300 Subject: [PATCH 1/3] adicao de testes da index --- src/index.spec.ts | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 src/index.spec.ts 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'); + }); +}); From 5783a301b201b0c45c0e0c04fe63d3917bd1cf60 Mon Sep 17 00:00:00 2001 From: victorleaoo Date: Sat, 27 Jul 2024 14:36:37 -0300 Subject: [PATCH 2/3] adicao de testes da index --- .github/workflows/blank.yml | 36 ------------------------------------ 1 file changed, 36 deletions(-) delete mode 100644 .github/workflows/blank.yml diff --git a/.github/workflows/blank.yml b/.github/workflows/blank.yml deleted file mode 100644 index 01502b1..0000000 --- a/.github/workflows/blank.yml +++ /dev/null @@ -1,36 +0,0 @@ -# This is a basic workflow to help you get started with Actions - -name: CI - -# Controls when the workflow will run -on: - # Triggers the workflow on push or pull request events but only for the "main" branch - push: - branches: [ "main" ] - pull_request: - branches: [ "main" ] - - # Allows you to run this workflow manually from the Actions tab - workflow_dispatch: - -# A workflow run is made up of one or more jobs that can run sequentially or in parallel -jobs: - # This workflow contains a single job called "build" - build: - # The type of runner that the job will run on - runs-on: ubuntu-latest - - # Steps represent a sequence of tasks that will be executed as part of the job - steps: - # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it - - uses: actions/checkout@v4 - - # Runs a single command using the runners shell - - name: Run a one-line script - run: echo Hello, world! - - # Runs a set of commands using the runners shell - - name: Run a multi-line script - run: | - echo Add other actions to build, - echo test, and deploy your project. From 2246c6771516013ea36baff04a268adfca862ae7 Mon Sep 17 00:00:00 2001 From: victorleaoo Date: Sat, 27 Jul 2024 14:40:02 -0300 Subject: [PATCH 3/3] correcao testes com adicao da url de admin --- src/utils.spec.ts | 11 +++++++++++ src/utils.ts | 4 ++-- 2 files changed, 13 insertions(+), 2 deletions(-) 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('') diff --git a/src/utils.ts b/src/utils.ts index 26e9a15..610d7ee 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -3,8 +3,8 @@ import dotenv from 'dotenv'; dotenv.config(); -const USER_API_URL = process.env["USER_API_URL"] || 'http://localhost:8000'; -const VIDEO_API_URL = process.env["VIDEO_API_URL"] || 'http://localhost:8001'; +const USER_API_URL = process.env["USER_API_URL"] || 'http://localhost:8001'; +const VIDEO_API_URL = process.env["VIDEO_API_URL"] || 'http://localhost:8002'; const ADMIN_API_URL = process.env["ADMIN_API_URL"] || 'http://localhost:8080'; const getUrl = (req: Request) => {