4 changed files with 50 additions and 10 deletions
@ -0,0 +1,12 @@ |
|||||||
|
package httpserv |
||||||
|
|
||||||
|
import "net/url" |
||||||
|
|
||||||
|
// LocationWithTrailingSlash returns the URL in a relative format, with a trailing slash.
|
||||||
|
func LocationWithTrailingSlash(u *url.URL) string { |
||||||
|
l := "./" + u.Path[1:] + "/" |
||||||
|
if u.RawQuery != "" { |
||||||
|
l += "?" + u.RawQuery |
||||||
|
} |
||||||
|
return l |
||||||
|
} |
@ -0,0 +1,36 @@ |
|||||||
|
package httpserv |
||||||
|
|
||||||
|
import ( |
||||||
|
"net/url" |
||||||
|
"testing" |
||||||
|
|
||||||
|
"github.com/stretchr/testify/require" |
||||||
|
) |
||||||
|
|
||||||
|
func TestLocationWithTrailingSlash(t *testing.T) { |
||||||
|
for _, ca := range []struct { |
||||||
|
name string |
||||||
|
url *url.URL |
||||||
|
loc string |
||||||
|
}{ |
||||||
|
{ |
||||||
|
"with query", |
||||||
|
&url.URL{ |
||||||
|
Path: "/test", |
||||||
|
RawQuery: "key=value", |
||||||
|
}, |
||||||
|
"./test/?key=value", |
||||||
|
}, |
||||||
|
{ |
||||||
|
"xss", |
||||||
|
&url.URL{ |
||||||
|
Path: "/www.example.com", |
||||||
|
}, |
||||||
|
"./www.example.com/", |
||||||
|
}, |
||||||
|
} { |
||||||
|
t.Run(ca.name, func(t *testing.T) { |
||||||
|
require.Equal(t, ca.loc, LocationWithTrailingSlash(ca.url)) |
||||||
|
}) |
||||||
|
} |
||||||
|
} |
Loading…
Reference in new issue