-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from carverauto/updates/cleanup
Updates/cleanup
- Loading branch information
Showing
5 changed files
with
115 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// Package middleware pkg/api/middleware/http_headers.go | ||
package middleware | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
|
||
customctx "github.com/carverauto/eventrunner/pkg/context" | ||
gofr "gofr.dev/pkg/gofr" | ||
gofrHTTP "gofr.dev/pkg/gofr/http" | ||
) | ||
|
||
func CustomHeadersMiddleware() gofrHTTP.Middleware { | ||
return func(inner http.Handler) http.Handler { | ||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
// Use the existing context to create a custom context | ||
gofrCtx := &gofr.Context{ | ||
Request: gofrHTTP.NewRequest(r), | ||
} | ||
customCtx := customctx.NewCustomContext(gofrCtx) | ||
|
||
// Extract headers from the HTTP request and store them in the custom context | ||
for key, values := range r.Header { | ||
if len(values) > 0 { | ||
customCtx.SetClaim(key, values[0]) | ||
} | ||
} | ||
|
||
// Create a new context with the custom context | ||
ctxWithCustom := context.WithValue(r.Context(), "customCtx", customCtx) | ||
|
||
// Store the custom context back into the request | ||
r = r.WithContext(ctxWithCustom) | ||
|
||
// Call the next handler in the chain | ||
inner.ServeHTTP(w, r) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// Package middleware pkg/api/middleware/http_headers_test.go | ||
package middleware | ||
|
||
import ( | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
customctx "github.com/carverauto/eventrunner/pkg/context" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestCustomHeadersMiddleware(t *testing.T) { | ||
// Create a sample HTTP request with headers | ||
req := httptest.NewRequest(http.MethodGet, "/test", nil) | ||
req.Header.Set("X-Custom-Header", "CustomValue") | ||
req.Header.Set("Authorization", "Bearer token") | ||
|
||
// Create a response recorder to capture the response | ||
rr := httptest.NewRecorder() | ||
|
||
// Create a mock final handler that will be called after the middleware | ||
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
// Extract the custom context from the request context | ||
customCtx, ok := r.Context().Value("customCtx").(*customctx.CustomContext) | ||
if !ok { | ||
t.Errorf("Failed to retrieve custom context") | ||
return | ||
} | ||
|
||
// Validate that the headers were correctly set in the custom context | ||
headerValue, _ := customCtx.GetClaim("X-Custom-Header") | ||
assert.Equal(t, "CustomValue", headerValue) | ||
|
||
authValue, _ := customCtx.GetClaim("Authorization") | ||
assert.Equal(t, "Bearer token", authValue) | ||
}) | ||
|
||
// Wrap the handler with the CustomHeadersMiddleware | ||
middleware := CustomHeadersMiddleware() | ||
wrappedHandler := middleware(handler) | ||
|
||
// Serve the request | ||
wrappedHandler.ServeHTTP(rr, req) | ||
|
||
// Verify the response status code | ||
assert.Equal(t, http.StatusOK, rr.Code) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters