|
| 1 | +// Copyright © 2024 Ory Corp |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package contextx |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "net/http" |
| 9 | + "net/http/httptest" |
| 10 | + |
| 11 | + "github.com/gofrs/uuid" |
| 12 | + |
| 13 | + "github.com/ory/x/configx" |
| 14 | +) |
| 15 | + |
| 16 | +type ( |
| 17 | + TestConfigProvider struct { |
| 18 | + ConfigSchema []byte |
| 19 | + Options []configx.OptionModifier |
| 20 | + } |
| 21 | + contextKey int |
| 22 | +) |
| 23 | + |
| 24 | +func NewTestConfigProvider(schema []byte, opts ...configx.OptionModifier) *TestConfigProvider { |
| 25 | + return &TestConfigProvider{ |
| 26 | + ConfigSchema: schema, |
| 27 | + Options: opts, |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +func (t *TestConfigProvider) Network(ctx context.Context, network uuid.UUID) uuid.UUID { |
| 32 | + return (&Default{}).Network(ctx, network) |
| 33 | +} |
| 34 | + |
| 35 | +func (t *TestConfigProvider) Config(ctx context.Context, config *configx.Provider) *configx.Provider { |
| 36 | + values, ok := ctx.Value(contextConfigKey).([]map[string]any) |
| 37 | + if !ok { |
| 38 | + return config |
| 39 | + } |
| 40 | + |
| 41 | + opts := make([]configx.OptionModifier, 1, 1+len(values)) |
| 42 | + opts[0] = configx.WithValues(config.All()) |
| 43 | + for _, v := range values { |
| 44 | + opts = append(opts, configx.WithValues(v)) |
| 45 | + } |
| 46 | + config, err := configx.New(ctx, t.ConfigSchema, append(t.Options, opts...)...) |
| 47 | + if err != nil { |
| 48 | + // This is not production code. The provider is only used in tests. |
| 49 | + panic(err) |
| 50 | + } |
| 51 | + return config |
| 52 | +} |
| 53 | + |
| 54 | +const contextConfigKey contextKey = 1 |
| 55 | + |
| 56 | +var ( |
| 57 | + _ Contextualizer = (*TestConfigProvider)(nil) |
| 58 | +) |
| 59 | + |
| 60 | +func WithConfigValue(ctx context.Context, key string, value any) context.Context { |
| 61 | + return WithConfigValues(ctx, map[string]any{key: value}) |
| 62 | +} |
| 63 | + |
| 64 | +func WithConfigValues(ctx context.Context, setValues ...map[string]any) context.Context { |
| 65 | + values, ok := ctx.Value(contextConfigKey).([]map[string]any) |
| 66 | + if !ok { |
| 67 | + values = make([]map[string]any, 0) |
| 68 | + } |
| 69 | + newValues := make([]map[string]any, len(values), len(values)+len(setValues)) |
| 70 | + copy(newValues, values) |
| 71 | + newValues = append(newValues, setValues...) |
| 72 | + |
| 73 | + return context.WithValue(ctx, contextConfigKey, newValues) |
| 74 | +} |
| 75 | + |
| 76 | +type ConfigurableTestHandler struct { |
| 77 | + configs map[uuid.UUID][]map[string]any |
| 78 | + handler http.Handler |
| 79 | +} |
| 80 | + |
| 81 | +func NewConfigurableTestHandler(h http.Handler) *ConfigurableTestHandler { |
| 82 | + return &ConfigurableTestHandler{ |
| 83 | + configs: make(map[uuid.UUID][]map[string]any), |
| 84 | + handler: h, |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +func (t *ConfigurableTestHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { |
| 89 | + cID := r.Header.Get("Test-Config-Id") |
| 90 | + if config, ok := t.configs[uuid.FromStringOrNil(cID)]; ok { |
| 91 | + r = r.WithContext(WithConfigValues(r.Context(), config...)) |
| 92 | + } |
| 93 | + t.handler.ServeHTTP(w, r) |
| 94 | +} |
| 95 | + |
| 96 | +func (t *ConfigurableTestHandler) RegisterConfig(config ...map[string]any) uuid.UUID { |
| 97 | + id := uuid.Must(uuid.NewV4()) |
| 98 | + t.configs[id] = config |
| 99 | + return id |
| 100 | +} |
| 101 | + |
| 102 | +func (t *ConfigurableTestHandler) UseConfig(r *http.Request, id uuid.UUID) *http.Request { |
| 103 | + r.Header.Set("Test-Config-Id", id.String()) |
| 104 | + return r |
| 105 | +} |
| 106 | + |
| 107 | +func (t *ConfigurableTestHandler) UseConfigValues(r *http.Request, values ...map[string]any) *http.Request { |
| 108 | + return t.UseConfig(r, t.RegisterConfig(values...)) |
| 109 | +} |
| 110 | + |
| 111 | +type ConfigurableTestServer struct { |
| 112 | + *httptest.Server |
| 113 | + handler *ConfigurableTestHandler |
| 114 | + transport http.RoundTripper |
| 115 | +} |
| 116 | + |
| 117 | +func NewConfigurableTestServer(h http.Handler) *ConfigurableTestServer { |
| 118 | + handler := NewConfigurableTestHandler(h) |
| 119 | + server := httptest.NewServer(handler) |
| 120 | + |
| 121 | + t := server.Client().Transport |
| 122 | + cts := &ConfigurableTestServer{ |
| 123 | + handler: handler, |
| 124 | + Server: server, |
| 125 | + transport: t, |
| 126 | + } |
| 127 | + server.Client().Transport = cts |
| 128 | + return cts |
| 129 | +} |
| 130 | + |
| 131 | +func (t *ConfigurableTestServer) RoundTrip(r *http.Request) (*http.Response, error) { |
| 132 | + config, ok := r.Context().Value(contextConfigKey).([]map[string]any) |
| 133 | + if ok && config != nil { |
| 134 | + r = t.handler.UseConfigValues(r, config...) |
| 135 | + } |
| 136 | + return t.transport.RoundTrip(r) |
| 137 | +} |
| 138 | + |
| 139 | +type AutoContextClient struct { |
| 140 | + *http.Client |
| 141 | + transport http.RoundTripper |
| 142 | + ctx context.Context |
| 143 | +} |
| 144 | + |
| 145 | +func (t *ConfigurableTestServer) Client(ctx context.Context) *AutoContextClient { |
| 146 | + baseClient := *t.Server.Client() |
| 147 | + autoClient := &AutoContextClient{ |
| 148 | + Client: &baseClient, |
| 149 | + transport: t, |
| 150 | + ctx: ctx, |
| 151 | + } |
| 152 | + baseClient.Transport = autoClient |
| 153 | + return autoClient |
| 154 | +} |
| 155 | + |
| 156 | +func (c *AutoContextClient) RoundTrip(r *http.Request) (*http.Response, error) { |
| 157 | + return c.transport.RoundTrip(r.WithContext(c.ctx)) |
| 158 | +} |
0 commit comments