Skip to content

Commit 3a4ba08

Browse files
zepatrikory-bot
authored andcommitted
feat: move config testhelpers to ory/x
GitOrigin-RevId: fd484445e9715760231f7f86ec212d094e826377
1 parent e91a691 commit 3a4ba08

File tree

5 files changed

+166
-104
lines changed

5 files changed

+166
-104
lines changed

CHANGELOG.md

Lines changed: 2 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
55
**Table of Contents**
66

7-
- [0.0.0 (2025-07-11)](#000-2025-07-11)
7+
- [0.0.0 (2025-07-03)](#000-2025-07-03)
88
- [Breaking Changes](#breaking-changes)
99
- [Related issue(s)](#related-issues)
1010

1111
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
1212

13-
# [0.0.0](https://github.com/ory/hydra/compare/v2.3.0...v0.0.0) (2025-07-11)
13+
# [0.0.0](https://github.com/ory/hydra/compare/v2.3.0...v0.0.0) (2025-07-03)
1414
## Breaking Changes
1515

1616
This patch changes the behavior of configuration item `foo` to do bar. To keep the existing
@@ -41,10 +41,6 @@ If this pull request
4141

4242
### Bug Fixes
4343

44-
* Add repo syncing for polis ([46d17f8](https://github.com/ory/hydra/commit/46d17f8bfdc59e2185e9ce65823eb2652e01f1b8)):
45-
46-
GitOrigin-RevId: e277a25d594b512b800d39dd18f36ea3d99fcf84
47-
4844
* Allow updating when JWKS URI is set ([#3935](https://github.com/ory/hydra/issues/3935)) ([#3946](https://github.com/ory/hydra/issues/3946)) ([fb1655b](https://github.com/ory/hydra/commit/fb1655ba86077b10141132ed332ba8d6f8c70582)):
4945

5046
The client validator no longer rejects PATCH and PUT updates when `JSONWebKeysURI` is non-empty and `JSONWebKeys` is not nil.
@@ -75,10 +71,6 @@ If this pull request
7571
GitOrigin-RevId: 61645585277edd95914705499afd7211a85983eb
7672

7773
* CLI usage help examples ([#3943](https://github.com/ory/hydra/issues/3943)) ([e24f9a7](https://github.com/ory/hydra/commit/e24f9a704c22c72690bc20c498439865181d9239))
78-
* Copybara script ([7b33358](https://github.com/ory/hydra/commit/7b333585bb44a069bf47267c853aa2e91db0efa3)):
79-
80-
GitOrigin-RevId: 14665e01451ac5fcdda148b473b8fc35d4fe21ef
81-
8274
* Correct multiple instances of 'stragegy' typo ([#3906](https://github.com/ory/hydra/issues/3906)) ([50eefbc](https://github.com/ory/hydra/commit/50eefbc21c2c43d221b6079bbd78a33ef8c754c4)):
8375

8476
This commit addresses several occurrences where 'strategy' was
@@ -105,10 +97,6 @@ If this pull request
10597

10698
GitOrigin-RevId: 83312e6544bc33ccc30e1e1e414cc04910429192
10799

108-
* Include go.mod in vendored oryx ([08a3ab4](https://github.com/ory/hydra/commit/08a3ab43ddb1e2a0df430224f969fe3f0ba161bf)):
109-
110-
GitOrigin-RevId: 20365bbe6b2cf95ac7973bcca9056455d2cb3803
111-
112100
* **infrastructure:** Hydra oss CI ([e846541](https://github.com/ory/hydra/commit/e84654185cdfffbf160d5309f744795d15d723f9)):
113101

114102
GitOrigin-RevId: 3df0724e5ea4c81a0f4c481c1a3a34529356d073
@@ -157,14 +145,6 @@ If this pull request
157145

158146
GitOrigin-RevId: 64950988a466bbdb4f25b8d9f5c416ff591c00bf
159147

160-
* Use hard-coded fallback key instead of panic ([e1f6450](https://github.com/ory/hydra/commit/e1f645012f43f62928fdc79710b45d935878367f)):
161-
162-
GitOrigin-RevId: d7a2270bbf5360288199e9632b2eac6cbc29737c
163-
164-
* Use main branch for polis ([6c24e68](https://github.com/ory/hydra/commit/6c24e68995b8eae9ba0b8872867270ef1d35113b)):
165-
166-
GitOrigin-RevId: 04533493184c6abdc3a211daffd98f6b68e1c9cc
167-
168148
* Using uuid_generate_v4 function ([#3958](https://github.com/ory/hydra/issues/3958)) ([c206066](https://github.com/ory/hydra/commit/c20606606654af975e5d82998956bb998acee576)):
169149

170150
Removing the md5 function for the uuid generation with native pgsql
@@ -173,13 +153,6 @@ If this pull request
173153
Closes https://github.com/ory/hydra/issues/3844
174154

175155

176-
### Code Refactoring
177-
178-
* Move database meta functions to root x folder for reusability ([7e49133](https://github.com/ory/hydra/commit/7e49133f435d6a0f74a63e8f8d03c5d314d7d3c6)):
179-
180-
GitOrigin-RevId: 30ee938ea5f1d19bac8967e0ebfe2d595ec27d2b
181-
182-
183156
### Features
184157

185158
* Add error reason to OAuth2TokenExchangeError event ([#3971](https://github.com/ory/hydra/issues/3971)) ([241dd45](https://github.com/ory/hydra/commit/241dd45fa17ed10d1101d890199df47dab4dbce5))
@@ -337,10 +310,6 @@ If this pull request
337310

338311
GitOrigin-RevId: 591238768218ba2b5af93f91ac7e16f4c170da5b
339312

340-
* Use vendored ory/x ([6581e01](https://github.com/ory/hydra/commit/6581e01679b2e146433061cbaaebb80a0e3905b5)):
341-
342-
GitOrigin-RevId: 994f3b754946ca5b2bd1bab0fe20532f5d5ab62f
343-
344313

345314
### Performance Improvements
346315

@@ -353,13 +322,6 @@ If this pull request
353322

354323
* Parallelize and improve ([#3989](https://github.com/ory/hydra/issues/3989)) ([a47e395](https://github.com/ory/hydra/commit/a47e39513f1f08076849f77517977abffa195364))
355324

356-
### Unclassified
357-
358-
* Merge 3834fab8c161a7dc98d43f32acf8efd9e6e95352 into 4dae0f4a8785eb36d8dbb27137f6b924c1e0f0b5 ([dc84053](https://github.com/ory/hydra/commit/dc840535c19d58caf130966e00d3d2fe9f3eb577)):
359-
360-
GitOrigin-RevId: 0c2dcaa065b64d2aafbcfd49c79363a214c5b2aa
361-
362-
363325

364326
# [2.3.0](https://github.com/ory/hydra/compare/v2.2.0...v2.3.0) (2025-01-17)
365327

oryx/contextx/config.go

Lines changed: 0 additions & 47 deletions
This file was deleted.

oryx/contextx/contextual.go

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,7 @@ type (
2929
NoOp struct{}
3030
)
3131

32-
func (d *Static) Network(ctx context.Context, network uuid.UUID) uuid.UUID {
33-
return d.NID
34-
}
35-
36-
func (d *Static) Config(ctx context.Context, config *configx.Provider) *configx.Provider {
37-
return d.C
38-
}
39-
40-
func (d *NoOp) Network(ctx context.Context, network uuid.UUID) uuid.UUID {
41-
return network
42-
}
43-
44-
func (d *NoOp) Config(ctx context.Context, config *configx.Provider) *configx.Provider {
45-
return config
46-
}
32+
func (d *Static) Network(context.Context, uuid.UUID) uuid.UUID { return d.NID }
33+
func (d *Static) Config(context.Context, *configx.Provider) *configx.Provider { return d.C }
34+
func (d *NoOp) Network(_ context.Context, n uuid.UUID) uuid.UUID { return n }
35+
func (d *NoOp) Config(_ context.Context, c *configx.Provider) *configx.Provider { return c }

oryx/contextx/default.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@ type Default struct{}
1515

1616
var _ Contextualizer = (*Default)(nil)
1717

18-
func (d *Default) Network(ctx context.Context, network uuid.UUID) uuid.UUID {
18+
func (d *Default) Network(_ context.Context, network uuid.UUID) uuid.UUID {
1919
if network == uuid.Nil {
2020
panic("nid must be not nil")
2121
}
2222
return network
2323
}
2424

25-
func (d *Default) Config(ctx context.Context, config *configx.Provider) *configx.Provider {
25+
func (d *Default) Config(_ context.Context, config *configx.Provider) *configx.Provider {
2626
return config
2727
}

oryx/contextx/testhelpers.go

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
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

Comments
 (0)