Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[question] How to retrieve the handler func without the middleware? #705

Open
1Mark opened this issue Dec 7, 2022 · 2 comments
Open

[question] How to retrieve the handler func without the middleware? #705

1Mark opened this issue Dec 7, 2022 · 2 comments
Labels

Comments

@1Mark
Copy link

1Mark commented Dec 7, 2022

I'm trying to write a test to confirm that both the POST and PATCH methods of the same URL go to the same handler.
So I tried the following

var match mux.RouteMatch
var handler http.Handler
req := &http.Request{}
req.URL = &url.URL{Path: "/post"}
req.Method = "POST"
if builder.router.Match(req, &match) {
    handler = match.Handler
}
g.Expect(handler).ToNot(BeNil())
fmt.Println(handler)

handler func is actually the handler func but wrapped in the MDW. Therefore, I couldn't assert that the handler was equal to my desired handler func

Any tips ?

@1Mark 1Mark added the question label Dec 7, 2022
@ah8ad3
Copy link

ah8ad3 commented Jul 16, 2023

@1Mark
It's been few months.
Maybe this test from the repository will help you rewrite you're test if you still need any help.

@theghostmac
Copy link

Perhaps this?

var match mux.RouteMatch
var handler http.Handler
req := &http.Request{}
req.URL = &url.URL{Path: "/post"}
req.Method = "POST"

if builder.router.Match(req, &match) {
    // Get the matched handler along with middleware
    matchedHandler := match.MatchedHandler

    // Define a temporary http.HandlerFunc to capture the inner handler
    tempHandlerFunc := func(w http.ResponseWriter, r *http.Request) {
        handler = matchedHandler
    }

    // Call the middleware with the temporary handler
    for _, mw := range match.Handler.Middlewares() {
        tempHandlerFunc = mw(tempHandlerFunc)
    }

    // Now 'handler' should contain the inner handler without middleware
}

g.Expect(handler).ToNot(BeNil())
fmt.Println(handler)

Unrelated, but I'm looking to contribute to this project in any capacity, but all I'm seeing in Issues are questions and discussions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: No status
Development

No branches or pull requests

3 participants