|
| 1 | +# HTTP2 Local server |
| 2 | + |
| 3 | +Local server based on [python-hyper/h2](https://github.com/python-hyper/h2). |
| 4 | + |
| 5 | +## How to run the server |
| 6 | + |
| 7 | +Python 3.5+ required. |
| 8 | + |
| 9 | +* Install hyper h2 python module. `python3 -m pip install h2` |
| 10 | + |
| 11 | +### TLS server |
| 12 | + |
| 13 | +* The code is based the [example](https://github.com/python-hyper/h2/blob/master/examples/asyncio/asyncio-server.py) from hyper h2 server. |
| 14 | +* Have the cert/key ready. The script now using `../resources/unittests.crt`, you can either just run the script within this directory, which will find the certificates and key from the related path, or you can use your own and change the code coordinately. |
| 15 | +* Run python. `python3 ./server.py`. |
| 16 | + |
| 17 | +#### Endpoint |
| 18 | + |
| 19 | +##### `/echo` - Echo endpoint (default) |
| 20 | + |
| 21 | +Echoes back request headers and body as JSON. |
| 22 | + |
| 23 | +```bash |
| 24 | +curl -k -v -H "foo:bar" https://localhost:3443/echo |
| 25 | +``` |
| 26 | +#### Special headers |
| 27 | + |
| 28 | +##### `/echo` with `x-repeat-data` header - Download test |
| 29 | + |
| 30 | +Sends repeated test pattern of specified size (in bytes). |
| 31 | + |
| 32 | +```bash |
| 33 | +# Download 1MB of repeated data |
| 34 | +curl -k -v -H "x-repeat-data: 1000000" https://localhost:3443/echo |
| 35 | +``` |
| 36 | + |
| 37 | +##### `/echo` with `x-repeat-data` + `x-slow-response` headers - Slow connection test |
| 38 | + |
| 39 | +Sends repeated data throttled to ~900 bytes/sec (for timeout testing). |
| 40 | + |
| 41 | +```bash |
| 42 | +# Download 5MB slowly at default speed (900 bytes/sec) |
| 43 | +curl -k -v -H "x-repeat-data: 5000000" -H "x-slow-response: true" https://localhost:3443/echo |
| 44 | +``` |
| 45 | + |
| 46 | +##### `/echo` with custom throughput - Custom speed test |
| 47 | + |
| 48 | +Override default throughput with `x-throughput-bps` header. |
| 49 | + |
| 50 | +```bash |
| 51 | +# Download 5MB at 500 bytes/sec |
| 52 | +curl -k -v -H "x-repeat-data: 5000000" -H "x-slow-response: true" -H "x-throughput-bps: 500" https://localhost:3443/echo |
| 53 | +``` |
| 54 | + |
| 55 | +##### `/echo` with `x-upload-test` header - Upload test |
| 56 | + |
| 57 | +Returns the byte count of the uploaded body without echoing the body content. |
| 58 | + |
| 59 | +```bash |
| 60 | +# Upload data and get byte count |
| 61 | +curl -k -v -X PUT -H "x-upload-test: true" -d "test data" https://localhost:3443/echo |
| 62 | +``` |
| 63 | + |
| 64 | +##### `/echo` with `x-expect-status` header - Custom status code |
| 65 | + |
| 66 | +Returns the specified HTTP status code. |
| 67 | + |
| 68 | +```bash |
| 69 | +# Get a 500 status code |
| 70 | +curl -k -v -H "x-expect-status: 500" https://localhost:3443/echo |
| 71 | +``` |
| 72 | + |
| 73 | +##### Any other path |
| 74 | + |
| 75 | +Returns 404 Not Found. |
| 76 | + |
| 77 | +### Non-TLS server |
| 78 | + |
| 79 | +- The code is based the non-tls [example](http://python-hyper.org/projects/h2/en/stable/basic-usage.html) from hyper h2 server. |
| 80 | +- Run python. `python3 ./non_tls_server.py`. |
| 81 | +- To test the server runs correctly, you can do `curl -v --http2-prior-knowledge http://localhost:3280` and check the result. |
| 82 | + |
| 83 | +# HTTP1.1 Local server |
| 84 | + |
| 85 | +## Requirements |
| 86 | + |
| 87 | +Install the required Python dependencies: |
| 88 | + |
| 89 | +```bash |
| 90 | +pip install trio h11 |
| 91 | +``` |
| 92 | + |
| 93 | +Or using pip3: |
| 94 | + |
| 95 | +```bash |
| 96 | +pip3 install trio h11 |
| 97 | +``` |
| 98 | + |
| 99 | +## Running the Server |
| 100 | + |
| 101 | +### Basic Usage (HTTP + HTTPS) |
| 102 | + |
| 103 | +Run both HTTP (port 80) and HTTPS (port 443) servers: |
| 104 | + |
| 105 | +```bash |
| 106 | +sudo python3 mock_server.py |
| 107 | +``` |
| 108 | + |
| 109 | +Note: `sudo` is required for ports 80 and 443 on most systems. |
| 110 | + |
| 111 | +### Test Mode (Custom Port) |
| 112 | + |
| 113 | +Run on a custom port without sudo: |
| 114 | + |
| 115 | +```bash |
| 116 | +TEST_PORT=8080 python3 mock_server.py |
| 117 | +``` |
| 118 | + |
| 119 | +**Important**: Since this uses a self-signed certificate, clients must disable peer verification. |
| 120 | + |
| 121 | +## Endpoints |
| 122 | + |
| 123 | +- **Any path**: Echoes request body as JSON |
| 124 | +- **/response-headers?HeaderName=value**: Adds custom headers to the response based on query parameters |
| 125 | + |
| 126 | +## Stopping the Server |
| 127 | + |
| 128 | +Press `Ctrl+C` to gracefully shut down the server. |
0 commit comments