Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged removeDefaultHeader and removeDefaultHeaders methods into one #366

Merged
merged 3 commits into from
Jul 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "pactum",
"version": "3.7.0",
"version": "3.7.1",
"description": "REST API Testing Tool for all levels in a Test Pyramid",
"main": "./src/index.js",
"types": "./src/index.d.ts",
Expand Down
9 changes: 2 additions & 7 deletions src/exports/request.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,9 @@ export function setBaseUrl(url: string): void;
export function setFollowRedirects(follow: boolean): void;

/**
* removes default header
* removes all or selective default headers
*/
export function removeDefaultHeader(key: string): void;

/**
* removes all default headers
*/
export function removeDefaultHeaders(): void;
export function removeDefaultHeaders(key?: string): void;

/**
* records data that will be available in reports
Expand Down
15 changes: 6 additions & 9 deletions src/exports/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,12 @@ const request = {
config.request.followRedirects = follow;
},

removeDefaultHeader(key) {
if (!key) {
throw new PactumRequestError(`Invalid header key provided - ${key}`);
}
delete config.request.headers[key];
},

removeDefaultHeaders() {
config.request.headers = {};
removeDefaultHeaders(key) {
if(key) {
delete config.request.headers[key];
} else {
config.request.headers = {};
}
},

setDefaultRecorders(name, path) {
Expand Down
39 changes: 32 additions & 7 deletions test/unit/request.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,8 @@ describe('request', () => {
expect(() => request.setDefaultHeaders()).throws('Invalid header key provided - undefined');
});

it('removeDefaultHeader - undefined', () => {
expect(() => request.removeDefaultHeader()).throws('Invalid header key provided - undefined');
});

it('removeDefaultHeader - which is not present', () => {
request.removeDefaultHeader('present');
it('removeDefaultHeaders - which is not present', () => {
request.removeDefaultHeaders('present');
});

it('setDefaultHeader & setDefaultHeaders & remove', () => {
Expand All @@ -41,7 +37,7 @@ describe('request', () => {
'no': 'space',
'gta': 'v'
});
request.removeDefaultHeader('no');
request.removeDefaultHeaders('no');
expect(config.request.headers).deep.equals({
'hello': 'space',
'gta': 'v'
Expand All @@ -62,6 +58,35 @@ describe('request', () => {
expect(() => request.setDefaultTimeout('100')).throws('Invalid timeout provided - 100');
});

it('setting default headers and removing it', () => {
request.setDefaultHeaders({
'pactumjs': 'userfriendly',
'apitesting': 'pactumjs'
});
expect(config.request.headers).deep.equals({
'pactumjs': 'userfriendly',
'apitesting': 'pactumjs'
});
request.removeDefaultHeaders();
expect(config.request.headers).deep.equals({});
});

it('setting duplicate headers and removing it', () => {
request.setDefaultHeaders({
'pactumjs': 'lightweight',
'test': 'testing',
'test': 'tested'
});
expect(config.request.headers).deep.equals({
'pactumjs': 'lightweight',
'test': 'tested'
});
request.removeDefaultHeaders('test');
expect(config.request.headers).deep.equals({
'pactumjs': 'lightweight'
});
});

afterEach(() => {
config.request.headers = {};
});
Expand Down
Loading