Skip to content

Commit

Permalink
Merge pull request #36 from pactumjs/https-mock-server-support-docs
Browse files Browse the repository at this point in the history
Https mock server docs update
  • Loading branch information
leelaprasadv authored Aug 14, 2023
2 parents d950f8e + a91384b commit de569c9
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/.vitepress/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ const api_sidebar = [
text: '🎭 Mock',
collapsed: false,
items: [
{ text: 'setDefaults', link: '/api/mock/setDefaults', },
{ text: 'start', link: '/api/mock/start', },
{ text: 'stop', link: '/api/mock/stop', },
{ text: 'interaction', link: '/api/mock/interaction', },
Expand Down
64 changes: 64 additions & 0 deletions docs/api/mock/setDefaults.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# setDefaults

Set default configurations for the mock server.

## Syntax

```js
setDefaults(mockOptions)
```

## Usage

### ✅ Correct Usage

```js
// set port
mock.setDefaults({port: 3000});
```

```js
// set port and hostname
mock.setDefaults({port: 3000, host: '127.0.0.1'});
```

```js
// set port, hostname and https options - runs https mock server
await mock.setDefaults({port: 3001, host: '127.0.0.1', httpsOpts: {key: "key-path", cert: "cert-path"}})
```

::: tip NOTE
If the `httpsOpts` are provided, then mock server will run on https else it will run on http (default).
:::



## Arguments

`mockOptions` object

| Property | Description |
| ------------------------ | ------------------------------------------------------------------------------- |
| port | mock server port [`number`] , default: `9393` |
| host | host name (`localhost`, `127.0.0.1` etc) [`string`] default: `0.0.0.0` |
| httsOpts | https key and certs [`object`], pass this parameter to run https mock server |
| httsOpts.key | path to `.key` file |
| httsOpts.cert | path to `.crt` file |

## Examples

### Normal

```js
const pactum = require('pactum');
const mock = pactum.mock;

const mockOpts = {port: 3001, host: '127.0.0.1', httpsOpts: {key: "server.key", cert: "server.crt"}};
await mock.setDefaults(mockOpts)
await mock.start();
```

## See Also

- [Mock server](/guides/mock-server)
- [Mock server start](/api/mock/start)
25 changes: 25 additions & 0 deletions docs/guides/mock-server.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,28 @@ Mock Server allows you to mock any server or service via HTTP or HTTPS, such as

At one end pactum is a REST API testing tool and on the other, it can act as a standalone mock server. It comes in handy while using this library for component & contract testing.


## Default Configuration

Use `mock.setDefaults()` method to set default configuration for the mock server.

Configuring a http mock server!
```js
const { mock } = require('pactum');
// sets port, host
const mockOpts = {port: 3001, host: '127.0.0.1'};
await mock.setDefaults(mockOpts)
```

Configuring a https mock server!
```js
const { mock } = require('pactum');
// sets port, host, httpsOpts
const mockOpts = {port: 3001, host: '127.0.0.1', httpsOpts: {key: "server.key", cert: "server.crt"}};
await mock.setDefaults(mockOpts)
```
`key` & `cert` values are the paths to .key and .crt files.

## Start Server

Use `mock.start()` method to run the server.
Expand All @@ -13,6 +35,9 @@ const { mock } = require('pactum');
// runs mock server on port 3000
await mock.start(3000);
```
::: tip TIP
Use of `mock.setDefaults()` is encouraged for setting port, hostname and/or https options
:::

Check the health of the mock server

Expand Down

0 comments on commit de569c9

Please sign in to comment.