# Network

> See and change what a web app's pages fetch, from the browser: requests that fail, answer with an error or a file, or take their time, a slow connection, a recording of the pages' traffic to answer from, and the messages of their websockets. It builds on the `web-core` pack.

Source: https://axx.nimbusxr.us/references/packs/web-network/

See and change what a web app's pages fetch, from the browser: requests that fail, answer with an error or a file, or take their time, a slow connection, a recording of the pages' traffic to answer from, and the messages of their websockets. It builds on the `web-core` pack.

A request is named by its address, a path below the web app's `url` (`/api/prices`) or a whole URL (`https://maps.example.com/tiles/**`): `*` stands for any part of a path, `**` for any parts; the query does not count unless the address has one. What a step sets holds for every web app of the scenario, from the pages they open next: set it before the page opens.

The pages' own requests only: for the services your app calls from its server, mock them instead (the `mock` pack). `record: true` in `axx.yaml` records what each web app's pages fetch, for a scenario to answer from later.

Add it to a project with `axx pack add web-network`; it builds on [`web-core`](https://axx.nimbusxr.us/references/packs/web-core/), which comes with it.

## Settings

In `axx.yaml`, under `packs.web-network`:

| Setting | Takes | Values | Default |
|---|---|---|---|
| `record` | Record what each scenario's pages fetch, a HAR file for each web app in .axx/web/recordings, for scenarios to answer from. | `true`, `false` | `false` |

## Steps

### `web-network.fail`

```gherkin
Given the page's requests to {string} fail
```

Make the pages' requests to an address fail, as when a service is down or out of reach: a path below the web app's url, or a whole URL, with `*` and `**`.

| Parameter | Takes | For example |
|---|---|---|
| `{string}` | text in double or single quotes, which the step leaves out | `"Get a quote"`, `'Express'` |

**Example:**

```gherkin
Given the page's requests to "https://maps.example.com/**" fail
```

_Since 0.1.1._

### `web-network.status`

```gherkin
Given the page's requests to {string} answer with status {int}
```

Answer the pages' requests to an address with a status and nothing else, like 503.

| Parameter | Takes | For example |
|---|---|---|
| `{string}` | text in double or single quotes, which the step leaves out | `"Get a quote"`, `'Express'` |
| `{int}` | a whole number | `200`, `3` |

**Example:**

```gherkin
Given the page's requests to "/api/prices" answer with status 503
```

_Since 0.1.1._

### `web-network.file`

```gherkin
Given the page's requests to {string} answer with the {filepath} file
```

Answer the pages' requests to an address with a file of the project, with the type of its extension.

| Parameter | Takes | For example |
|---|---|---|
| `{string}` | text in double or single quotes, which the step leaves out | `"Get a quote"`, `'Express'` |
| `{filepath}` | a file of the project: a path relative to the feature file's directory, the `resources` directories or axx.yaml's directory (looked for in that order), or an absolute path, quoted when it has a space | `seeds/parcels.yaml`, `kafka/scan-delivered.json` |

**Example:**

```gherkin
Given the page's requests to "/api/prices" answer with the network/prices-berlin.json file
```

_Since 0.1.1._

### `web-network.slow`

```gherkin
Given the page's requests to {string} take {duration}
```

Make the pages' requests to an address take that long before they go out, as a busy service does.

| Parameter | Takes | For example |
|---|---|---|
| `{string}` | text in double or single quotes, which the step leaves out | `"Get a quote"`, `'Express'` |
| `{duration}` | a duration in seconds (`s`) or minutes (`m`) | `5s`, `2m` |

**Example:**

```gherkin
Given the page's requests to "/api/prices" take 3s
```

_Since 0.1.1._

### `web-network.connection.slow`

```gherkin
Given the browser's connection is slow
```

Make the browser's connection slow, as a phone's in a bad spot: 400 ms of latency and 500 kbit/s each way. Chromium, Chrome and Edge only.

**Example:**

```gherkin
Given the browser's connection is slow
```

_Since 0.1.1._

### `web-network.recording`

```gherkin
Given the page's requests are answered from the {filepath} recording
```

Answer the pages' requests from a recording of what they fetched, a HAR file (`record: true` makes them): the requests it has, it answers; the others go out, and so do websockets.

| Parameter | Takes | For example |
|---|---|---|
| `{filepath}` | a file of the project: a path relative to the feature file's directory, the `resources` directories or axx.yaml's directory (looked for in that order), or an absolute path, quoted when it has a space | `seeds/parcels.yaml`, `kafka/scan-delivered.json` |

**Example:**

```gherkin
Given the page's requests are answered from the network/quote.har recording
```

_Since 0.1.1._

### `web-network.ws.sent`

```gherkin
Then [[within {duration} ]]the page sent a websocket message containing {string}
```

Check that a page of the scenario sent a websocket message with that text in it. The check waits: 10 seconds, or `within {duration}`.

| Parameter | Takes | For example |
|---|---|---|
| `{duration}` | a duration in seconds (`s`) or minutes (`m`) | `5s`, `2m` |
| `{string}` | text in double or single quotes, which the step leaves out | `"Get a quote"`, `'Express'` |

**Variants**, the parts in `[[...]]` said or left out:

- `the page sent a websocket message containing {string}`
- `within {duration} the page sent a websocket message containing {string}`

**Example:**

```gherkin
Then the page sent a websocket message containing "subscribe:PX-4101"
```

_Since 0.1.1._

### `web-network.ws.received`

```gherkin
Then [[within {duration} ]]the page received a websocket message containing {string}
```

Check that a page of the scenario received a websocket message with that text in it. The check waits: 10 seconds, or `within {duration}`.

| Parameter | Takes | For example |
|---|---|---|
| `{duration}` | a duration in seconds (`s`) or minutes (`m`) | `5s`, `2m` |
| `{string}` | text in double or single quotes, which the step leaves out | `"Get a quote"`, `'Express'` |

**Variants**, the parts in `[[...]]` said or left out:

- `the page received a websocket message containing {string}`
- `within {duration} the page received a websocket message containing {string}`

**Example:**

```gherkin
Then the page received a websocket message containing "IN_TRANSIT"
```

_Since 0.1.1._
