Skip to content

Commit

Permalink
Merge pull request #374 from pactumjs/feat/support-configure-follow-r…
Browse files Browse the repository at this point in the history
…edirectcount

Allow configuring follow redirects count
  • Loading branch information
leelaprasadv authored Aug 18, 2024
2 parents a83e34e + e7cb046 commit 7a692c0
Show file tree
Hide file tree
Showing 9 changed files with 224 additions and 24 deletions.
63 changes: 48 additions & 15 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
Expand Up @@ -62,7 +62,7 @@
"openapi-fuzzer-core": "^1.0.6",
"pactum-matchers": "^1.1.7",
"parse-graphql": "^1.0.0",
"phin": "^3.7.0",
"phin": "^3.7.1",
"polka": "^0.5.2"
},
"devDependencies": {
Expand Down
5 changes: 4 additions & 1 deletion src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ const config = {
baseUrl: process.env.PACTUM_REQUEST_BASE_URL || '',
timeout: process.env.PACTUM_REQUEST_TIMEOUT ? parseInt(process.env.PACTUM_REQUEST_TIMEOUT) : 3000,
headers: {},
followRedirects: false,
followRedirects: {
enabled: false,
count: 20
},
retry: {
count: 1,
delay: 1000
Expand Down
3 changes: 2 additions & 1 deletion src/exports/request.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ export function setBaseUrl(url: string): void;
/**
* sets default value for follow redirects
*/
export function setFollowRedirects(follow: boolean): void;
export function setDefaultFollowRedirects(follow: boolean): void;
export function setDefaultFollowRedirects(follow: number): void;

/**
* removes all or selective default headers
Expand Down
8 changes: 7 additions & 1 deletion src/exports/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,13 @@ const request = {
},

setDefaultFollowRedirects(follow) {
config.request.followRedirects = follow;
if (typeof follow !== 'number' && typeof follow !== 'boolean') {
throw new PactumRequestError(`Invalid follow redirect option - ${follow}, allowed boolean/number`);
}
if (typeof follow == 'number' && follow >=0 ) {
config.request.followRedirects.count = follow;
}
config.request.followRedirects.enabled = follow;
},

removeDefaultHeaders(key) {
Expand Down
4 changes: 2 additions & 2 deletions src/helpers/requestProcessor.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,8 @@ function setMultiPartFormData(request) {
}

function setFollowRedirects(request) {
if (config.request.followRedirects && typeof request.followRedirects === 'undefined') {
request.followRedirects = config.request.followRedirects;
if (config.request.followRedirects.enabled && typeof request.followRedirects === 'undefined') {
request.followRedirects = config.request.followRedirects.count;
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/models/Spec.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ declare class Spec {
* @see https://pactumjs.github.io/api/requests/withFollowRedirects.html
*/
withFollowRedirects(follow: boolean): Spec;

withFollowRedirects(follow: number): Spec;
/**
* enables compression
* @see https://pactumjs.github.io/api/requests/withCompression.html
Expand Down
9 changes: 8 additions & 1 deletion src/models/Spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -321,8 +321,15 @@ class Spec {
}

withFollowRedirects(follow) {
if (typeof follow !== 'number' && typeof follow !== 'boolean') {
throw new PactumRequestError('Follow redirects should be number or boolean');
}
if (typeof follow == 'number' && follow >=0 ) {
this._request.followRedirects = follow;
return this;
}
this._request.followRedirects = follow;
return this;
return this;
}

withCompression() {
Expand Down
152 changes: 151 additions & 1 deletion test/component/interactions.mock.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,7 @@ describe('Interactions - Not Strict - Wait', () => {

describe('Interactions - Not Strict - Follow Redirects', () => {

it('with Follow Redirects', async () => {
it('with Follow Redirects - boolean config', async () => {
await pactum.spec()
.useInteraction({
strict: false,
Expand Down Expand Up @@ -528,6 +528,75 @@ describe('Interactions - Not Strict - Follow Redirects', () => {
.expectStatus(200);
});

it('with Follow Redirects - count config', async () => {
await pactum.spec()
.useInteraction({
strict: false,
request: {
method: 'GET',
path: '/mock/redirect'
},
response: {
status: 301,
headers: {
'location': 'http://localhost:9393/mock/intermediate/1'
}
}
})
.useInteraction({
strict: false,
request: {
method: 'GET',
path: '/mock/intermediate/1'
},
response: {
status: 301,
headers: {
'location': 'http://localhost:9393/mock/intermediate/2'
}
}
})
.useInteraction({
strict: false,
request: {
method: 'GET',
path: '/mock/intermediate/2'
},
response: {
status: 301,
headers: {
'location': 'http://localhost:9393/mock/intermediate/3'
}
}
})
.useInteraction({
strict: false,
request: {
method: 'GET',
path: '/mock/intermediate/3'
},
response: {
status: 301,
headers: {
'location': 'http://localhost:9393/mock/actual'
}
}
})
.useInteraction({
strict: false,
request: {
method: 'GET',
path: '/mock/actual'
},
response: {
status: 200
}
})
.get('http://localhost:9393/mock/redirect')
.withFollowRedirects(4)
.expectStatus(200);
});

it('without Follow Redirects', async () => {
await pactum.spec()
.useInteraction({
Expand Down Expand Up @@ -597,6 +666,36 @@ describe('Interactions - Not Strict - Follow Redirects', () => {
.expectStatus(200);
});

it('with default Follow Redirects - count config', async () => {
pactum.request.setDefaultFollowRedirects(1);
await pactum.spec()
.useInteraction({
strict: false,
request: {
method: 'GET',
path: '/mock/redirect'
},
response: {
status: 301,
headers: {
'location': 'http://localhost:9393/mock/actual'
}
}
})
.useInteraction({
strict: false,
request: {
method: 'GET',
path: '/mock/actual'
},
response: {
status: 200
}
})
.get('http://localhost:9393/mock/redirect')
.expectStatus(200);
});

it('with Follow Redirects as false & default as true', async () => {
pactum.request.setDefaultFollowRedirects(true);
await pactum.spec()
Expand All @@ -618,6 +717,57 @@ describe('Interactions - Not Strict - Follow Redirects', () => {
.expectStatus(301);
});

it('with Follow Redirects as 0 & default as 1', async () => {
pactum.request.setDefaultFollowRedirects(1);
await pactum.spec()
.useInteraction({
strict: false,
request: {
method: 'GET',
path: '/mock/redirect'
},
response: {
status: 301,
headers: {
'location': 'http://localhost:9393/mock/actual'
}
}
})
.get('http://localhost:9393/mock/redirect')
.withFollowRedirects(0)
.expectStatus(301);
});

it('with Follow Redirects as 1 & default as 0', async () => {
pactum.request.setDefaultFollowRedirects(0);
await pactum.spec()
.useInteraction({
strict: false,
request: {
method: 'GET',
path: '/mock/redirect'
},
response: {
status: 301,
headers: {
'location': 'http://localhost:9393/mock/actual'
}
}
})
.useInteraction({
strict: false,
request: {
method: 'GET',
path: '/mock/actual'
},
response: {
status: 200
}
})
.get('http://localhost:9393/mock/redirect')
.withFollowRedirects(1)
.expectStatus(200);
});

afterEach(() => {
pactum.request.setDefaultFollowRedirects(false);
Expand Down

0 comments on commit 7a692c0

Please sign in to comment.