|
| 1 | +package integration |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "io" |
| 7 | + "net/http" |
| 8 | + "net/http/httptest" |
| 9 | + "net/url" |
| 10 | + "strings" |
| 11 | + "testing" |
| 12 | + |
| 13 | + ht "github.com/ogen-go/ogen/http" |
| 14 | + api "github.com/ogen-go/ogen/internal/integration/test_client_options" |
| 15 | + |
| 16 | + "github.com/stretchr/testify/require" |
| 17 | +) |
| 18 | + |
| 19 | +type clientOptionsHandler struct{} |
| 20 | + |
| 21 | +// Foo implements api.Handler. |
| 22 | +func (c *clientOptionsHandler) Foo(ctx context.Context, params api.FooParams) (string, error) { |
| 23 | + return params.Body, nil |
| 24 | +} |
| 25 | + |
| 26 | +var _ api.Handler = (*clientOptionsHandler)(nil) |
| 27 | + |
| 28 | +func TestClientOptions(t *testing.T) { |
| 29 | + h, err := api.NewServer(&clientOptionsHandler{}) |
| 30 | + require.NoError(t, err) |
| 31 | + |
| 32 | + s := httptest.NewServer(h) |
| 33 | + defer s.Close() |
| 34 | + |
| 35 | + t.Run("WithRequestClient", func(t *testing.T) { |
| 36 | + ctx := context.Background() |
| 37 | + |
| 38 | + c, err := api.NewClient(s.URL) |
| 39 | + require.NoError(t, err) |
| 40 | + |
| 41 | + op := api.WithRequestClient(new(testFaultyClient)) |
| 42 | + _, err = c.Foo(ctx, api.FooParams{Body: "test"}, op) |
| 43 | + require.ErrorContains(t, err, `test faulty client`) |
| 44 | + }) |
| 45 | + t.Run("WithServerURL", func(t *testing.T) { |
| 46 | + ctx := context.Background() |
| 47 | + |
| 48 | + c, err := api.NewClient(`http://completly-wrong-url.foo`) |
| 49 | + require.NoError(t, err) |
| 50 | + |
| 51 | + u, err := url.Parse(s.URL) |
| 52 | + require.NoError(t, err) |
| 53 | + |
| 54 | + op := api.WithServerURL(u) |
| 55 | + resp, err := c.Foo(ctx, api.FooParams{Body: "test"}, op) |
| 56 | + require.NoError(t, err) |
| 57 | + require.Equal(t, "test", resp) |
| 58 | + }) |
| 59 | + t.Run("WithEditRequest", func(t *testing.T) { |
| 60 | + ctx := context.Background() |
| 61 | + |
| 62 | + c, err := api.NewClient(s.URL) |
| 63 | + require.NoError(t, err) |
| 64 | + |
| 65 | + op := api.WithEditRequest(func(req *http.Request) error { |
| 66 | + q := req.URL.Query() |
| 67 | + q.Set("body", "request-override") |
| 68 | + req.URL.RawQuery = q.Encode() |
| 69 | + return nil |
| 70 | + }) |
| 71 | + resp, err := c.Foo(ctx, api.FooParams{Body: "test"}, op) |
| 72 | + require.NoError(t, err) |
| 73 | + require.Equal(t, "request-override", resp) |
| 74 | + |
| 75 | + op = api.WithEditRequest(func(*http.Request) error { |
| 76 | + return errors.New("request editor error") |
| 77 | + }) |
| 78 | + _, err = c.Foo(ctx, api.FooParams{Body: "test"}, op) |
| 79 | + require.ErrorContains(t, err, `request editor error`) |
| 80 | + }) |
| 81 | + t.Run("WithEditResponse", func(t *testing.T) { |
| 82 | + ctx := context.Background() |
| 83 | + |
| 84 | + c, err := api.NewClient(s.URL) |
| 85 | + require.NoError(t, err) |
| 86 | + |
| 87 | + op := api.WithEditResponse(func(resp *http.Response) error { |
| 88 | + resp.Body = io.NopCloser(strings.NewReader(`"response-override"`)) |
| 89 | + return nil |
| 90 | + }) |
| 91 | + resp, err := c.Foo(ctx, api.FooParams{Body: "test"}, op) |
| 92 | + require.NoError(t, err) |
| 93 | + require.Equal(t, "response-override", resp) |
| 94 | + |
| 95 | + op = api.WithEditResponse(func(*http.Response) error { |
| 96 | + return errors.New("response editor error") |
| 97 | + }) |
| 98 | + _, err = c.Foo(ctx, api.FooParams{Body: "test"}, op) |
| 99 | + require.ErrorContains(t, err, `response editor error`) |
| 100 | + }) |
| 101 | +} |
| 102 | + |
| 103 | +type testFaultyClient struct{} |
| 104 | + |
| 105 | +var _ ht.Client = (*testFaultyClient)(nil) |
| 106 | + |
| 107 | +func (f *testFaultyClient) Do(req *http.Request) (*http.Response, error) { |
| 108 | + return nil, errors.New("test faulty client") |
| 109 | +} |
0 commit comments