From 8535fc0f51229d3c129e8ed237abbee0804ce3d7 Mon Sep 17 00:00:00 2001 From: Leandro Costa Date: Fri, 27 Jan 2023 08:37:22 -0300 Subject: [PATCH] Implementando request tests --- __tests__/lhisp-oauth-client.test.ts | 144 +++++++++++++++++++++++++-- src/lhisp-oauth-client.t.ts | 2 +- 2 files changed, 135 insertions(+), 11 deletions(-) diff --git a/__tests__/lhisp-oauth-client.test.ts b/__tests__/lhisp-oauth-client.test.ts index 54f5e66..c9b95db 100644 --- a/__tests__/lhisp-oauth-client.test.ts +++ b/__tests__/lhisp-oauth-client.test.ts @@ -1,6 +1,6 @@ import axios from 'axios'; import LhispOauthClient from "../src/lhisp-oauth-client"; -import { LhispOauthClientConstructorParams } from '../src/lhisp-oauth-client.t'; +import { ContentType, LhispOauthClientConstructorParams } from '../src/lhisp-oauth-client.t'; // Mock jest and set the type jest.mock('axios'); @@ -13,6 +13,7 @@ const clientSecret = "testClientSecret"; const baseClientParams = { apiUrl, authUrl, clientId, clientSecret }; const basicAuth = `Basic ${Buffer.from(`${clientId}:${clientSecret}`).toString('base64')}`; const contentTypeApplicationJson = "application/json"; +const contentTypeApplicationXFormUrlEncoded = "application/x-www-form-urlencoded"; const defaultGrantValue='client_credentials'; const defaultGrantType=`{"grant_type":"${defaultGrantValue}"}`; @@ -23,15 +24,7 @@ describe("Get Access Token", ()=>{ mockedAxios.request.mockResolvedValueOnce({ data: mockedAccessToken }); await accessTokenValidator(cli); - expect(mockedAxios.request).toBeCalledWith(expect.objectContaining({ - url: authUrl, - method: "POST", - headers: { - Authorization: basicAuth, - 'Content-Type': contentTypeApplicationJson, - }, - data: defaultGrantType, - })); + validateDefaultGetAccessToken(); }); it("Shoud Get with Custom Auth Header", async ()=>{ @@ -115,6 +108,137 @@ describe("Get Access Token", ()=>{ }); }); +describe("Request", ()=>{ + it("Get without Params", async ()=>{ + const cli = getOauthClient(); + mockedAxios.request.mockReset(); + // Mockando a requizição para obter o token. + mockedAxios.request.mockResolvedValueOnce({ data: mockedAccessToken }); + mockedAxios.request.mockResolvedValueOnce({ data: {"status": "ok"} }); + + const resp = await cli.get({ path: '/my-test-url' }); + validateDefaultGetAccessToken(); + expect(mockedAxios.request).toBeCalledWith(expect.objectContaining({ + url: `${apiUrl}/my-test-url`, + method: "GET", + headers: { + Authorization: `Bearer SomeAccessToken`, + 'Content-Type': contentTypeApplicationJson, + }, + data: undefined + })); + expect(resp).toStrictEqual({"status": "ok"}); + }); + + it("Get with Params", async ()=>{ + const cli = getOauthClient(); + mockedAxios.request.mockReset(); + // Mockando a requizição para obter o token. + mockedAxios.request.mockResolvedValueOnce({ data: mockedAccessToken }); + mockedAxios.request.mockResolvedValueOnce({ data: {"status": "ok"} }); + + const resp = await cli.get({ path: '/my-test-url', params: { id: 1 } }); + validateDefaultGetAccessToken(); + expect(mockedAxios.request).toBeCalledWith(expect.objectContaining({ + url: `${apiUrl}/my-test-url`, + method: "GET", + headers: { + Authorization: `Bearer SomeAccessToken`, + 'Content-Type': contentTypeApplicationJson, + }, + params: { id: 1 }, + data: undefined + })); + expect(resp).toStrictEqual({"status": "ok"}); + }); + + it("Post", async ()=>{ + const cli = getOauthClient(); + mockedAxios.request.mockReset(); + // Mockando a requizição para obter o token. + mockedAxios.request.mockResolvedValueOnce({ data: mockedAccessToken }); + mockedAxios.request.mockResolvedValueOnce({ data: {"status": "ok"} }); + + const resp = await cli.post({ path: '/my-test-url-post', data: { id: 1, user: 'test' } }); + validateDefaultGetAccessToken(); + expect(mockedAxios.request).toBeCalledWith(expect.objectContaining({ + url: `${apiUrl}/my-test-url-post`, + method: "POST", + headers: { + Authorization: `Bearer SomeAccessToken`, + 'Content-Type': contentTypeApplicationJson, + }, + data: { id: 1, user: 'test' } + })); + expect(resp).toStrictEqual({"status": "ok"}); + }); + + it("Post with different contentType", async ()=>{ + const cli = getOauthClient(); + mockedAxios.request.mockReset(); + // Mockando a requizição para obter o token. + mockedAxios.request.mockResolvedValueOnce({ data: mockedAccessToken }); + mockedAxios.request.mockResolvedValueOnce({ data: {"status": "ok"} }); + + const resp = await cli.post({ + path: '/my-test-url-post', + data: { id: 1, user: 'test' }, + contentType: ContentType.APPLICATION_X_WWW_FORM_URLENCODED + }); + validateDefaultGetAccessToken(); + expect(mockedAxios.request).toBeCalledWith(expect.objectContaining({ + url: `${apiUrl}/my-test-url-post`, + method: "POST", + headers: { + Authorization: `Bearer SomeAccessToken`, + 'Content-Type': contentTypeApplicationXFormUrlEncoded, + }, + data: { id: 1, user: 'test' } + })); + expect(resp).toStrictEqual({"status": "ok"}); + }); + + it("Post with Different Token Header Name", async ()=>{ + const cli = getOauthClient({ + ...baseClientParams, + tokenHeaderName: "x-token", + }); + mockedAxios.request.mockReset(); + // Mockando a requizição para obter o token. + mockedAxios.request.mockResolvedValueOnce({ data: mockedAccessToken }); + mockedAxios.request.mockResolvedValueOnce({ data: {"status": "ok"} }); + + const resp = await cli.post({ + path: '/my-test-url-post', + data: { id: 1, user: 'test' }, + contentType: ContentType.APPLICATION_X_WWW_FORM_URLENCODED + }); + validateDefaultGetAccessToken(); + expect(mockedAxios.request).toBeCalledWith(expect.objectContaining({ + url: `${apiUrl}/my-test-url-post`, + method: "POST", + headers: { + 'x-token': `Bearer SomeAccessToken`, + 'Content-Type': contentTypeApplicationXFormUrlEncoded, + }, + data: { id: 1, user: 'test' } + })); + expect(resp).toStrictEqual({"status": "ok"}); + }); +}); + +function validateDefaultGetAccessToken(){ + expect(mockedAxios.request).toBeCalledWith(expect.objectContaining({ + url: authUrl, + method: "POST", + headers: { + Authorization: basicAuth, + 'Content-Type': contentTypeApplicationJson, + }, + data: defaultGrantType, + })); +} + async function accessTokenValidator(cli: LhispOauthClient){ const now = Date.now(); const accessToken = await cli.getAccessToken(); diff --git a/src/lhisp-oauth-client.t.ts b/src/lhisp-oauth-client.t.ts index 1cb67b9..460f6a1 100644 --- a/src/lhisp-oauth-client.t.ts +++ b/src/lhisp-oauth-client.t.ts @@ -23,7 +23,7 @@ export interface LhispOauthClientConstructorParams { export interface ExecutarRequestParams extends AxiosRequestConfig { path: string; - contentType: ContentType, + contentType?: ContentType, } export interface AccessToken {