Skip to content

Commit

Permalink
Parse request body when using X-HTTP-Method-Override header
Browse files Browse the repository at this point in the history
  • Loading branch information
eyasy1217 committed Jan 22, 2024
1 parent 359dc11 commit baab8aa
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
2 changes: 1 addition & 1 deletion runtime/mux.go
Original file line number Diff line number Diff line change
Expand Up @@ -341,13 +341,13 @@ func (s *ServeMux) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}

if override := r.Header.Get("X-HTTP-Method-Override"); override != "" && s.isPathLengthFallback(r) {
r.Method = strings.ToUpper(override)
if err := r.ParseForm(); err != nil {
_, outboundMarshaler := MarshalerForRequest(s, r)
sterr := status.Error(codes.InvalidArgument, err.Error())
s.errorHandler(ctx, s, outboundMarshaler, w, r, sterr)
return
}
r.Method = strings.ToUpper(override)
}

var pathComponents []string
Expand Down
13 changes: 13 additions & 0 deletions runtime/mux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -607,6 +607,19 @@ func TestMuxServeHTTP(t *testing.T) {
}
}

func TestServeHTTP_WithMethodOverrideAndFormParsing(t *testing.T) {
r := httptest.NewRequest("POST", "/foo", strings.NewReader("bar=hoge"))
r.Header.Set("Content-Type", "application/x-www-form-urlencoded")
r.Header.Set("X-HTTP-Method-Override", "GET")
w := httptest.NewRecorder()

runtime.NewServeMux().ServeHTTP(w, r)

if r.FormValue("bar") != "hoge" {
t.Error("form is not parsed")
}
}

var defaultHeaderMatcherTests = []struct {
name string
in string
Expand Down

0 comments on commit baab8aa

Please sign in to comment.