Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

create url_string_test.go #97

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions internal/pkg/utils/url_string.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ func URLToString(u *url.URL) string {
tempHost = u.Hostname()
}

// Handle IPv6 Address Formatting
if strings.Contains(tempHost, ":") && !(strings.HasPrefix(tempHost, "[") && strings.HasSuffix(tempHost, "]")) {
tempHost = "[" + tempHost + "]"
}
Expand Down
94 changes: 94 additions & 0 deletions internal/pkg/utils/url_string_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package utils

import (
"net/url"
"testing"
)

func TestURLToString(t *testing.T) {
tests := []struct {
name string
input string
expectErr bool
expected string
}{
{
name: "Valid URL with query",
input: "http://bing.com/search?q=dotnet",
expectErr: false,
expected: "http://bing.com/search?q=dotnet",
},
{
name: "Valid URL without query",
input: "http://example.com",
expectErr: false,
expected: "http://example.com",
},
{
name: "URL with port",
input: "http://localhost:8080",
expectErr: false,
expected: "http://localhost:8080",
},
{
name: "URL with path",
input: "http://example.com/path/to/resource",
expectErr: false,
expected: "http://example.com/path/to/resource",
},
{
name: "Invalid URL without scheme",
input: "://missing-scheme.com",
expectErr: true,
expected: "",
},
{
name: "URL with fragment",
input: "http://example.com/path#section",
expectErr: false,
expected: "http://example.com/path#section",
},
{
name: "URL with IDN domain",
input: "http://xn--fsq.com",
expectErr: false,
expected: "http://xn--fsq.com",
},
{
name: "URL with unicode domain",
input: "http://bücher.de",
expectErr: false,
expected: "http://xn--bcher-kva.de",
},
{
name: "URL with another unicode domain",
input: "http://faß.de",
expectErr: false,
expected: "http://xn--fa-hia.de",
},
{
name: "URL with IPv6 address",
input: "http://[2001:db8::1]:8080",
expectErr: false,
expected: "http://[2001:db8::1]:8080",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
u, err := url.Parse(tt.input)
if (err != nil) != tt.expectErr {
t.Fatalf("Error parsing input string: %s, err: %v", tt.input, err)
}
if err == nil && tt.expectErr {
t.Fatalf("Expected an error for input string: %s, but got none", tt.input)
}
if err == nil {
result := URLToString(u)
if result != tt.expected {
t.Errorf("Result was incorrect, got: %s, want: %s", result, tt.expected)
}
}
})
}
}
Loading