-
Notifications
You must be signed in to change notification settings - Fork 1
Closed
Description
I really hope the ServeMux
enhancements end up in the stdlib! And I apologize if this feedback is too early ... but while attempting to add a test using muxpatterns
(I'm really hoping this gets into the stdlib!), I noticed a couple of issues.
- There seems to be a routing bug where
HandleFunc("GET /api/widgets/{slug}", h)
matches the path/api/widgets/
and provides"/"
as the path value forslug
. I would have expected that to not match at all (slug needs to be 1 or more characters), as all the other routing libraries do.
Here's a program that reproduces this:
package main
import (
"fmt"
"log"
"net/http"
"github.com/jba/muxpatterns"
)
func main() {
mux := muxpatterns.NewServeMux()
mux.HandleFunc("GET /api/widgets/{slug}", func(w http.ResponseWriter, r *http.Request) {
slug := mux.PathValue(r, "slug")
fmt.Fprintf(w, "slug = %q\n", slug)
})
fmt.Println("Listening on http://localhost:8080")
log.Fatal(http.ListenAndServe(":8080", mux))
}
If you run that and then curl
that URL, you get this:
$ curl http://localhost:8080/api/widgets/ # expected 404
slug = "/"
$ curl http://localhost:8080/api/widgets/foo # 200 with slug "foo" as expected
slug = "foo"
- The other issue (and maybe this just hasn't been programmed yet) is that
muxpatterns
doesn't yet seem to return 405 MethodNotAllowed errors as expected. It definitely seems in the spirit of the HTTP status codes to return 405, and your comment here seems to suggest that the enhancedhttp.ServeMux
will too. Should be straight-forward to reproduce, but rungo test
on my test code if you can't.
$ curl -X POST http://localhost:8080/api/widgets/foo # expected 405
404 page not found
flibustenet
Metadata
Metadata
Assignees
Labels
No labels