Skip to content

Commit

Permalink
Documentation update for follow redirects (#55)
Browse files Browse the repository at this point in the history
  • Loading branch information
leelaprasadv authored Oct 26, 2024
1 parent 936344b commit 5e17ff5
Show file tree
Hide file tree
Showing 4 changed files with 170 additions and 7 deletions.
1 change: 1 addition & 0 deletions docs/.vitepress/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ const api_sidebar = [
{ text: 'setBaseUrl', link: '/api/settings/setBaseUrl', },
{ text: 'setDefaultHeaders', link: '/api/settings/setDefaultHeaders', },
{ text: 'setDefaultTimeout', link: '/api/settings/setDefaultTimeout', },
{ text: 'setDefaultFollowRedirects', link: '/api/settings/setDefaultFollowRedirects', },
{ text: 'setLogLevel', link: '/api/settings/setLogLevel', },
{ text: 'setLogger', link: '/api/settings/setLogger', },
{ text: 'setJsonLikeAdapter', link: '/api/settings/setJsonLikeAdapter', },
Expand Down
54 changes: 51 additions & 3 deletions docs/api/requests/withCore.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ tags:

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.

::: warning WARNING
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.
:::

## Syntax

```js
Expand Down Expand Up @@ -63,6 +67,50 @@ await spec()
.expectStatus(200);
```

::: warning WARNING
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.
:::
### Https Agent with SSL certificates

```js
// If you have the cert/key pair
const { spec } = require('pactum');
const https = require('https');
const fs = require('fs');

const key = fs.readFileSync("server.key")
const cert = fs.readFileSync("server.crt")

const agent = new https.Agent({
cert: cert,
key: key,
});

await spec()
.get('<https url>')
.withCore({agent: agent })
.expectStatus(200)
```

### Https Agent with self signed / private SSL certificates

```js
const { spec } = require('pactum');
const https = require('https');
const fs = require('fs');

// If you have the cert/key pair
const key = fs.readFileSync("server.key")
const cert = fs.readFileSync("server.crt")

const agent = new https.Agent({
cert: cert, // Optional - add if cert available
key: key, // Optional - add if key is available
rejectUnauthorized: false // Ignore certificate errors
});

await spec()
.get('<https url>')
.withCore({agent: agent })
.expectStatus(200)
```



42 changes: 38 additions & 4 deletions docs/api/requests/withFollowRedirects.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,36 @@ tags:

Follows redirection.

::: tip Tip
Default value for follow redirects is false / disabled
:::

## Syntax

```js
withFollowRedirects(follow)
```

- `follow` (**boolean**) - follow redirect.
follow redirects option
- `follow` (**boolean**) - follow redirect toggle, to enable follow redirects and use default follow redirect count of 20.
- `follow` (**number**) - follow redirect, count of redirects. Allowed values are >=0.

## Usage

#### ✅ Correct Usage

```js
//
await spec()
.get('/api/old/location')
.withFollowRedirects()
.withFollowRedirects(true)
.expectStatus(200);
```

```js
//
await spec()
.get('/api/old/location')
.withFollowRedirects(5)
.expectStatus(200);
```

Expand All @@ -32,11 +46,31 @@ await spec()
#### General Redirection

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

await spec()
.get('https://httpbin.org/redirect-to')
.withQueryParams('url', 'https://httpbin.org/status/200')
.withFollowRedirects(true)
.expectStatus(200);
```
```

```js
// toggle follow redirects with custom follow-redirect count
const { spec } = require('pactum');

await spec()
.get('https://httpbin.org/redirect-to')
.withQueryParams('url', 'https://httpbin.org/status/200')
.withFollowRedirects(2)
.expectStatus(200);
```

::: tip Tip
Follow redirects count should be greater than or equal to number of redirects on the server side for the request.
:::

## See Also

- [setDefaultFollowRedirects](/api/settings/setDefaultFollowRedirects)
80 changes: 80 additions & 0 deletions docs/api/settings/setDefaultFollowRedirects.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
---
tags:
- redirects
- follow redirects
---

# setDefaultFollowRedirects

set default Follow Redirects for all the requests.

::: tip Tip
Default value for follow redirects is false / disabled
:::

## Syntax

```js
setDefaultFollowRedirects(follow)
```

## Usage

### ✅ Correct Usage

```js
// Boolean parameter - defaults to count 20
request.setDefaultFollowRedirects(true)
```

```js
// Absolute follow redirect count
request.setDefaultFollowRedirects(5)
```

## Arguments

#### > follow (boolean)

Toggle follow redirects

#### > follow (number)

Toggle follow redirects and set the absolute redirect count.



## Examples

### Normal

```js
const { spec, request } = require('pactum');

request.setDefaultFollowRedirects(true);

await spec()
.get('https://httpbin.org/redirect-to')
.withQueryParams('url', 'https://httpbin.org/status/200')
.expectStatus(200);
```

```js
const { spec, request } = require('pactum');

request.setDefaultFollowRedirects(5);

await spec()
.get('https://httpbin.org/redirect-to')
.withQueryParams('url', 'https://httpbin.org/status/200')
.expectStatus(200);
```

::: tip Tip
Follow redirects count should be greater than or equal to number of redirects on the server side for the request.
:::


## See Also

- [withFollowRedirects](/api/requests/withFollowRedirects)

0 comments on commit 5e17ff5

Please sign in to comment.