...

Source file src/github.com/ory/x/urlx/join_test.go

Documentation: github.com/ory/x/urlx

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

View as plain text