Skip to content

Unexpected match providing path value of "/"; routing not giving 405's #1

@benhoyt

Description

@benhoyt

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.

  1. 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 for slug. 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"
  1. 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 enhanced http.ServeMux will too. Should be straight-forward to reproduce, but run go test on my test code if you can't.
$ curl -X POST http://localhost:8080/api/widgets/foo  # expected 405
404 page not found

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions