snapshot of https://github.com/ory/keto.git (master, 2026-09-16; blobs > 131072B omitted)
Some checks failed
CI / Run lints and checks (push) Failing after 47s
CI / Run tests (push) Failing after 1s
CI / Run race tests (push) Failing after 5s
CI / Build CLI docs (push) Has been skipped
CI / Run buf actions (push) Failing after 5s
CodeQL / Analyze (go) (push) Failing after 3s
CodeQL / Analyze (javascript) (push) Failing after 2s
Docker Image Scanners / scanners (push) Failing after 3s
Format / format (push) Failing after 3s
Synchronize Issue Labels / Synchronize Issue Labels (push) Has been skipped
CI / Generate release (push) Has been skipped
CI / Draft newsletter (push) Has been skipped
CI / Pending approval Slack notification (push) Has been skipped
CI / Send newsletter (push) Has been skipped
Some checks failed
CI / Run lints and checks (push) Failing after 47s
CI / Run tests (push) Failing after 1s
CI / Run race tests (push) Failing after 5s
CI / Build CLI docs (push) Has been skipped
CI / Run buf actions (push) Failing after 5s
CodeQL / Analyze (go) (push) Failing after 3s
CodeQL / Analyze (javascript) (push) Failing after 2s
Docker Image Scanners / scanners (push) Failing after 3s
Format / format (push) Failing after 3s
Synchronize Issue Labels / Synchronize Issue Labels (push) Has been skipped
CI / Generate release (push) Has been skipped
CI / Draft newsletter (push) Has been skipped
CI / Pending approval Slack notification (push) Has been skipped
CI / Send newsletter (push) Has been skipped
This commit is contained in:
43
oryx/contextx/contextual.go
Normal file
43
oryx/contextx/contextual.go
Normal file
@@ -0,0 +1,43 @@
|
||||
// Copyright © 2023 Ory Corp
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package contextx
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/ory/x/configx"
|
||||
|
||||
"github.com/gofrs/uuid"
|
||||
)
|
||||
|
||||
type (
|
||||
Contextualizer interface {
|
||||
// Network returns the network id for the given context.
|
||||
Network(ctx context.Context, network uuid.UUID) uuid.UUID
|
||||
|
||||
// Config returns the config for the given context.
|
||||
Config(ctx context.Context, config *configx.Provider) *configx.Provider
|
||||
}
|
||||
Provider interface {
|
||||
Contextualizer() Contextualizer
|
||||
}
|
||||
Static struct {
|
||||
NID uuid.UUID
|
||||
C *configx.Provider
|
||||
}
|
||||
)
|
||||
|
||||
func (d *Static) Network(_ context.Context, nid uuid.UUID) uuid.UUID {
|
||||
if d.NID == uuid.Nil {
|
||||
return nid
|
||||
}
|
||||
return d.NID
|
||||
}
|
||||
|
||||
func (d *Static) Config(_ context.Context, c *configx.Provider) *configx.Provider {
|
||||
if d.C == nil {
|
||||
return c
|
||||
}
|
||||
return d.C
|
||||
}
|
||||
39
oryx/contextx/contextual_mock.go
Normal file
39
oryx/contextx/contextual_mock.go
Normal file
@@ -0,0 +1,39 @@
|
||||
// Copyright © 2023 Ory Corp
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package contextx
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/ory/x/configx"
|
||||
|
||||
"github.com/gofrs/uuid"
|
||||
)
|
||||
|
||||
// TestContextualizer is a mock implementation of the Contextualizer interface.
|
||||
type TestContextualizer struct{}
|
||||
|
||||
type contextKeyFake int
|
||||
|
||||
// fakeNIDContext is a test key for NID.
|
||||
const fakeNIDContext contextKeyFake = 1
|
||||
|
||||
// SetNIDContext sets the nid for the given context.
|
||||
func SetNIDContext(ctx context.Context, nid uuid.UUID) context.Context {
|
||||
return context.WithValue(ctx, fakeNIDContext, nid) //nolint:staticcheck
|
||||
}
|
||||
|
||||
// Network returns the network id for the given context.
|
||||
func (d *TestContextualizer) Network(ctx context.Context, network uuid.UUID) uuid.UUID {
|
||||
nid, ok := ctx.Value(fakeNIDContext).(uuid.UUID)
|
||||
if !ok {
|
||||
return network
|
||||
}
|
||||
return nid
|
||||
}
|
||||
|
||||
// Config returns the config for the given context.
|
||||
func (d *TestContextualizer) Config(ctx context.Context, config *configx.Provider) *configx.Provider {
|
||||
return config
|
||||
}
|
||||
27
oryx/contextx/default.go
Normal file
27
oryx/contextx/default.go
Normal file
@@ -0,0 +1,27 @@
|
||||
// Copyright © 2023 Ory Corp
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package contextx
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/gofrs/uuid"
|
||||
|
||||
"github.com/ory/x/configx"
|
||||
)
|
||||
|
||||
type Default struct{}
|
||||
|
||||
var _ Contextualizer = (*Default)(nil)
|
||||
|
||||
func (d *Default) Network(_ context.Context, network uuid.UUID) uuid.UUID {
|
||||
if network == uuid.Nil {
|
||||
panic("nid must be not nil")
|
||||
}
|
||||
return network
|
||||
}
|
||||
|
||||
func (d *Default) Config(_ context.Context, config *configx.Provider) *configx.Provider {
|
||||
return config
|
||||
}
|
||||
158
oryx/contextx/testhelpers.go
Normal file
158
oryx/contextx/testhelpers.go
Normal file
@@ -0,0 +1,158 @@
|
||||
// Copyright © 2024 Ory Corp
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package contextx
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
|
||||
"github.com/gofrs/uuid"
|
||||
|
||||
"github.com/ory/x/configx"
|
||||
)
|
||||
|
||||
type (
|
||||
TestConfigProvider struct {
|
||||
ConfigSchema []byte
|
||||
Options []configx.OptionModifier
|
||||
}
|
||||
contextKey int
|
||||
)
|
||||
|
||||
func NewTestConfigProvider(schema []byte, opts ...configx.OptionModifier) *TestConfigProvider {
|
||||
return &TestConfigProvider{
|
||||
ConfigSchema: schema,
|
||||
Options: opts,
|
||||
}
|
||||
}
|
||||
|
||||
func (t *TestConfigProvider) Network(ctx context.Context, network uuid.UUID) uuid.UUID {
|
||||
return (&Default{}).Network(ctx, network)
|
||||
}
|
||||
|
||||
func (t *TestConfigProvider) Config(ctx context.Context, config *configx.Provider) *configx.Provider {
|
||||
values, ok := ctx.Value(contextConfigKey).([]map[string]any)
|
||||
if !ok {
|
||||
return config
|
||||
}
|
||||
|
||||
opts := make([]configx.OptionModifier, 1, 1+len(values))
|
||||
opts[0] = configx.WithValues(config.All())
|
||||
for _, v := range values {
|
||||
opts = append(opts, configx.WithValues(v))
|
||||
}
|
||||
config, err := configx.New(ctx, t.ConfigSchema, append(t.Options, opts...)...)
|
||||
if err != nil {
|
||||
// This is not production code. The provider is only used in tests.
|
||||
panic(err)
|
||||
}
|
||||
return config
|
||||
}
|
||||
|
||||
const contextConfigKey contextKey = 1
|
||||
|
||||
var (
|
||||
_ Contextualizer = (*TestConfigProvider)(nil)
|
||||
)
|
||||
|
||||
func WithConfigValue(ctx context.Context, key string, value any) context.Context {
|
||||
return WithConfigValues(ctx, map[string]any{key: value})
|
||||
}
|
||||
|
||||
func WithConfigValues(ctx context.Context, setValues ...map[string]any) context.Context {
|
||||
values, ok := ctx.Value(contextConfigKey).([]map[string]any)
|
||||
if !ok {
|
||||
values = make([]map[string]any, 0)
|
||||
}
|
||||
newValues := make([]map[string]any, len(values), len(values)+len(setValues))
|
||||
copy(newValues, values)
|
||||
newValues = append(newValues, setValues...)
|
||||
|
||||
return context.WithValue(ctx, contextConfigKey, newValues)
|
||||
}
|
||||
|
||||
type ConfigurableTestHandler struct {
|
||||
configs map[uuid.UUID][]map[string]any
|
||||
handler http.Handler
|
||||
}
|
||||
|
||||
func NewConfigurableTestHandler(h http.Handler) *ConfigurableTestHandler {
|
||||
return &ConfigurableTestHandler{
|
||||
configs: make(map[uuid.UUID][]map[string]any),
|
||||
handler: h,
|
||||
}
|
||||
}
|
||||
|
||||
func (t *ConfigurableTestHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
cID := r.Header.Get("Test-Config-Id")
|
||||
if config, ok := t.configs[uuid.FromStringOrNil(cID)]; ok {
|
||||
r = r.WithContext(WithConfigValues(r.Context(), config...))
|
||||
}
|
||||
t.handler.ServeHTTP(w, r)
|
||||
}
|
||||
|
||||
func (t *ConfigurableTestHandler) RegisterConfig(config ...map[string]any) uuid.UUID {
|
||||
id := uuid.Must(uuid.NewV4())
|
||||
t.configs[id] = config
|
||||
return id
|
||||
}
|
||||
|
||||
func (t *ConfigurableTestHandler) UseConfig(r *http.Request, id uuid.UUID) *http.Request {
|
||||
r.Header.Set("Test-Config-Id", id.String())
|
||||
return r
|
||||
}
|
||||
|
||||
func (t *ConfigurableTestHandler) UseConfigValues(r *http.Request, values ...map[string]any) *http.Request {
|
||||
return t.UseConfig(r, t.RegisterConfig(values...))
|
||||
}
|
||||
|
||||
type ConfigurableTestServer struct {
|
||||
*httptest.Server
|
||||
handler *ConfigurableTestHandler
|
||||
transport http.RoundTripper
|
||||
}
|
||||
|
||||
func NewConfigurableTestServer(h http.Handler) *ConfigurableTestServer {
|
||||
handler := NewConfigurableTestHandler(h)
|
||||
server := httptest.NewServer(handler)
|
||||
|
||||
t := server.Client().Transport
|
||||
cts := &ConfigurableTestServer{
|
||||
handler: handler,
|
||||
Server: server,
|
||||
transport: t,
|
||||
}
|
||||
server.Client().Transport = cts
|
||||
return cts
|
||||
}
|
||||
|
||||
func (t *ConfigurableTestServer) RoundTrip(r *http.Request) (*http.Response, error) {
|
||||
config, ok := r.Context().Value(contextConfigKey).([]map[string]any)
|
||||
if ok && config != nil {
|
||||
r = t.handler.UseConfigValues(r, config...)
|
||||
}
|
||||
return t.transport.RoundTrip(r)
|
||||
}
|
||||
|
||||
type AutoContextClient struct {
|
||||
*http.Client
|
||||
transport http.RoundTripper
|
||||
ctx context.Context
|
||||
}
|
||||
|
||||
func (t *ConfigurableTestServer) Client(ctx context.Context) *AutoContextClient {
|
||||
baseClient := *t.Server.Client()
|
||||
autoClient := &AutoContextClient{
|
||||
Client: &baseClient,
|
||||
transport: t,
|
||||
ctx: ctx,
|
||||
}
|
||||
baseClient.Transport = autoClient
|
||||
return autoClient
|
||||
}
|
||||
|
||||
func (c *AutoContextClient) RoundTrip(r *http.Request) (*http.Response, error) {
|
||||
return c.transport.RoundTrip(r.WithContext(c.ctx))
|
||||
}
|
||||
26
oryx/contextx/tree.go
Normal file
26
oryx/contextx/tree.go
Normal file
@@ -0,0 +1,26 @@
|
||||
// Copyright © 2023 Ory Corp
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package contextx
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type ContextKey int
|
||||
|
||||
const (
|
||||
ValidContextKey ContextKey = iota + 1
|
||||
)
|
||||
|
||||
var RootContext = context.WithValue(context.Background(), ValidContextKey, true)
|
||||
|
||||
func TestRootContext(t testing.TB) context.Context {
|
||||
return context.WithValue(t.Context(), ValidContextKey, true)
|
||||
}
|
||||
|
||||
func IsRootContext(ctx context.Context) bool {
|
||||
is, ok := ctx.Value(ValidContextKey).(bool)
|
||||
return is && ok
|
||||
}
|
||||
Reference in New Issue
Block a user