-
-
Notifications
You must be signed in to change notification settings - Fork 959
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: gracefully handle double slashes in URLs
Closes #779
- Loading branch information
Showing
9 changed files
with
77 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package testhelpers | ||
|
||
import ( | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
) | ||
|
||
func NewHTTPTestServer(t *testing.T, h http.Handler) *httptest.Server { | ||
ts := httptest.NewServer(h) | ||
t.Cleanup(ts.Close) | ||
return ts | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package x | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/julienschmidt/httprouter" | ||
"github.com/urfave/negroni" | ||
) | ||
|
||
var CleanPath negroni.HandlerFunc = func(rw http.ResponseWriter, r *http.Request, next http.HandlerFunc) { | ||
r.URL.Path = httprouter.CleanPath(r.URL.Path) | ||
next(rw, r) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package x | ||
|
||
import ( | ||
"fmt" | ||
"io/ioutil" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/bmizerany/assert" | ||
"github.com/stretchr/testify/require" | ||
"github.com/urfave/negroni" | ||
) | ||
|
||
func TestCleanPath(t *testing.T) { | ||
n := negroni.New(CleanPath) | ||
n.UseHandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
_, _ = w.Write([]byte(r.URL.String())) | ||
}) | ||
ts := httptest.NewServer(n) | ||
defer ts.Close() | ||
|
||
for k, tc := range [][]string{ | ||
{"//foo", "/foo"}, | ||
{"//foo//bar", "/foo/bar"}, | ||
} { | ||
t.Run(fmt.Sprintf("case=%d", k), func(t *testing.T) { | ||
res, err := ts.Client().Get(ts.URL + tc[0]) | ||
require.NoError(t, err) | ||
defer res.Body.Close() | ||
body, err := ioutil.ReadAll(res.Body) | ||
assert.Equal(t, string(body), tc[1]) | ||
}) | ||
} | ||
} |