|
| 1 | +# Mockserver |
| 2 | + |
| 3 | +A simple, high-performing mockserver that can dynamically build new routes with customized responses. |
| 4 | + |
| 5 | +## Use |
| 6 | + |
| 7 | +Call the `/register` endpoint to define a route. |
| 8 | + |
| 9 | +### Curl |
| 10 | + |
| 11 | +```sh |
| 12 | +curl -X POST http://localhost:8080/register -d '{ |
| 13 | + "method": "GET", |
| 14 | + "path": "/hello", |
| 15 | + "response": "{\"message\": \"Hello, world!\"}", |
| 16 | + "status_code": 200, |
| 17 | + "content_type": "application/json" |
| 18 | +}' -H "Content-Type: application/json" |
| 19 | +``` |
| 20 | + |
| 21 | +### Go and [Resty](https://github.com/go-resty/resty) |
| 22 | + |
| 23 | +```go |
| 24 | +client := resty.New() |
| 25 | + |
| 26 | +route := map[string]interface{}{ |
| 27 | + "method": "GET", |
| 28 | + "path": "/hello", |
| 29 | + "response": "{\"message\":\"Hello, world!\"}", |
| 30 | + "status_code": 200, |
| 31 | + "content_type": "application/json", |
| 32 | +} |
| 33 | + |
| 34 | +resp, _ := client.R(). |
| 35 | + SetHeader("Content-Type", "application/json"). |
| 36 | + SetBody(route). |
| 37 | + Post("http://localhost:8080/register") |
| 38 | +``` |
| 39 | + |
| 40 | +You can now call your endpoint and receive the JSON response back. |
| 41 | + |
| 42 | +```sh |
| 43 | +curl -X GET http://localhost:8080/hello -H "Content-Type: application/json" |
| 44 | +# {"message":"Hello, world!"} |
| 45 | +``` |
| 46 | + |
| 47 | +## Configure |
| 48 | + |
| 49 | +Config is through environment variables. |
| 50 | + |
| 51 | +| **Environment Variable** | **Description** | **Default Value** | |
| 52 | +| ------------------------ | -------------------------------------------------------------- | ----------------- | |
| 53 | +| `LOG_LEVEL` | Controls the logging level (`debug`, `info`, `warn`, `error`). | `debug` | |
| 54 | +| `SAVE_FILE` | Path to the file where routes are saved and loaded. | `save.json` | |
| 55 | + |
| 56 | +## Run |
| 57 | + |
| 58 | +```sh |
| 59 | +go run . |
| 60 | +``` |
| 61 | + |
| 62 | +## Test |
| 63 | + |
| 64 | +```sh |
| 65 | +go test ./... |
| 66 | +``` |
| 67 | + |
| 68 | +## Benchmark |
| 69 | + |
| 70 | +```sh |
| 71 | +LOG_LEVEL=disabled go test -bench=. -benchmem -run=^\$ |
| 72 | +``` |
| 73 | + |
| 74 | +Benchmark run on an Apple M3 Max. |
| 75 | + |
| 76 | +```sh |
| 77 | +goos: darwin |
| 78 | +goarch: arm64 |
| 79 | +pkg: github.com/smartcontractkit/chainlink-testing-framework/mockserver |
| 80 | +BenchmarkRegisterRoute-14 604978 1967 ns/op 6263 B/op 29 allocs/op |
| 81 | +BenchmarkRouteResponse-14 16561670 70.62 ns/op 80 B/op 1 allocs/op |
| 82 | +BenchmarkSaveRoutes-14 1245 956784 ns/op 636042 B/op 2014 allocs/op |
| 83 | +BenchmarkLoadRoutes-14 1020 1185990 ns/op 348919 B/op 9020 allocs/op |
| 84 | +``` |
0 commit comments