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

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

Closed
benhoyt opened this issue Jun 25, 2023 · 2 comments
Closed

Comments

@benhoyt
Copy link

benhoyt commented Jun 25, 2023

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
@jba jba closed this as completed in 7c8e210 Jul 18, 2023
@jba
Copy link
Owner

jba commented Jul 18, 2023

a337a87 makes the server return 405 when appropriate.

@benhoyt
Copy link
Author

benhoyt commented Jul 18, 2023

Excellent, thanks. With those two fixes, my go-routing tests all pass!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants