From 5d1f4c1c62ec3612fb9d9b951f7a8c458dd44e26 Mon Sep 17 00:00:00 2001 From: Takato Fukui <50371596+eyasy1217@users.noreply.github.com> Date: Wed, 24 Jan 2024 10:23:37 +0900 Subject: [PATCH] Parse request body when using X-HTTP-Method-Override header (#3918) --- runtime/mux.go | 2 +- runtime/mux_test.go | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/runtime/mux.go b/runtime/mux.go index 628e1fde1cb..ed9a7e4387d 100644 --- a/runtime/mux.go +++ b/runtime/mux.go @@ -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 diff --git a/runtime/mux_test.go b/runtime/mux_test.go index 62dd7f40fd0..298f68c814f 100644 --- a/runtime/mux_test.go +++ b/runtime/mux_test.go @@ -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