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

[bug] Adding to GET the same endpoint with POST and different Queries ends up with inconsistent error messages #704

Closed
Ivasan7 opened this issue Dec 7, 2022 · 0 comments · Fixed by #712
Labels

Comments

@Ivasan7
Copy link

Ivasan7 commented Dec 7, 2022

Describe the bug
Adding the same endpoint POST with different Queries ends up with incosnsistent error messages

Versions
Go version: 1.18

Steps to Reproduce
Playground: https://go.dev/play/p/xzoAkpEhGgy

// You can edit this code!
// Click here and start typing.
package main

import (
	"encoding/json"
	"fmt"
	"net/http"
	"net/http/httptest"
	"time"

	"github.com/gorilla/mux"
)

func main() {

	r := mux.NewRouter()

	r.HandleFunc("/api/v2", func(w http.ResponseWriter, r *http.Request) {
		// an example API handler
		fmt.Fprintf(w, "You made a POST request")
		json.NewEncoder(w).Encode(map[string]bool{"ok": true})
	}).Methods("POST")

	r.HandleFunc("/api/v2", func(w http.ResponseWriter, r *http.Request) {
		// an example API handler
		fmt.Fprintf(w, "You made a GET request")
		json.NewEncoder(w).Encode(map[string]bool{"ok": true})
	}).
		Queries("from", "{from:[0-9]+}",
			"to", "{to:[0-9]+}").Methods("GET")


	srv := &http.Server{
		Handler: r,
		Addr:    "127.0.0.1:8000",
		// Good practice: enforce timeouts for servers you create!
		WriteTimeout: 15 * time.Second,
		ReadTimeout:  15 * time.Second,
	}
	go srv.ListenAndServe()


	req2 := httptest.NewRequest("GET", "/api/v2?from=3&to=-5", nil)
	out2 := httptest.NewRecorder()

	r.ServeHTTP(out2, req2)

	fmt.Println(out2.Code)
	fmt.Println(out2)

}

ENDS with the result, I was expecting 404:

405

HOWEVER

When I remove the POST request
playground: https://go.dev/play/p/EXiF00_WrFW

// You can edit this code!
// Click here and start typing.
package main

import (
	"encoding/json"
	"fmt"
	"net/http"
	"net/http/httptest"
	"time"

	"github.com/gorilla/mux"
)

func main() {

	r := mux.NewRouter()

	r.HandleFunc("/api/v2", func(w http.ResponseWriter, r *http.Request) {
		// an example API handler
		fmt.Fprintf(w, "You made a GET request")
		json.NewEncoder(w).Encode(map[string]bool{"ok": true})
	}).
		Queries("from", "{from:[0-9]+}",
			"to", "{to:[0-9]+}").Methods("GET")


	srv := &http.Server{
		Handler: r,
		Addr:    "127.0.0.1:8000",
		// Good practice: enforce timeouts for servers you create!
		WriteTimeout: 15 * time.Second,
		ReadTimeout:  15 * time.Second,
	}
	go srv.ListenAndServe()


	req2 := httptest.NewRequest("GET", "/api/v2?from=3&to=-5", nil)
	out2 := httptest.NewRecorder()

	r.ServeHTTP(out2, req2)

	fmt.Println(out2.Code)

}

Result is

 404

Expected behavior
Both cases should end in only 404-s

@Ivasan7 Ivasan7 added the bug label Dec 7, 2022
@Ivasan7 Ivasan7 changed the title [bug] Adding the same endpoint POST with different Queries ends up with incosnsistent error messages [bug] Adding the same endpoint POST with different Queries ends up with inconsistent error messages Dec 7, 2022
@Ivasan7 Ivasan7 changed the title [bug] Adding the same endpoint POST with different Queries ends up with inconsistent error messages [bug] Adding to GET the same endpoint with POST and different Queries ends up with inconsistent error messages Dec 7, 2022
soheilrt added a commit to soheilrt/mux that referenced this issue Jul 14, 2023
The logical behavour of a router should return http status code of 404 when a request fails to stisfy a route validation logics. Before this, mux was returning 405 http status code in some rare scenarios which is not a valid on its case.

For more info, See: gorilla#704

Signed-off-by: Soheil Rahmat <soheilrt314@gmail.com>
soheilrt added a commit to soheilrt/mux that referenced this issue Jul 14, 2023
The logical behavour of a router should return http status code of 404 when a request fails to stisfy a route validation logics. Before this, mux was returning 405 http status code in some rare scenarios which is not a valid on its case.

For more info, See: gorilla#704
coreydaley added a commit that referenced this issue Aug 17, 2023
The logical behavior of a router should return an HTTP status code of
404 when a request fails to satisfy route validation logic. Previously,
MUX was returning a 405 HTTP status code in some rare scenarios, which
was not valid in its case.

For more info, See: #704

Fixes #704 

**Summary of Changes**

1. Clear the mismatch error of the previous validations on method match.
2. Added related tests

> PS: Make sure your PR includes/updates tests! If you need help with
this part, just ask!

Co-authored-by: Corey Daley <cdaley@redhat.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: ✅ Done
Development

Successfully merging a pull request may close this issue.

1 participant