1 package urlx
2
3 import (
4 "fmt"
5 "net/url"
6 "testing"
7
8 "github.com/stretchr/testify/assert"
9 "github.com/stretchr/testify/require"
10 )
11
12 func TestAppendPaths(t *testing.T) {
13 u, err := url.Parse("http://localhost/home/")
14 require.NoError(t, err)
15 assert.Equal(t, "http://localhost/home/", AppendPaths(u).String())
16
17 for k, tc := range []struct {
18 give []string
19 expect string
20 }{
21 {
22 give: []string{"http://localhost/", "/home"},
23 expect: "http://localhost/home",
24 },
25 {
26 give: []string{"http://localhost", "/home"},
27 expect: "http://localhost/home",
28 },
29 {
30 give: []string{"https://localhost/", "/home"},
31 expect: "https://localhost/home",
32 },
33 {
34 give: []string{"http://localhost/", "/home", "home/", "/home/"},
35 expect: "http://localhost/home/home/home/",
36 },
37 } {
38 t.Run(fmt.Sprintf("case=%d", k), func(t *testing.T) {
39 u, err := url.Parse(tc.give[0])
40 require.NoError(t, err)
41 assert.Equal(t, tc.expect, AppendPaths(u, tc.give[1:]...).String())
42 })
43 }
44 }
45
46 func TestAppendQuery(t *testing.T) {
47 u, err := url.Parse("http://localhost/home?foo=bar&baz=bar")
48 require.NoError(t, err)
49
50 assert.Equal(t, "http://localhost/home?baz=bar&foo=bar", SetQuery(u, url.Values{}).String())
51 assert.Equal(t, "http://localhost/home?bar=baz&baz=bar&foo=bar", SetQuery(u, url.Values{"bar": {"baz"}}).String())
52 assert.Equal(t, "http://localhost/home?bar=baz&baz=bar&foo=bar", SetQuery(u, url.Values{"bar": {"baz", "baz"}}).String())
53 assert.Equal(t, "http://localhost/home?baz=foo&foo=bar", SetQuery(u, url.Values{"baz": {"foo"}}).String())
54 }
55
View as plain text