diff --git a/src/config/supertest.ts b/src/config/supertest.ts index 320fe94..8fbac82 100644 --- a/src/config/supertest.ts +++ b/src/config/supertest.ts @@ -1,3 +1,3 @@ import supertest from 'supertest'; -export const request = supertest(process.env.BASEURI || "https://reqres.in/api/"); \ No newline at end of file +export const request = supertest(process.env.BASEURI || "https://reqres.in/api"); \ No newline at end of file diff --git a/src/helper/apicall.ts b/src/helper/apicall.ts index 7caa2c9..8bcf796 100644 --- a/src/helper/apicall.ts +++ b/src/helper/apicall.ts @@ -1,14 +1,16 @@ -import { request } from "src/config/supertest"; -import { endpoint } from "src/services/endpoints"; +import { request } from "@Config/supertest"; +import { logResponseToReport } from "@Helper/logger"; +import { GetApiType, PostApiType } from "@Types/http"; import { Response } from "supertest"; -export const postCall = async (service: endpoint, payload: object, headers?: object): Promise => { - if (headers) return request.post(service).set(headers); - return request.post(service).send(payload); +export const httpPostCall = async (post: PostApiType): Promise => { + let response = await request.post(post.service).set(post.headers ? post.headers : {}).send(post.payload); + logResponseToReport(post.context, response); + return response; } -export const getCall = async (service: endpoint, payload?: object, headers?: object): Promise => { - if (payload && headers) return request.get(service).set(headers).send(payload); - else if (payload) return request.get(service).send(payload); - else return request.get(service); +export const httpGetCall = async (get: GetApiType): Promise => { + let response = await request.get(get.service).set(get.headers ? get.headers : {}).send(get.payload ? get.payload : {}); + logResponseToReport(get.context, response); + return response; } \ No newline at end of file diff --git a/src/helper/utils.ts b/src/helper/utils.ts new file mode 100644 index 0000000..ffe5814 --- /dev/null +++ b/src/helper/utils.ts @@ -0,0 +1,3 @@ +import { performance } from 'perf_hooks'; + +export const performanceTime = () => performance.now(); \ No newline at end of file diff --git a/src/services/endpoints.ts b/src/services/endpoints.ts index 4b78318..ec90e38 100644 --- a/src/services/endpoints.ts +++ b/src/services/endpoints.ts @@ -1,3 +1,3 @@ export enum endpoint { - user = "users" + user = "/users" } \ No newline at end of file diff --git a/src/static/Constants.ts b/src/static/Constants.ts new file mode 100644 index 0000000..235d546 --- /dev/null +++ b/src/static/Constants.ts @@ -0,0 +1,5 @@ +export const RESPONSE = { + TIME: 20000, + CODE: 201, + TYPE: "application/json" +} \ No newline at end of file diff --git a/src/test/Users.spec.ts b/src/test/Users.spec.ts index 7cfd126..6a00501 100644 --- a/src/test/Users.spec.ts +++ b/src/test/Users.spec.ts @@ -1,33 +1,35 @@ -import { postCall } from '@Helper/apicall'; -import { logResponseToReport } from '@Helper/logger'; +import { httpPostCall } from '@Helper/apicall'; +import { performanceTime } from '@Helper/utils'; import users from "@Resources/testdata.json"; import userschema from "@Schema/user.json"; import { endpoint } from '@Services/endpoints'; +import { RESPONSE } from '@Static/Constants'; import { UserPayloadType, UserResponseType } from '@Types/user'; -import { expect, use } from 'chai'; -import { performance } from 'perf_hooks'; -use(require('chai-json-schema')); +import ResponseAssert from "@Validation/ResponseAssert"; describe('Test ReqRes APIs', () => { users.forEach((user: UserPayloadType) => { it(`should validate create user ${user.name}`, async function () { - const startTime = performance.now(); - const response = await postCall(endpoint.user, user); + const startTime = performanceTime() + const response = await httpPostCall({ + service: endpoint.user, + payload: user, + context: this + }); - expect(performance.now() - startTime).to.be.lessThan(2000); - expect(response.statusCode).to.equal(201); - expect(response.type).to.equal("application/json"); + const { name, job, id, createdAt }: UserResponseType = response.body; - logResponseToReport(this, response); - const responseBody: UserResponseType = response.body; - expect(responseBody).to.be.jsonSchema(userschema) + new ResponseAssert(response) + .timeIsLessThan(startTime, RESPONSE.TIME) + .statusCodeIs(RESPONSE.CODE) + .typeIs(RESPONSE.TYPE) + .schemaIs(userschema) + .toEqual(name, user.name) + .toEqual(job, user.job) + .isNotEmpty(id) + .isNotEmpty(createdAt); - const { name, job, id, createdAt } = responseBody; - expect(name).equal(user.name); - expect(job).equal(user.job); - expect(id).is.not.empty; - expect(createdAt).is.not.empty; }) }) diff --git a/src/types/http.d.ts b/src/types/http.d.ts new file mode 100644 index 0000000..2838f4e --- /dev/null +++ b/src/types/http.d.ts @@ -0,0 +1,15 @@ +import { endpoint } from "@Services/endpoints"; + +export type PostApiType = { + service: endpoint, + payload: object, + headers?: object, + context: Mocha.Context, +} + +export type GetApiType = { + service: endpoint, + payload?: object, + headers?: object, + context: Mocha.Context +} \ No newline at end of file diff --git a/src/validations/ResponseAssert.ts b/src/validations/ResponseAssert.ts new file mode 100644 index 0000000..76782a4 --- /dev/null +++ b/src/validations/ResponseAssert.ts @@ -0,0 +1,49 @@ +import { performanceTime } from '@Helper/utils'; +import { expect, use } from 'chai'; +import { Response } from 'supertest'; +use(require('chai-json-schema')); + +export default class ResponseAssert { + + response: Response; + + constructor(response: Response) { + this.response = response; + } + + assertThat(response: Response) { + this.response = response; + return this; + } + + timeIsLessThan(start: any, expectedTime: number) { + expect(performanceTime() - start).to.be.lessThan(expectedTime); + return this; + } + + statusCodeIs(code: number) { + expect(this.response.status).to.equal(code); + return this; + } + + typeIs(type: string) { + expect(this.response.type).to.equal(type); + return this; + } + + schemaIs(schema: {}) { + expect(this.response.body).to.be.jsonSchema(schema); + return this; + } + + toEqual(expected: string | number, actual: string | number) { + expect(expected).equal(actual); + return this; + } + + isNotEmpty(value: string | number) { + expect(value).is.not.empty; + return this; + } + +} \ No newline at end of file diff --git a/tsconfig.json b/tsconfig.json index 8fc59b5..b424354 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -4,6 +4,9 @@ "esModuleInterop": true, "resolveJsonModule": true, "paths": { + "@Config/*": [ + "./src/config/*" + ], "@Resources/*": [ "./src/resources/*" ], @@ -18,6 +21,12 @@ ], "@Schema/*": [ "./src/schema/*" + ], + "@Validation/*": [ + "./src/validations/*" + ], + "@Static/*": [ + "./src/static/*" ] } }