Skip to content

Commit

Permalink
add JoinURL() to httputil
Browse files Browse the repository at this point in the history
  • Loading branch information
kjk committed Oct 9, 2021
1 parent 6429b81 commit b0c8f0d
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
14 changes: 14 additions & 0 deletions httputil/httputil.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,17 @@ func PostMultiPart(uri string, files map[string]string) ([]byte, error) {
}
return ioutil.ReadAll(resp.Body)
}

func JoinURL(s1, s2 string) string {
if strings.HasSuffix(s1, "/") {
if strings.HasPrefix(s2, "/") {
return s1 + s2[1:]
}
return s1 + s2
}

if strings.HasPrefix(s2, "/") {
return s1 + s2
}
return s1 + "/" + s2
}
22 changes: 22 additions & 0 deletions httputil/httputil_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package httputil

import (
"testing"

"github.com/kjk/common/assert"
)

func TestJoinURL(t *testing.T) {
tests := []string{
"foo", "bar", "foo/bar",
"foo", "/bar", "foo/bar",
"foo/", "bar", "foo/bar",
"foo/", "/bar", "foo/bar",
}
n := len(tests)
for i := 0; i < n; i += 3 {
got := JoinURL(tests[i], tests[i+1])
exp := tests[i+2]
assert.Equal(t, exp, got)
}
}

0 comments on commit b0c8f0d

Please sign in to comment.