Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions middleware/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ func NewRouter(ctx *Context, next http.Handler) http.Handler {
return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
if _, rCtx, ok := ctx.RouteInfo(r); ok {
next.ServeHTTP(rw, rCtx)
*r = *rCtx
return
}

Expand Down
65 changes: 65 additions & 0 deletions middleware/router_context_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package middleware

import (
stdcontext "context"
stderrors "errors"
"fmt"
"io"
"net/http"
"net/http/httptest"
"testing"

"github.com/go-openapi/runtime/internal/testing/petstore"
"github.com/go-openapi/testify/v2/assert"
"github.com/go-openapi/testify/v2/require"
)

func TestRouterContext_Issue375(t *testing.T) {
spec, api := petstore.NewAPI(t)
spec.Spec().BasePath = "/api/"
context := NewContext(spec, api, nil)

type authCtxKey uint8
const authCtx authCtxKey = iota + 1
authCtxErr := stderrors.New("test error in context")

mw := NewRouter(context, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// check context after API middleware
authContext := stdcontext.WithValue(r.Context(), authCtx, authCtxErr)
*r = *r.WithContext(authContext)
}))

start := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
t.Logf("calling API router with context: %v", authCtxErr)
mw.ServeHTTP(w, r)

value := r.Context().Value(authCtx)
assert.NotNilf(t, value, "end of middleware chain: expected to find authCtx in request context")

if value == nil {
w.WriteHeader(http.StatusInternalServerError)
}

errAuth, ok := value.(error)
assert.Truef(t, ok, "expected authCtx to be an error, but got: %T", value)
t.Logf("got context from request: %v", errAuth)
fmt.Fprintf(w, "%v", errAuth)
w.WriteHeader(http.StatusOK)

// *r = *r.WithContext(authContext)
// mw.ServeHTTP(w, r)
})

recorder := httptest.NewRecorder()
request, err := http.NewRequestWithContext(stdcontext.Background(), http.MethodGet, "/api/pets/123", nil)
require.NoError(t, err)

start.ServeHTTP(recorder, request)
assert.Equal(t, http.StatusOK, recorder.Code)

res := recorder.Result()
require.NotNil(t, res.Body)
msg, err := io.ReadAll(res.Body)
require.NoError(t, err)
t.Logf("response message: %q", string(msg))
}