Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(echo): add dynamic path support for echo endpoint #271

Merged
merged 2 commits into from
Jun 22, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 29 additions & 15 deletions pkg/api/echo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
6 changes: 4 additions & 2 deletions pkg/api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestions:

  • What about using PathPrefix() to allow "/echo/{anyPath}"?
  • What about accepting all HTTP methods, e.g. not restricting these paths to specific methods?
Suggested change
s.router.HandleFunc("/echo", s.echoHandler).Methods("POST,PUT")
s.router.HandleFunc("/echo/{echo:.*}", s.echoHandler).Methods("POST,PUT")
s.router.Path("/echo").HandlerFunc(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")
Expand All @@ -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")
Copy link

@merusso merusso Jun 7, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be consistent with the path patterns used above (currently api/echo{echo:.*} vs /echo/{echo:.*})

Copy link
Contributor Author

@jjchambl jjchambl Jun 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be consistent with the path patterns used above (currently api/echo{echo:.} vs /echo/{echo:.})

This is consistent with the previous route paths; both /api/echo and /echo were exposed previously.

s.router.HandleFunc("/ws/echo", s.echoWsHandler)
s.router.HandleFunc("/chunked", s.chunkedHandler)
s.router.HandleFunc("/chunked/{wait:[0-9]+}", s.chunkedHandler)
Expand Down