|
| 1 | +package fake_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/go-resty/resty/v2" |
| 5 | + "github.com/smartcontractkit/chainlink-testing-framework/framework/components/fake" |
| 6 | + "github.com/stretchr/testify/require" |
| 7 | + "net/http" |
| 8 | + "testing" |
| 9 | +) |
| 10 | + |
| 11 | +func TestRecord(t *testing.T) { |
| 12 | + cfg := &fake.Input{ |
| 13 | + Port: 9111, |
| 14 | + } |
| 15 | + out, err := fake.NewFakeDataProvider(cfg) |
| 16 | + require.NoError(t, err) |
| 17 | + r := resty.New().SetBaseURL(out.BaseURLHost) |
| 18 | + |
| 19 | + t.Run("can mock a response with JSON", func(t *testing.T) { |
| 20 | + apiPath := "/fake/api/1" |
| 21 | + err = fake.JSON(apiPath, map[string]any{ |
| 22 | + "status": "ok", |
| 23 | + }, 200) |
| 24 | + require.NoError(t, err) |
| 25 | + var respBody struct { |
| 26 | + Status string `json:"status"` |
| 27 | + } |
| 28 | + resp, err := r.R().SetResult(&respBody).Get(apiPath) |
| 29 | + require.NoError(t, err) |
| 30 | + require.Equal(t, 200, resp.StatusCode()) |
| 31 | + require.Equal(t, "ok", respBody.Status) |
| 32 | + }) |
| 33 | + |
| 34 | + t.Run("can record request/response and access it", func(t *testing.T) { |
| 35 | + apiPath := "/fake/api/2" |
| 36 | + err = fake.JSON(apiPath, map[string]any{ |
| 37 | + "status": "ok", |
| 38 | + }, 200) |
| 39 | + require.NoError(t, err) |
| 40 | + reqBody := struct { |
| 41 | + SomeData string `json:"some_data"` |
| 42 | + }{ |
| 43 | + SomeData: "some_data", |
| 44 | + } |
| 45 | + var respBody struct { |
| 46 | + Status string `json:"status"` |
| 47 | + } |
| 48 | + _, err := r.R().SetBody(reqBody).SetResult(&respBody).Post(apiPath) |
| 49 | + require.NoError(t, err) |
| 50 | + |
| 51 | + // get request and response |
| 52 | + recordedData, err := fake.R.Get(apiPath) |
| 53 | + require.Equal(t, &fake.Record{ |
| 54 | + Method: "POST", |
| 55 | + Path: apiPath, |
| 56 | + Headers: http.Header{ |
| 57 | + "Accept-Encoding": []string{"gzip"}, |
| 58 | + "Content-Type": []string{"application/json"}, |
| 59 | + "Content-Length": []string{"25"}, |
| 60 | + "User-Agent": []string{"go-resty/2.15.3 (https://github.com/go-resty/resty)"}, |
| 61 | + }, |
| 62 | + ReqBody: `{"some_data":"some_data"}`, |
| 63 | + ResBody: `{"status":"ok"}`, |
| 64 | + Status: 200, |
| 65 | + }, recordedData) |
| 66 | + }) |
| 67 | +} |
0 commit comments