Skip to content

Commit

Permalink
Merge pull request #43 from GustavoCesarSantos/develop
Browse files Browse the repository at this point in the history
Atualiza main
  • Loading branch information
GustavoCesarSantos authored Jun 9, 2022
2 parents 1c7adb1 + c69776f commit 90667c7
Show file tree
Hide file tree
Showing 15 changed files with 101 additions and 43 deletions.
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,19 @@

All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.

## [1.15.0](https://github.com/GustavoCesarSantos/encurtador-api/compare/v1.14.0...v1.15.0) (2022-06-09)


### Features

* :sparkles: cria rota de health check ([2b59bb3](https://github.com/GustavoCesarSantos/encurtador-api/commit/2b59bb3c1763be543c00b9f6fd2ff636af0d5f52))
* adiciona as rotas de health check ao router do express ([e808eb6](https://github.com/GustavoCesarSantos/encurtador-api/commit/e808eb6a3dcea4cab4dc675781c21dafb2246ee3))


### Bug Fixes

* :bug: corrige número da porta ([e093432](https://github.com/GustavoCesarSantos/encurtador-api/commit/e093432bcb7e05740391c6d14540e9ccf65fecbe))

## [1.14.0](https://github.com/GustavoCesarSantos/encurtador-api/compare/v1.13.0...v1.14.0) (2022-05-29)


Expand Down
33 changes: 21 additions & 12 deletions __tests__/e2e/accessRootUrl.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,42 @@ app.setupRoutes();

describe('Access root url', () => {
test('Should return status code 404 when schema is not found', async () => {
const { status } = await request(app.getApp()).get('/v1/fail');
const { status } = await request(app.getApp()).get(
'/v1/shortenedUrls/fail',
);
expect(status).toBe(404);
});

test('Should return not found message when code is not found', async () => {
const { text } = await request(app.getApp()).get('/v1/fail');
const { text } = await request(app.getApp()).get(
'/v1/shortenedUrls/fail',
);
const body = JSON.parse(text);
expect(body.error.message).toBe('Not found');
});

test('Should return status code 302', async () => {
test('Should return status code 201', async () => {
const { text } = await request(app.getApp())
.post('/v1/')
.post('/v1/shortenedUrls')
.send({ url: 'success' });
const body = JSON.parse(text);
const code: string = body.url.split('v1/')[1];
const { status } = await request(app.getApp()).get(`/v1/${code}`);
expect(status).toBe(302);
const code: string = body.url.slice(-5);
const { status } = await request(app.getApp()).get(
`/v1/shortenedUrls/${code}`,
);
expect(status).toBe(201);
});

test('Should redirect to root url', async () => {
test('Should return the root url', async () => {
const { text } = await request(app.getApp())
.post('/v1/')
.post('/v1/shortenedUrls')
.send({ url: 'success' });
const body = JSON.parse(text);
const code: string = body.url.split('v1/')[1];
const { redirect } = await request(app.getApp()).get(`/v1/${code}`);
expect(redirect).toBe(true);
const code: string = body.url.slice(-5);
const response = await request(app.getApp()).get(
`/v1/shortenedUrls/${code}`,
);
const body2 = JSON.parse(response.text);
expect(body2.rootUrl).toBe('success');
});
});
12 changes: 8 additions & 4 deletions __tests__/e2e/createShortUrl.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,30 @@ app.setupRoutes();

describe('Create short url', () => {
test('Should return status code 400 when url is not passed in request body', async () => {
const { status } = await request(app.getApp()).post('/v1/').send({});
const { status } = await request(app.getApp())
.post('/v1/shortenedUrls')
.send({});
expect(status).toBe(400);
});

test('Should return error message with missing param when url is not passed in request body', async () => {
const { text } = await request(app.getApp()).post('/v1/').send({});
const { text } = await request(app.getApp())
.post('/v1/shortenedUrls')
.send({});
const body = JSON.parse(text);
expect(body.error.message).toBe('Missing params: Url');
});

test('Should return status code 201 when valid params are passed', async () => {
const { status } = await request(app.getApp())
.post('/v1/')
.post('/v1/shortenedUrls')
.send({ url: 'success' });
expect(status).toBe(201);
});

test('Should return a short url when valid params are passed', async () => {
const { text } = await request(app.getApp())
.post('/v1/')
.post('/v1/shortenedUrls')
.send({ url: 'success' });
const body = JSON.parse(text);
expect(body).toHaveProperty('url');
Expand Down
19 changes: 19 additions & 0 deletions __tests__/e2e/healthCheck.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import request from 'supertest';

import { ExpressApp } from '@infra/http/express';

const app = new ExpressApp();
app.setupRoutes();

describe('Health Check url', () => {
test('Should return status code 200 when server is running', async () => {
const { status } = await request(app.getApp()).get('/v1/health');
expect(status).toBe(200);
});

test('Should return status ok when server is running', async () => {
const { text } = await request(app.getApp()).get('/v1/health');
const response = JSON.parse(text);
expect(response.message).toBe('ok');
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,14 @@ describe('Create short url', () => {
accessRootUrl = makeSut();
});

test('Should return 302 when short url can be redirect', async () => {
test('Should return 201 when foot url is found', async () => {
const response = await accessRootUrl.handle({
params: { code: 'success' },
});
expect(response.status).toBe(302);
expect(response.status).toBe(201);
});

test('Should return root url when this can be redirect', async () => {
test('Should return root url when this is found', async () => {
const response = await accessRootUrl.handle({
params: { code: 'success' },
});
Expand Down
6 changes: 3 additions & 3 deletions __tests__/unit/shortUrl/controllers/accessRootUrl.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,14 +96,14 @@ describe('Access root url', () => {
});
});

test('Should return 302 when root url can be redirect', async () => {
test('Should return 201 when root url is found', async () => {
const response = await accessRootUrl.handle({
params: { code: 'success' },
});
expect(response.status).toBe(302);
expect(response.status).toBe(201);
});

test('Should return root url when this can be redirect', async () => {
test('Should return root url when is found', async () => {
const response = await accessRootUrl.handle({
params: { code: 'success' },
});
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
{
"name": "shortenerurl-api",
"version": "1.14.0",
"version": "1.15.0",
"description": "",
"main": "dist/index.js",
"scripts": {
"test": "jest --no-cache --runInBand",
"test:unit": "jest --no-cache --runInBand --testPathPattern=./__tests__/unit",
"test:integration": "jest --no-cache --runInBand --testPathPattern=./__tests__/integration",
"test:e2e": "jest --no-cache --runInBand --testPathPattern=./__tests__/e2e",
"test:dev": "jest --no-cache --runInBand --watchAll",
"test:cov": "jest --no-cache --runInBand --coverage",
"test:staged": "jest --passWithNoTests --findRelatedTests",
Expand Down
7 changes: 0 additions & 7 deletions src/helpers/httpResponse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,6 @@ export class HttpResponse {
};
}

static redirect(body: object): Response {
return {
status: 302,
body,
};
}

static badRequest(error: Error): Response {
return {
status: 400,
Expand Down
4 changes: 0 additions & 4 deletions src/infra/adapters/expressRouteAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@ export const expressRouteAdapter = (controller: IController) => {
.status(httpResponse.status)
.json({ error: httpResponse.body });
}
if (httpResponse.status === 302) {
const { rootUrl } = httpResponse.body as { rootUrl: string };
return response.redirect(rootUrl);
}
return response.status(httpResponse.status).json(httpResponse.body);
};
};
22 changes: 18 additions & 4 deletions src/infra/http/express.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { Server } from 'node:http';

export class ExpressApp {
private readonly app: Express;
private readonly port: string = process.env.PORT ?? '3000';
private readonly port: string = process.env.PORT ?? '3001';
private readonly logger: Logger<any> = PinoLogger.create().child({
environment: `${process.env.NODE_ENV}`,
});
Expand All @@ -23,17 +23,31 @@ export class ExpressApp {
}

public setCors() {
const allowedOrigins = ['http://localhost:3000'];
const options: cors.CorsOptions = {
origin: allowedOrigins,
allowedHeaders: [
'Origin',
'X-Requested-With',
'Content-Type',
'Accept',
'X-Access-Token',
],
credentials: true,
methods: 'GET,HEAD,OPTIONS,PUT,PATCH,POST,DELETE',
origin: 'http://localhost:3000',
preflightContinue: false,
};

// const allowedOrigins = ['*'];
// const options: cors.CorsOptions = {
// origin: allowedOrigins,
// };
this.app.use(cors(options));
}

public setupRoutes(): void {
const router = Router();
this.app.use('/v1', router);
routes(router);
this.app.use('/v1', router);
}

public listen(): Server {
Expand Down
7 changes: 7 additions & 0 deletions src/infra/routes/healthCheck.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Router } from 'express';

export const healthCheck = (router: Router): void => {
router.get('/health', (request, response) =>
response.status(200).json({ message: 'ok' }),
);
};
2 changes: 2 additions & 0 deletions src/infra/routes/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { Router } from 'express';

import { shortUrl } from './shortUrl';
import { healthCheck } from './healthCheck';

export const routes = (router: Router) => {
shortUrl(router);
healthCheck(router);
};
4 changes: 2 additions & 2 deletions src/infra/routes/shortUrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ const shortUrlControllerFactory = new ShortUrlControllerFactory();

export const shortUrl = (router: Router): void => {
router.get(
'/:code',
'/shortenedUrls/:code',
expressRouteAdapter(shortUrlControllerFactory.makeAccessRootUrl()),
);
router.post(
'/',
'/shortenedUrls',
expressRouteAdapter(shortUrlControllerFactory.makeCreateShortUrl()),
);
};
2 changes: 1 addition & 1 deletion src/modules/shortUrls/controllers/accessRootUrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ export class AccessRootUrl implements IController<Request> {
})}`,
},
});
return HttpResponse.redirect({ rootUrl: shortUrl.getRootUrl() });
return HttpResponse.okWithBody({ rootUrl: shortUrl.getRootUrl() });
} catch (error) {
return HttpResponse.serverError();
}
Expand Down

0 comments on commit 90667c7

Please sign in to comment.