Skip to content

Commit 5e17ff5

Browse files
authored
Documentation update for follow redirects (#55)
1 parent 936344b commit 5e17ff5

File tree

4 files changed

+170
-7
lines changed

4 files changed

+170
-7
lines changed

docs/.vitepress/config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ const api_sidebar = [
168168
{ text: 'setBaseUrl', link: '/api/settings/setBaseUrl', },
169169
{ text: 'setDefaultHeaders', link: '/api/settings/setDefaultHeaders', },
170170
{ text: 'setDefaultTimeout', link: '/api/settings/setDefaultTimeout', },
171+
{ text: 'setDefaultFollowRedirects', link: '/api/settings/setDefaultFollowRedirects', },
171172
{ text: 'setLogLevel', link: '/api/settings/setLogLevel', },
172173
{ text: 'setLogger', link: '/api/settings/setLogger', },
173174
{ text: 'setJsonLikeAdapter', link: '/api/settings/setJsonLikeAdapter', },

docs/api/requests/withCore.md

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ tags:
99

1010
To further customize the request, pactum allows us directly set the [core options](https://nodejs.org/api/http.html#httprequesturl-options-callback) of the request.
1111

12+
::: warning WARNING
13+
If `withCore` is used at the end in request chaining, all [http core options](https://nodejs.org/api/http.html#httprequesturl-options-callback) provided in `withCore` will take precedence and they will override any previously values.
14+
:::
15+
1216
## Syntax
1317

1418
```js
@@ -63,6 +67,50 @@ await spec()
6367
.expectStatus(200);
6468
```
6569

66-
::: warning WARNING
67-
If `withCore` is used at the end in request chaining, all [http core options](https://nodejs.org/api/http.html#httprequesturl-options-callback) provided in `withCore` will take precedence and they will override any previously values.
68-
:::
70+
### Https Agent with SSL certificates
71+
72+
```js
73+
// If you have the cert/key pair
74+
const { spec } = require('pactum');
75+
const https = require('https');
76+
const fs = require('fs');
77+
78+
const key = fs.readFileSync("server.key")
79+
const cert = fs.readFileSync("server.crt")
80+
81+
const agent = new https.Agent({
82+
cert: cert,
83+
key: key,
84+
});
85+
86+
await spec()
87+
.get('<https url>')
88+
.withCore({agent: agent })
89+
.expectStatus(200)
90+
```
91+
92+
### Https Agent with self signed / private SSL certificates
93+
94+
```js
95+
const { spec } = require('pactum');
96+
const https = require('https');
97+
const fs = require('fs');
98+
99+
// If you have the cert/key pair
100+
const key = fs.readFileSync("server.key")
101+
const cert = fs.readFileSync("server.crt")
102+
103+
const agent = new https.Agent({
104+
cert: cert, // Optional - add if cert available
105+
key: key, // Optional - add if key is available
106+
rejectUnauthorized: false // Ignore certificate errors
107+
});
108+
109+
await spec()
110+
.get('<https url>')
111+
.withCore({agent: agent })
112+
.expectStatus(200)
113+
```
114+
115+
116+

docs/api/requests/withFollowRedirects.md

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,36 @@ tags:
88

99
Follows redirection.
1010

11+
::: tip Tip
12+
Default value for follow redirects is false / disabled
13+
:::
14+
1115
## Syntax
1216

1317
```js
1418
withFollowRedirects(follow)
1519
```
16-
17-
- `follow` (**boolean**) - follow redirect.
20+
follow redirects option
21+
- `follow` (**boolean**) - follow redirect toggle, to enable follow redirects and use default follow redirect count of 20.
22+
- `follow` (**number**) - follow redirect, count of redirects. Allowed values are >=0.
1823

1924
## Usage
2025

2126
#### ✅ Correct Usage
2227

2328
```js
29+
//
2430
await spec()
2531
.get('/api/old/location')
26-
.withFollowRedirects()
32+
.withFollowRedirects(true)
33+
.expectStatus(200);
34+
```
35+
36+
```js
37+
//
38+
await spec()
39+
.get('/api/old/location')
40+
.withFollowRedirects(5)
2741
.expectStatus(200);
2842
```
2943

@@ -32,11 +46,31 @@ await spec()
3246
#### General Redirection
3347

3448
```js
49+
// toggle follow redirects with default follow-redirect count
3550
const { spec } = require('pactum');
3651

3752
await spec()
3853
.get('https://httpbin.org/redirect-to')
3954
.withQueryParams('url', 'https://httpbin.org/status/200')
4055
.withFollowRedirects(true)
4156
.expectStatus(200);
42-
```
57+
```
58+
59+
```js
60+
// toggle follow redirects with custom follow-redirect count
61+
const { spec } = require('pactum');
62+
63+
await spec()
64+
.get('https://httpbin.org/redirect-to')
65+
.withQueryParams('url', 'https://httpbin.org/status/200')
66+
.withFollowRedirects(2)
67+
.expectStatus(200);
68+
```
69+
70+
::: tip Tip
71+
Follow redirects count should be greater than or equal to number of redirects on the server side for the request.
72+
:::
73+
74+
## See Also
75+
76+
- [setDefaultFollowRedirects](/api/settings/setDefaultFollowRedirects)
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
---
2+
tags:
3+
- redirects
4+
- follow redirects
5+
---
6+
7+
# setDefaultFollowRedirects
8+
9+
set default Follow Redirects for all the requests.
10+
11+
::: tip Tip
12+
Default value for follow redirects is false / disabled
13+
:::
14+
15+
## Syntax
16+
17+
```js
18+
setDefaultFollowRedirects(follow)
19+
```
20+
21+
## Usage
22+
23+
### ✅ Correct Usage
24+
25+
```js
26+
// Boolean parameter - defaults to count 20
27+
request.setDefaultFollowRedirects(true)
28+
```
29+
30+
```js
31+
// Absolute follow redirect count
32+
request.setDefaultFollowRedirects(5)
33+
```
34+
35+
## Arguments
36+
37+
#### > follow (boolean)
38+
39+
Toggle follow redirects
40+
41+
#### > follow (number)
42+
43+
Toggle follow redirects and set the absolute redirect count.
44+
45+
46+
47+
## Examples
48+
49+
### Normal
50+
51+
```js
52+
const { spec, request } = require('pactum');
53+
54+
request.setDefaultFollowRedirects(true);
55+
56+
await spec()
57+
.get('https://httpbin.org/redirect-to')
58+
.withQueryParams('url', 'https://httpbin.org/status/200')
59+
.expectStatus(200);
60+
```
61+
62+
```js
63+
const { spec, request } = require('pactum');
64+
65+
request.setDefaultFollowRedirects(5);
66+
67+
await spec()
68+
.get('https://httpbin.org/redirect-to')
69+
.withQueryParams('url', 'https://httpbin.org/status/200')
70+
.expectStatus(200);
71+
```
72+
73+
::: tip Tip
74+
Follow redirects count should be greater than or equal to number of redirects on the server side for the request.
75+
:::
76+
77+
78+
## See Also
79+
80+
- [withFollowRedirects](/api/requests/withFollowRedirects)

0 commit comments

Comments
 (0)