Revision b7feb34acfc5a24212ad7846d6a8e30014cf88a2 authored by brendamuir on 17 October 2022, 15:54:00 UTC, committed by GitHub on 17 October 2022, 15:54:00 UTC
* Fixes relrefs

* Fixes more relrefs
1 parent 2bed451
Raw File
url_test.go
package util

import (
	"net/url"
	"testing"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

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

	tests := []struct {
		description string
		base        string
		path        string
		expected    string
	}{
		{
			description: "RHS is empty",
			base:        "http://localhost:8080",
			path:        "",
			expected:    "http://localhost:8080",
		},
		{
			description: "RHS is empty and LHS has trailing slash",
			base:        "http://localhost:8080/",
			path:        "",
			expected:    "http://localhost:8080/",
		},
		{
			description: "neither has trailing slash",
			base:        "http://localhost:8080",
			path:        "api",
			expected:    "http://localhost:8080/api",
		},
		{
			description: "LHS has trailing slash",
			base:        "http://localhost:8080/",
			path:        "api",
			expected:    "http://localhost:8080/api",
		},
		{
			description: "LHS and RHS has trailing slash",
			base:        "http://localhost:8080/",
			path:        "api/",
			expected:    "http://localhost:8080/api/",
		},
		{
			description: "LHS has trailing slash and RHS has preceding slash",
			base:        "http://localhost:8080/",
			path:        "/api/",
			expected:    "http://localhost:8080/api/",
		},
	}
	for _, testcase := range tests {
		t.Run("where "+testcase.description, func(t *testing.T) {
			assert.Equalf(
				t,
				testcase.expected,
				JoinURLFragments(testcase.base, testcase.path),
				"base: '%s', path: '%s'",
				testcase.base,
				testcase.path,
			)
		})
	}
}

func TestNewURLQueryReader(t *testing.T) {
	u, err := url.Parse("http://www.abc.com/foo?bar=baz&bar2=baz2")
	require.NoError(t, err)

	uqr, err := NewURLQueryReader(u)
	require.NoError(t, err)

	assert.Equal(t, "baz", uqr.Get("bar", "foodef"), "first param")
	assert.Equal(t, "baz2", uqr.Get("bar2", "foodef"), "second param")
	assert.Equal(t, "foodef", uqr.Get("bar3", "foodef"), "non-existing param, use fallback")
}
back to top