From 3ef0b4cd09f09dd012c4a6fb0a9cfdbcc5b5fa78 Mon Sep 17 00:00:00 2001 From: Jacob Chambliss Date: Wed, 7 Jun 2023 16:55:25 -0500 Subject: [PATCH 1/2] feat: add dynamic path support for echo endpoint --- pkg/api/echo_test.go | 44 +++++++++++++++++++++++++++++--------------- pkg/api/server.go | 6 ++++-- 2 files changed, 33 insertions(+), 17 deletions(-) diff --git a/pkg/api/echo_test.go b/pkg/api/echo_test.go index a37da8e90..74e472a07 100644 --- a/pkg/api/echo_test.go +++ b/pkg/api/echo_test.go @@ -8,27 +8,41 @@ import ( ) func TestEchoHandler(t *testing.T) { - expected := `{"test": true}` - req, err := http.NewRequest("POST", "/api/echo", strings.NewReader(expected)) - if err != nil { - t.Fatal(err) + cases := []struct { + url string + method string + expected string + }{ + {url: "/api/echo", method: "POST", expected: `{"test": true}`}, + {url: "/api/echo", method: "PUT", expected: `{"test": true}`}, + {url: "/echo", method: "PUT", expected: `{"test": true}`}, + {url: "/echo/", method: "POST", expected: `{"test": true}`}, + {url: "/echo/test", method: "POST", expected: `{"test": true}`}, + {url: "/echo/test/", method: "POST", expected: `{"test": true}`}, + {url: "/echo/test/test123-test", method: "POST", expected: `{"test": true}`}, } - rr := httptest.NewRecorder() srv := NewMockServer() handler := http.HandlerFunc(srv.echoHandler) - handler.ServeHTTP(rr, req) + for _, c := range cases { + req, err := http.NewRequest(c.method, c.url, strings.NewReader(c.expected)) + if err != nil { + t.Fatal(err) + } + rr := httptest.NewRecorder() + handler.ServeHTTP(rr, req) - // Check the status code is what we expect. - if status := rr.Code; status != http.StatusAccepted { - t.Errorf("handler returned wrong status code: got %v want %v", - status, http.StatusAccepted) - } + // Check the status code is what we expect. + if status := rr.Code; status != http.StatusAccepted { + t.Errorf("handler returned wrong status code: got %v want %v", + status, http.StatusAccepted) + } - // Check the response body is what we expect. - if rr.Body.String() != expected { - t.Fatalf("handler returned unexpected body:\ngot \n%v \nwant \n%s", - rr.Body.String(), expected) + // Check the response body is what we expect. + if rr.Body.String() != c.expected { + t.Fatalf("handler returned unexpected body:\ngot \n%v \nwant \n%s", + rr.Body.String(), c.expected) + } } } diff --git a/pkg/api/server.go b/pkg/api/server.go index 7075a96aa..c90775d87 100644 --- a/pkg/api/server.go +++ b/pkg/api/server.go @@ -100,7 +100,8 @@ func (s *Server) registerHandlers() { s.router.HandleFunc("/", s.indexHandler).HeadersRegexp("User-Agent", "^Mozilla.*").Methods("GET") s.router.HandleFunc("/", s.infoHandler).Methods("GET") s.router.HandleFunc("/version", s.versionHandler).Methods("GET") - s.router.HandleFunc("/echo", s.echoHandler).Methods("POST") + s.router.HandleFunc("/echo", s.echoHandler).Methods("POST,PUT") + s.router.HandleFunc("/echo/{echo:.*}", s.echoHandler).Methods("POST,PUT") s.router.HandleFunc("/env", s.envHandler).Methods("GET", "POST") s.router.HandleFunc("/headers", s.echoHeadersHandler).Methods("GET", "POST") s.router.HandleFunc("/delay/{wait:[0-9]+}", s.delayHandler).Methods("GET").Name("delay") @@ -119,7 +120,8 @@ func (s *Server) registerHandlers() { s.router.HandleFunc("/token", s.tokenGenerateHandler).Methods("POST") s.router.HandleFunc("/token/validate", s.tokenValidateHandler).Methods("GET") s.router.HandleFunc("/api/info", s.infoHandler).Methods("GET") - s.router.HandleFunc("/api/echo", s.echoHandler).Methods("POST") + s.router.HandleFunc("/api/echo", s.echoHandler).Methods("POST,PUT") + s.router.HandleFunc("/api/echo{echo:.*}", s.echoHandler).Methods("POST,PUT") s.router.HandleFunc("/ws/echo", s.echoWsHandler) s.router.HandleFunc("/chunked", s.chunkedHandler) s.router.HandleFunc("/chunked/{wait:[0-9]+}", s.chunkedHandler) From 7280e43cbfd2312799f156656092355985ca11e9 Mon Sep 17 00:00:00 2001 From: Jacob Chambliss Date: Thu, 8 Jun 2023 10:14:21 -0500 Subject: [PATCH 2/2] chore: add pathprefix to enable dynamic paths for echo --- pkg/api/server.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/pkg/api/server.go b/pkg/api/server.go index c90775d87..b4224f45b 100644 --- a/pkg/api/server.go +++ b/pkg/api/server.go @@ -100,8 +100,8 @@ func (s *Server) registerHandlers() { s.router.HandleFunc("/", s.indexHandler).HeadersRegexp("User-Agent", "^Mozilla.*").Methods("GET") s.router.HandleFunc("/", s.infoHandler).Methods("GET") s.router.HandleFunc("/version", s.versionHandler).Methods("GET") - s.router.HandleFunc("/echo", s.echoHandler).Methods("POST,PUT") - s.router.HandleFunc("/echo/{echo:.*}", s.echoHandler).Methods("POST,PUT") + s.router.HandleFunc("/echo", s.echoHandler) + s.router.PathPrefix("/echo/").HandlerFunc(s.echoHandler) s.router.HandleFunc("/env", s.envHandler).Methods("GET", "POST") s.router.HandleFunc("/headers", s.echoHeadersHandler).Methods("GET", "POST") s.router.HandleFunc("/delay/{wait:[0-9]+}", s.delayHandler).Methods("GET").Name("delay") @@ -120,8 +120,8 @@ func (s *Server) registerHandlers() { s.router.HandleFunc("/token", s.tokenGenerateHandler).Methods("POST") s.router.HandleFunc("/token/validate", s.tokenValidateHandler).Methods("GET") s.router.HandleFunc("/api/info", s.infoHandler).Methods("GET") - s.router.HandleFunc("/api/echo", s.echoHandler).Methods("POST,PUT") - s.router.HandleFunc("/api/echo{echo:.*}", s.echoHandler).Methods("POST,PUT") + s.router.HandleFunc("/api/echo", s.echoHandler) + s.router.PathPrefix("/api/echo/").HandlerFunc(s.echoHandler) s.router.HandleFunc("/ws/echo", s.echoWsHandler) s.router.HandleFunc("/chunked", s.chunkedHandler) s.router.HandleFunc("/chunked/{wait:[0-9]+}", s.chunkedHandler)