Skip to content

Commit

Permalink
v2: correctly URL encode tailnet in buildTailnetURL (#95)
Browse files Browse the repository at this point in the history
Adds unit test to test URL building.

Updates tailscale/corp#21867

Signed-off-by: Percy Wegmann <[email protected]>
  • Loading branch information
oxtoacart authored Aug 6, 2024
1 parent 96ed52f commit ba52834
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 11 deletions.
6 changes: 2 additions & 4 deletions v2/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,10 +169,8 @@ func (c *Client) buildURL(pathElements ...any) *url.URL {
func (c *Client) buildTailnetURL(pathElements ...any) *url.URL {
allElements := make([]any, 2, len(pathElements)+2)
allElements[0] = "tailnet"
allElements[1] = url.PathEscape(c.Tailnet)
for _, element := range pathElements {
allElements = append(allElements, element)
}
allElements[1] = c.Tailnet
allElements = append(allElements, pathElements...)
return c.buildURL(allElements...)
}

Expand Down
30 changes: 23 additions & 7 deletions v2/client_test.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
package tailscale_test
package tailscale

import (
_ "embed"
"io"
"net/url"
"testing"

"github.com/stretchr/testify/assert"

"github.com/tailscale/tailscale-client-go/v2"
"github.com/stretchr/testify/require"
)

func TestErrorData(t *testing.T) {
t.Parallel()

t.Run("It should return the data element from a valid error", func(t *testing.T) {
expected := tailscale.APIError{
Data: []tailscale.APIErrorData{
expected := APIError{
Data: []APIErrorData{
{
User: "[email protected]",
Errors: []string{
Expand All @@ -25,11 +25,27 @@ func TestErrorData(t *testing.T) {
},
}

actual := tailscale.ErrorData(expected)
actual := ErrorData(expected)
assert.EqualValues(t, expected.Data, actual)
})

t.Run("It should return an empty slice for any other error", func(t *testing.T) {
assert.Empty(t, tailscale.ErrorData(io.EOF))
assert.Empty(t, ErrorData(io.EOF))
})
}

func Test_BuildTailnetURL(t *testing.T) {
t.Parallel()

base, err := url.Parse("http://example.com")
require.NoError(t, err)

c := &Client{
BaseURL: base,
Tailnet: "tn/with/slashes",
}
actual := c.buildTailnetURL("component/with/slashes")
expected, err := url.Parse("http://example.com/api/v2/tailnet/tn%2Fwith%2Fslashes/component%2Fwith%2Fslashes")
require.NoError(t, err)
assert.EqualValues(t, expected.String(), actual.String())
}

0 comments on commit ba52834

Please sign in to comment.