Skip to content

Commit

Permalink
Fix request interceptor tests (#8)
Browse files Browse the repository at this point in the history
* Fix request interceptor tests

---------

Co-authored-by: Marek Dev <[email protected]>
  • Loading branch information
marekdev-me and Marek Dev authored Jan 10, 2024
1 parent bb3143a commit 3a4623f
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 38 deletions.
10 changes: 5 additions & 5 deletions src/Response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export default class TailorResponse {
/**
* Request configuration
*/
public config: IRequestOptions;
public requestOptions: IRequestOptions;

/**
* Whether returned response was cached or not
Expand All @@ -37,12 +37,12 @@ export default class TailorResponse {
*/
public response: Response | undefined;

constructor(data: any, response: Response | undefined, config: IRequestOptions, isCached: boolean = false) {
constructor(data: any, response: Response | undefined, requestOptions: IRequestOptions, isCached: boolean = false) {
this.data = data;
this.status = response?.status;
this.statusText = response?.statusText;
this.headers = response?.headers;
this.config = config;
this.requestOptions = requestOptions;
this.isCached = isCached;
this.response = response;
}
Expand All @@ -54,7 +54,7 @@ export default class TailorResponse {
status: this.status,
statusText: this.statusText,
headers: this.headers,
config: this.config,
requestOptions: this.requestOptions,
}
}

Expand All @@ -66,7 +66,7 @@ export default class TailorResponse {
* Return response in JSON format
*/
json(): string | undefined {
if (!this.config.json && this.data) {
if (!this.requestOptions.json && this.data) {
return JSON.parse(this.data);
}

Expand Down
68 changes: 35 additions & 33 deletions tests/RequestInterceptorTest.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import TailorFetch, {IRequestOptions, TailorResponse, BaseRequestInterceptor} from "../src";
import {assert, expect} from "chai";

import {assert} from "chai";

const makeRequest = async (url: string, reqOptions: IRequestOptions): Promise<TailorResponse> => {
return await TailorFetch.GET(url, reqOptions);
Expand All @@ -10,68 +9,71 @@ class RequestInterceptor implements BaseRequestInterceptor {
intercept(requestOptions: RequestInit): RequestInit {
const headers = new Headers(requestOptions.headers);

headers.set("just", "set");

headers.set('Content-Type','application/json');
requestOptions.headers = headers;

if (typeof requestOptions.body === "string") {
const bodyData = JSON.parse(requestOptions.body);

bodyData.title = "intercepted name";

requestOptions.body = JSON.stringify(bodyData);
}

return requestOptions;
}
}

describe("request interceptor test", () => {
it("should update request using functional interceptor", async () => {
const url = "http://localhost:3000/headers";
const url = "https://dummyjson.com/products/add";
const requestOptions: IRequestOptions = {
json: true,
body: JSON.stringify({
title: 'example product',
price: 300.00
}),
requestInterceptor: (requestOptions: RequestInit): RequestInit => {

const headers = new Headers(requestOptions.headers);

headers.set(
"just", "set"
);

headers.set('Content-Type','application/json');
requestOptions.headers = headers;

return requestOptions;
}
};
if (typeof requestOptions.body === "string") {
const bodyData = JSON.parse(requestOptions.body);

// Act: Perform the GET request and receive response
const response = await makeRequest(url, requestOptions);
bodyData.title = "intercepted name";

// Assert: Verify the expected outcomes
requestOptions.body = JSON.stringify(bodyData);
}

// Check if the response is successful (status code 2xx).
assert.equal(response.successful(), true, "GET request failed");
return requestOptions;
}
};

// Check if response is an instance of TailorResponse
assert.instanceOf(response, TailorResponse);
const response = await TailorFetch.POST(url, requestOptions);

// console.log(response.data.hasOwnProperty('just'));
assert.equal(response.successful(), true, "Functional interceptor POST failed");

expect(Object.keys(response.data)).to.include.members(['just']);
assert.equal(response.data.title, "intercepted name", "Functional interceptor data change failed");
});

// Class Based
it("should update request using class based interceptor", async () => {
const url = "http://localhost:3000/headers";
const url = "https://dummyjson.com/products/add";
const requestOptions: IRequestOptions = {
json: true,
body: JSON.stringify({
title: 'example product',
price: 300.00
}),
requestInterceptor: new RequestInterceptor
};

// Act: Perform the GET request and receive response
const response = await makeRequest(url, requestOptions);

// Assert: Verify the expected outcomes

// Check if the response is successful (status code 2xx).
assert.equal(response.successful(), true, "GET request failed");
const response = await TailorFetch.POST(url, requestOptions);

// Check if response is an instance of TailorResponse
assert.instanceOf(response, TailorResponse);
assert.equal(response.successful(), true, "Class interceptor POST failed");

expect(Object.keys(response.data)).to.include.members(['just']);
assert.equal(response.data.title, "intercepted name", "Class interceptor data change failed");
});
});

0 comments on commit 3a4623f

Please sign in to comment.