---
title: Test add_sub_reverse_proxy
skip_showdoc: true
skip_exec: true
---
```python
from fastcaddy.core import *
from fastcore.test import *
```

<!-- WARNING: THIS FILE WAS AUTOGENERATED! DO NOT EDIT! -->

```python
reset()
```

```python
setup_caddy(local=True)
```

At this point our Caddy config is:

```python
print(gcfg())
```

We can confirm that by going to [http://localhost:2019/config/](http://localhost:2019/config/)

## Setup: add wildcard route \*.something.example.com.

This is needed in order to add subroutes to it.

```python
add_wildcard_route('something.example.com')
```

Now our config should include the wildcard route:

```python
print(gcfg())
```

At this point there are no subroutes associated with that `*.something.example.com` wildcard. When that is matched, the handlers list is empty until...

## Add subroute 1: foo.something.example.com

```python
add_sub_reverse_proxy('something.example.com', 'foo', 5001)
```

```python
print(gcfg('/apps/http/servers/srv0/routes/0/handle'))
```

Now we can see the `handle` config with the list of sub-routes and the sub-route handler.

```python
print(gcfg('/apps/http/servers/srv0/routes/0/handle/0/routes'))
```

We can see that:

1. A route with id `foo.something.example.com` was created.

```python
test_eq(gcfg('/apps/http/servers/srv0/routes/0/handle/0/routes/0/@id'), 'foo.something.example.com')
```

2. It matches requests for `foo.something.example.com`

```python
test_eq(gcfg('apps/http/servers/srv0/routes/0/handle/0/routes/0/match/0/host/0'), 'foo.something.example.com')
```

3. When a request for `foo.something.example.com` is matched, its handler is a reverse proxy to `localhost:5001`.

```python
test_eq(gcfg('apps/http/servers/srv0/routes/0/handle/0/routes/0/handle/1/upstreams/0/dial'), 'localhost:5001')
```

## Add subroute 2: bar.something.example.com

```python
add_sub_reverse_proxy('something.example.com', 'bar', 5002)
```

```python
print(gcfg('/apps/http/servers/srv0/routes/0/handle'))
```

Now we see a single sub-route handler containing two routes.

```python
print(gcfg('/apps/http/servers/srv0/routes/0/handle/0/routes'))
```

Here we see that:

1. The `foo.something.example.com` subroute is still present.

```python
test_eq(gcfg('/apps/http/servers/srv0/routes/0/handle/0/handler'), 'subroute')
```

2. A route with id `bar.something.example.com` was created.

```python
test_eq(gcfg('/apps/http/servers/srv0/routes/0/handle/0/routes/1/@id'), 'bar.something.example.com')
```

3. It matches requests for `bar.something.example.com`

```python
test_eq(gcfg('/apps/http/servers/srv0/routes/0/handle/0/routes/1/match/0/host/0'),'bar.something.example.com')
```

4. When a request for `bar.something.example.com` is matched, its handler is a reverse proxy to `localhost:5002`.

```python
test_eq(gcfg('/apps/http/servers/srv0/routes/0/handle/0/routes/1/handle/1/upstreams/0/dial'), 'localhost:5002')
```

## Add multi-port subroute

```python
add_sub_reverse_proxy('something.example.com', 'multiport', [5003, 5004])
```

```python
print(gcfg('/apps/http/servers/srv0/routes/0/handle/0/routes/2/handle/1/upstreams'))
```

```python
test_eq(gcfg('/apps/http/servers/srv0/routes/0/handle/0/routes/2/handle/1/upstreams'), [{'dial': 'localhost:5003'}, {'dial': 'localhost:5004'}])
```
