Skip to content

Commit

Permalink
Go 1.22 update: parses the path values using stdlib if present
Browse files Browse the repository at this point in the history
  • Loading branch information
apourchet committed Jun 18, 2024
1 parent bde724d commit 1ec4e6a
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 25 deletions.
27 changes: 27 additions & 0 deletions decoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,33 @@ type holder struct {
}

func TestDecoder(t *testing.T) {
t.Run("decode full", func(t *testing.T) {
body := strings.NewReader(`{"body1":42}`)
url := fmt.Sprintf("http://localhost/path?query1=query1val&query2=1&query2=2")
req := httptest.NewRequest("POST", url, body)
req.Header.Set("header1", "header1val")
req.AddCookie(&http.Cookie{Name: "cookie1", Value: "true"})
req.SetPathValue("segment1", "segment1val")

decoder := NewDecoder()
into := holder{}
err := decoder.Decode(req, &into)
require.NoError(t, err)

_headerVal := "header1val"
expected := holder{
Body1: 42,
Header1: &_headerVal,
Segment1: "segment1val",
Cookie1: true,
Query1: "query1val",
Query2: []int{1, 2},
}
require.Equal(t, expected, into)
})
}

func TestDecoderGorillaMux(t *testing.T) {
t.Run("decode full", func(t *testing.T) {
body := strings.NewReader(`{"body1":42}`)
url := fmt.Sprintf("http://localhost/path?query1=query1val&query2=1&query2=2")
Expand Down
11 changes: 7 additions & 4 deletions defaults/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,18 @@ func GetHeader(req *http.Request, key string) (string, error) {
// knowing how the server mux has altered the request to let this
// information resurface.
func GetSegment(req *http.Request, key string) (string, error) {
if segmentVal := req.PathValue(key); segmentVal != "" {
return segmentVal, nil
}

// We need some insight into the parsing of the request path if we
// want access to this information. Therefore, we expect this to be
// user-supplied. As a default, we use gorilla's mux package to
// fetch the right variables.
segmentVal := mux.Vars(req)[key]
if segmentVal == "" {
return "", ErrValueNotFound
if segmentVal := mux.Vars(req)[key]; segmentVal != "" {
return segmentVal, nil
}
return segmentVal, nil
return "", ErrValueNotFound
}

// GetQueries returns the list of values that this query parameter
Expand Down
20 changes: 9 additions & 11 deletions examples/simple/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"net/http"

"github.com/apourchet/httpwrap"
"github.com/gorilla/mux"
)

// ***** Type Definitions *****
Expand Down Expand Up @@ -156,8 +155,6 @@ func (h *PetStoreHandler) ClearStore() error {
}

func main() {
r := mux.NewRouter()

handler := &PetStoreHandler{pets: map[string]*Pet{}}
mw := &Middlewares{}

Expand All @@ -166,14 +163,15 @@ func main() {
Before(mw.checkAPICreds).
Finally(mw.sendResponse)

r.Handle("/pets", wrapper.Wrap(handler.AddPet)).Methods("POST")
r.Handle("/pets", wrapper.Wrap(handler.GetPets)).Methods("GET")
r.Handle("/pets/filtered", wrapper.Wrap(handler.FilterPets)).Methods("GET")
r.Handle("/pets/{name}", wrapper.Wrap(handler.GetPetByName)).Methods("GET")
r.Handle("/pets/{name}", wrapper.Wrap(handler.UpdatePet)).Methods("PUT")
router := http.NewServeMux()
router.Handle("POST /pets", wrapper.Wrap(handler.AddPet))
router.Handle("GET /pets", wrapper.Wrap(handler.GetPets))
router.Handle("GET /pets/filtered", wrapper.Wrap(handler.FilterPets))
router.Handle("GET /pets/{name}", wrapper.Wrap(handler.GetPetByName))
router.Handle("PUT /pets/{name}", wrapper.Wrap(handler.UpdatePet))

r.Handle("/clear", wrapper.Wrap(handler.ClearStore)).Methods("POST")
router.Handle("POST /clear", wrapper.Wrap(handler.ClearStore))

http.Handle("/", r)
log.Fatal(http.ListenAndServe(":3000", r))
http.Handle("/", router)
log.Fatal(http.ListenAndServe(":3000", router))
}
16 changes: 7 additions & 9 deletions examples/standard/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"net/http"

"github.com/apourchet/httpwrap"
"github.com/gorilla/mux"
)

// ***** Type Definitions *****
Expand Down Expand Up @@ -125,18 +124,17 @@ func (h *PetStoreHandler) ClearStore() error {
}

func main() {
router := mux.NewRouter()

handler := &PetStoreHandler{pets: map[string]*Pet{}}
wrapper := httpwrap.NewStandardWrapper().Before(checkAPICreds)

router.Handle("/pets", wrapper.Wrap(handler.AddPet)).Methods("POST")
router.Handle("/pets", wrapper.Wrap(handler.GetPets)).Methods("GET")
router.Handle("/pets/filtered", wrapper.Wrap(handler.FilterPets)).Methods("GET")
router.Handle("/pets/{name}", wrapper.Wrap(handler.GetPetByName)).Methods("GET")
router.Handle("/pets/{name}", wrapper.Wrap(handler.UpdatePet)).Methods("PUT")
router := http.NewServeMux()
router.Handle("POST /pets", wrapper.Wrap(handler.AddPet))
router.Handle("GET /pets", wrapper.Wrap(handler.GetPets))
router.Handle("GET /pets/filtered", wrapper.Wrap(handler.FilterPets))
router.Handle("GET /pets/{name}", wrapper.Wrap(handler.GetPetByName))
router.Handle("PUT /pets/{name}", wrapper.Wrap(handler.UpdatePet))

router.Handle("/clear", wrapper.Wrap(handler.ClearStore)).Methods("POST")
router.Handle("POST /clear", wrapper.Wrap(handler.ClearStore))

http.Handle("/", router)
log.Fatal(http.ListenAndServe(":3000", router))
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/apourchet/httpwrap

go 1.20
go 1.22

require (
github.com/gorilla/mux v1.7.2
Expand Down

0 comments on commit 1ec4e6a

Please sign in to comment.