Skip to content

Commit 732475a

Browse files
committed
refactor(http): replace di context with http context in tests and middleware
- Updated tests and middleware to utilize the new `forge_http.NewContext` instead of the deprecated `di.NewContext`, ensuring consistency across the codebase. - Removed the `di` package references in favor of the new `http` package for context management, enhancing clarity and maintainability. - Adjusted related test cases to reflect the changes in context creation, improving overall test coverage and reliability.
1 parent de411d0 commit 732475a

26 files changed

Lines changed: 122 additions & 120 deletions

extensions/auth/providers/ldap_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414
"github.com/stretchr/testify/require"
1515
"github.com/xraph/forge"
1616
"github.com/xraph/forge/extensions/auth"
17-
"github.com/xraph/forge/internal/di"
17+
forge_http "github.com/xraph/forge/internal/http"
1818
"github.com/xraph/forge/internal/logger"
1919
)
2020

@@ -222,7 +222,7 @@ func TestLDAPProvider_Middleware(t *testing.T) {
222222

223223
req := httptest.NewRequest(http.MethodGet, "/", nil)
224224
rec := httptest.NewRecorder()
225-
ctx := di.NewContext(rec, req, nil)
225+
ctx := forge_http.NewContext(rec, req, nil)
226226

227227
_ = middleware(handler)(ctx)
228228

extensions/security/csrf_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
"time"
88

99
"github.com/xraph/forge"
10-
"github.com/xraph/forge/internal/di"
10+
forge_http "github.com/xraph/forge/internal/http"
1111
"github.com/xraph/forge/internal/logger"
1212
)
1313

@@ -30,7 +30,7 @@ func TestCSRFMiddleware_SafeMethods(t *testing.T) {
3030
t.Run(method, func(t *testing.T) {
3131
req := httptest.NewRequest(method, "/api/test", nil)
3232
w := httptest.NewRecorder()
33-
ctx := di.NewContext(w, req, nil)
33+
ctx := forge_http.NewContext(w, req, nil)
3434

3535
called := false
3636
next := func(ctx forge.Context) error {
@@ -76,7 +76,7 @@ func TestCSRFMiddleware_UnsafeMethods_NoToken(t *testing.T) {
7676
t.Run(method+"_NoToken", func(t *testing.T) {
7777
req := httptest.NewRequest(method, "/api/test", nil)
7878
w := httptest.NewRecorder()
79-
ctx := di.NewContext(w, req, nil)
79+
ctx := forge_http.NewContext(w, req, nil)
8080

8181
called := false
8282
next := func(ctx forge.Context) error {
@@ -120,7 +120,7 @@ func TestCSRFMiddleware_SkipPaths(t *testing.T) {
120120
t.Run(path, func(t *testing.T) {
121121
req := httptest.NewRequest(http.MethodPost, path, nil)
122122
w := httptest.NewRecorder()
123-
ctx := di.NewContext(w, req, nil)
123+
ctx := forge_http.NewContext(w, req, nil)
124124

125125
called := false
126126
next := func(ctx forge.Context) error {
@@ -164,7 +164,7 @@ func TestCSRFMiddleware_DisabledConfig(t *testing.T) {
164164
// Test POST without token when CSRF is disabled - should pass
165165
req := httptest.NewRequest(http.MethodPost, "/api/test", nil)
166166
w := httptest.NewRecorder()
167-
ctx := di.NewContext(w, req, nil)
167+
ctx := forge_http.NewContext(w, req, nil)
168168

169169
called := false
170170
next := func(ctx forge.Context) error {

internal/di/context.go

Lines changed: 0 additions & 8 deletions
This file was deleted.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package di
1+
package http
22

33
import (
44
"encoding"
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package di
1+
package http
22

33
import (
44
"bytes"
Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package di
1+
package http
22

33
import (
44
"context"
@@ -12,6 +12,7 @@ import (
1212
"strings"
1313
"time"
1414

15+
"github.com/xraph/forge/internal/di"
1516
"github.com/xraph/forge/internal/shared"
1617
)
1718

@@ -28,8 +29,8 @@ type Ctx struct {
2829
response http.ResponseWriter
2930
params map[string]string
3031
values map[string]any
31-
scope Scope
32-
container Container
32+
scope di.Scope
33+
container di.Container
3334
metrics Metrics
3435
healthManager HealthManager
3536
session shared.Session
@@ -43,8 +44,8 @@ type ResponseBuilder struct {
4344
}
4445

4546
// NewContext creates a new context.
46-
func NewContext(w http.ResponseWriter, r *http.Request, container Container) Context {
47-
var scope Scope
47+
func NewContext(w http.ResponseWriter, r *http.Request, container di.Container) shared.Context {
48+
var scope di.Scope
4849
if container != nil {
4950
scope = container.BeginScope()
5051
}
@@ -498,7 +499,7 @@ func (c *Ctx) WithContext(ctx context.Context) {
498499
}
499500

500501
// Container returns the DI container.
501-
func (c *Ctx) Container() Container {
502+
func (c *Ctx) Container() di.Container {
502503
return c.container
503504
}
504505

@@ -513,7 +514,7 @@ func (c *Ctx) HealthManager() HealthManager {
513514
}
514515

515516
// Scope returns the request scope.
516-
func (c *Ctx) Scope() Scope {
517+
func (c *Ctx) Scope() di.Scope {
517518
return c.scope
518519
}
519520

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package di
1+
package http
22

33
import (
44
"bytes"
@@ -12,6 +12,7 @@ import (
1212

1313
"github.com/stretchr/testify/assert"
1414
"github.com/stretchr/testify/require"
15+
"github.com/xraph/forge/internal/di"
1516
)
1617

1718
// Test service for DI injection.
@@ -398,7 +399,7 @@ func TestContext_WithContext(t *testing.T) {
398399
}
399400

400401
func TestContext_Container(t *testing.T) {
401-
container := NewContainer()
402+
container := di.NewContainer()
402403
req := httptest.NewRequest(http.MethodGet, "/test", nil)
403404
rec := httptest.NewRecorder()
404405

@@ -408,7 +409,7 @@ func TestContext_Container(t *testing.T) {
408409
}
409410

410411
func TestContext_Scope(t *testing.T) {
411-
container := NewContainer()
412+
container := di.NewContainer()
412413
req := httptest.NewRequest(http.MethodGet, "/test", nil)
413414
rec := httptest.NewRecorder()
414415

@@ -419,9 +420,9 @@ func TestContext_Scope(t *testing.T) {
419420
}
420421

421422
func TestContext_Resolve(t *testing.T) {
422-
container := NewContainer()
423+
container := di.NewContainer()
423424

424-
err := RegisterSingleton(container, "service", func(c Container) (*TestUserService, error) {
425+
err := di.RegisterSingleton(container, "service", func(c di.Container) (*TestUserService, error) {
425426
return &TestUserService{users: []string{"user1"}}, nil
426427
})
427428
require.NoError(t, err)
@@ -447,9 +448,9 @@ func TestContext_Resolve_NoContainer(t *testing.T) {
447448
}
448449

449450
func TestContext_Must(t *testing.T) {
450-
container := NewContainer()
451+
container := di.NewContainer()
451452

452-
err := RegisterSingleton(container, "service", func(c Container) (*TestUserService, error) {
453+
err := di.RegisterSingleton(container, "service", func(c di.Container) (*TestUserService, error) {
453454
return &TestUserService{}, nil
454455
})
455456
require.NoError(t, err)
@@ -464,7 +465,7 @@ func TestContext_Must(t *testing.T) {
464465
}
465466

466467
func TestContext_Must_Panic(t *testing.T) {
467-
container := NewContainer()
468+
container := di.NewContainer()
468469
req := httptest.NewRequest(http.MethodGet, "/test", nil)
469470
rec := httptest.NewRecorder()
470471

@@ -476,7 +477,7 @@ func TestContext_Must_Panic(t *testing.T) {
476477
}
477478

478479
func TestContext_Cleanup(t *testing.T) {
479-
container := NewContainer()
480+
container := di.NewContainer()
480481
req := httptest.NewRequest(http.MethodGet, "/test", nil)
481482
rec := httptest.NewRecorder()
482483

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package di
1+
package http
22

33
import (
44
"net/http"
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package di
1+
package http
22

33
import (
44
"net/http"
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package di
1+
package http
22

33
import (
44
"fmt"

0 commit comments

Comments
 (0)