Skip to content

Commit

Permalink
Always append slash to path
Browse files Browse the repository at this point in the history
  • Loading branch information
sevein committed May 21, 2024
1 parent cf4b8fe commit 82874ed
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 13 deletions.
2 changes: 1 addition & 1 deletion example/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func TestRun(t *testing.T) {
stdout := bytes.NewBuffer([]byte{})

srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
assertEqual(t, r.URL.Path, "/api/v2/location")
assertEqual(t, r.URL.Path, "/api/v2/location/")
assertEqual(t, r.Header, http.Header(map[string][]string{
"Accept": {"application/json"},
"Accept-Encoding": {"gzip"},
Expand Down
13 changes: 2 additions & 11 deletions ssclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,18 +96,9 @@ func (p *authProvider) AuthenticateRequest(ctx context.Context, request *kabs.Re
// not something that we have not been able to describe using TypeSpec yet.
type appendTrailingSlashHandler struct{}

var useSlash = map[string]struct{}{
http.MethodPost: {},
http.MethodPut: {},
http.MethodPatch: {},
http.MethodDelete: {},
}

func (middleware appendTrailingSlashHandler) Intercept(pipeline khttp.Pipeline, middlewareIndex int, req *http.Request) (*http.Response, error) {
if _, ok := useSlash[req.Method]; ok {
if !strings.HasSuffix(req.URL.Path, "/") {
req.URL.Path += "/"
}
if !strings.HasSuffix(req.URL.Path, "/") {
req.URL.Path += "/"
}

return pipeline.Next(req, middlewareIndex)
Expand Down
2 changes: 1 addition & 1 deletion ssclient_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func assertEqual(t *testing.T, got, want interface{}) {

func TestClient(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
assertEqual(t, r.URL.Path, "/api/v2/location")
assertEqual(t, r.URL.Path, "/api/v2/location/")
assertEqual(t, r.Header, http.Header(map[string][]string{
"Accept": {"application/json"},
"Accept-Encoding": {"gzip"},
Expand Down

0 comments on commit 82874ed

Please sign in to comment.