Skip to content

Commit

Permalink
httputils: remove AddAllowHeader because chi now has it (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
phenpessoa authored Sep 8, 2023
1 parent fb012eb commit 401e8d7
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 105 deletions.
39 changes: 0 additions & 39 deletions netutils/httputils/httputils.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"strings"
"time"

"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
"go.uber.org/zap"
)
Expand Down Expand Up @@ -113,41 +112,3 @@ var methods = [9]string{
http.MethodPost, http.MethodPut,
http.MethodTrace,
}

// AddAllowHeader adds the Allow header to w and writes
// http.StatusMethodNotAllowed to the header.
//
// No headers can be added/set after AddAllowHeader is called.
//
// AddAllowHeader will not work if w's WriteHeader method was called before it.
//
// The caller can still write to w after calling AddAllowHeader.
//
// https://www.rfc-editor.org/rfc/rfc7231#section-6.5.5
func AddAllowHeader(r chi.Router, w http.ResponseWriter, req *http.Request) {
rctx := chi.RouteContext(req.Context())

routePath := rctx.RoutePath
if routePath == "" {
if req.URL.RawPath != "" {
routePath = req.URL.RawPath
} else {
routePath = req.URL.Path
}
if routePath == "" {
routePath = "/"
}
}

if rctx.RouteMethod == "" {
rctx.RouteMethod = req.Method
}

for _, m := range methods {
if r.Match(rctx, m, routePath) {
w.Header().Add("Allow", m)
}
}

w.WriteHeader(http.StatusMethodNotAllowed)
}
66 changes: 0 additions & 66 deletions netutils/httputils/httputils_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package httputils

import (
"context"
"net/http"
"net/http/httptest"
"strings"
"testing"

"github.com/go-chi/chi/v5"
"go.uber.org/zap"
"go.uber.org/zap/zaptest/observer"
)
Expand Down Expand Up @@ -163,67 +161,3 @@ func TestFindStatusCodeColor(t *testing.T) {
}
}
}

func TestAddAllowHeader(t *testing.T) {
r := chi.NewRouter()
r.Get("/", func(w http.ResponseWriter, r *http.Request) { _, _ = w.Write([]byte(`ok`)) })
r.Put("/", func(w http.ResponseWriter, r *http.Request) { _, _ = w.Write([]byte(`ok`)) })
r.Delete("/", func(w http.ResponseWriter, r *http.Request) { _, _ = w.Write([]byte(`ok`)) })
r.Patch("/", func(w http.ResponseWriter, r *http.Request) { _, _ = w.Write([]byte(`ok`)) })

for _, tc := range []struct {
name string
req *http.Request
}{
{
"filled rawpath",
func() *http.Request {
req := httptest.NewRequest("POST", "/", nil)
rctx := chi.NewRouteContext()
rctx.RoutePath = ""
req.URL.RawPath = "/"
req = req.WithContext(context.WithValue(req.Context(), chi.RouteCtxKey, rctx))
return req
}(),
},
{
"empty rawpath",
func() *http.Request {
req := httptest.NewRequest("POST", "/", nil)
rctx := chi.NewRouteContext()
rctx.RoutePath = ""
req.URL.RawPath = ""
req.URL.Path = ""
rctx.RouteMethod = ""
req = req.WithContext(context.WithValue(req.Context(), chi.RouteCtxKey, rctx))
return req
}(),
},
} {
t.Run(tc.name, func(t *testing.T) {
w := httptest.NewRecorder()
AddAllowHeader(r, w, tc.req)

if res := w.Result().StatusCode; res != http.StatusMethodNotAllowed {
t.Errorf("\ntest '%s' failed\nwrong status received: %d", tc.name, res)
}

want := []string{"GET", "PUT", "DELETE", "PATCH"}
got := w.Result().Header.Values("Allow")

if len(want) != len(got) {
t.Errorf("\ntest '%s' failed\nwrong amount of methods in header\nwant: %v\ngot: %v", tc.name, len(want), len(got))
}

OUTER:
for _, w := range want {
for _, g := range got {
if w == g {
continue OUTER
}
}
t.Errorf("\ntest '%s' failed\nmethod '%s' not present in header", tc.name, w)
}
})
}
}

0 comments on commit 401e8d7

Please sign in to comment.