Skip to content

Commit

Permalink
refactor ctxutil to httputil
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffy-mathew committed Oct 8, 2024
1 parent c83cc5d commit 5b82e5c
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 21 deletions.
6 changes: 3 additions & 3 deletions ctx/ctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"encoding/json"
"net/http"

"github.com/TykTechnologies/tyk/internal/ctxutil"
"github.com/TykTechnologies/tyk/internal/httputil"

"github.com/TykTechnologies/tyk/apidef/oas"

Expand Down Expand Up @@ -78,7 +78,7 @@ func ctxSetSession(r *http.Request, s *user.SessionState, scheduleUpdate bool, h
s.Touch()
}

ctxutil.SetContext(r, ctx)
httputil.SetContext(r, ctx)
}

func GetAuthToken(r *http.Request) string {
Expand Down Expand Up @@ -116,7 +116,7 @@ func SetSession(r *http.Request, s *user.SessionState, scheduleUpdate bool, hash
func SetDefinition(r *http.Request, s *apidef.APIDefinition) {
ctx := r.Context()
ctx = context.WithValue(ctx, Definition, s)
ctxutil.SetContext(r, ctx)
httputil.SetContext(r, ctx)
}

func GetDefinition(r *http.Request) *apidef.APIDefinition {
Expand Down
5 changes: 2 additions & 3 deletions gateway/mw_upstream_basic_auth.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package gateway

import (
"github.com/TykTechnologies/tyk/internal/httputil"
"net/http"

"github.com/TykTechnologies/tyk/internal/ctxutil"
"github.com/TykTechnologies/tyk/internal/httputil"

"github.com/TykTechnologies/tyk/header"
)
Expand Down Expand Up @@ -47,7 +46,7 @@ func (t *UpstreamBasicAuth) ProcessRequest(_ http.ResponseWriter, r *http.Reques

upstreamBasicAuthProvider.AuthValue = httputil.AuthHeader(basicAuthConfig.Username, basicAuthConfig.Password)

ctxutil.SetUpstreamAuth(r, upstreamBasicAuthProvider)
httputil.SetUpstreamAuth(r, upstreamBasicAuthProvider)
return nil, http.StatusOK
}

Expand Down
4 changes: 1 addition & 3 deletions gateway/reverse_proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ import (
"sync"
"time"

"github.com/TykTechnologies/tyk/internal/ctxutil"

"github.com/akutz/memconn"
"github.com/gorilla/websocket"
"github.com/opentracing/opentracing-go"
Expand Down Expand Up @@ -1855,7 +1853,7 @@ func (p *ReverseProxy) addAuthInfo(outReq, req *http.Request) {
return
}

if authProvider := ctxutil.GetUpstreamAuth(req); authProvider != nil {
if authProvider := httputil.GetUpstreamAuth(req); authProvider != nil {
authProvider.Fill(outReq)
}
}
2 changes: 1 addition & 1 deletion internal/ctxutil/ctx.go → internal/httputil/context.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ctxutil
package httputil

import (
"context"
Expand Down
23 changes: 12 additions & 11 deletions internal/ctxutil/ctx_test.go → internal/httputil/context_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package ctxutil_test
package httputil_test

import (
"context"
"net/http"
"testing"

"github.com/TykTechnologies/tyk/internal/ctxutil"
"github.com/TykTechnologies/tyk/internal/httputil"

"github.com/TykTechnologies/tyk/internal/model"

"github.com/stretchr/testify/assert"
Expand All @@ -23,29 +24,29 @@ func TestUpstreamAuth(t *testing.T) {
mockAuthProvider := &model.MockUpstreamAuthProvider{}
req := createReq(t)

ctxutil.SetUpstreamAuth(req, mockAuthProvider)
httputil.SetUpstreamAuth(req, mockAuthProvider)

// Retrieve the auth provider from the request's context to verify it was set
retrievedAuth := ctxutil.GetUpstreamAuth(req)
retrievedAuth := httputil.GetUpstreamAuth(req)
assert.NotNil(t, retrievedAuth)
assert.Equal(t, mockAuthProvider, retrievedAuth)
})

t.Run("no auth provider", func(t *testing.T) {
req := createReq(t)

retrievedAuth := ctxutil.GetUpstreamAuth(req)
retrievedAuth := httputil.GetUpstreamAuth(req)
assert.Nil(t, retrievedAuth)
})

t.Run("invalid auth provider", func(t *testing.T) {
req := createReq(t)

// Set a context with a value that is not of type proxy.UpstreamAuthProvider
ctx := context.WithValue(req.Context(), ctxutil.ContextKey("upstream-auth"), "invalid-type")
ctxutil.SetContext(req, ctx)
ctx := context.WithValue(req.Context(), httputil.ContextKey("upstream-auth"), "invalid-type")
httputil.SetContext(req, ctx)

retrievedAuth := ctxutil.GetUpstreamAuth(req)
retrievedAuth := httputil.GetUpstreamAuth(req)
assert.Nil(t, retrievedAuth)
})
}
Expand All @@ -58,7 +59,7 @@ func TestSetContext(t *testing.T) {
ctx := context.WithValue(context.Background(), "key", "value")

// Call SetContext to update the request's context
ctxutil.SetContext(req, ctx)
httputil.SetContext(req, ctx)

// Verify that the request's context has been updated
retrievedValue := req.Context().Value("key")
Expand All @@ -75,7 +76,7 @@ func TestSetContext(t *testing.T) {
newCtx := context.WithValue(context.Background(), "newKey", "newValue")

// Call SetContext to update the request's context with the new context
ctxutil.SetContext(req, newCtx)
httputil.SetContext(req, newCtx)

assert.Nil(t, req.Context().Value("existingKey"))
assert.Equal(t, "newValue", req.Context().Value("newKey"))
Expand All @@ -86,7 +87,7 @@ func TestSetContext(t *testing.T) {

emptyCtx := context.Background()

ctxutil.SetContext(req, emptyCtx)
httputil.SetContext(req, emptyCtx)

assert.Equal(t, emptyCtx, req.Context())
})
Expand Down

0 comments on commit 5b82e5c

Please sign in to comment.