Skip to content

Commit

Permalink
test: add tests for decoded port
Browse files Browse the repository at this point in the history
  • Loading branch information
maeb committed Jan 13, 2025
1 parent 7e1a6ad commit 0507ad4
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
22 changes: 22 additions & 0 deletions url/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,3 +211,25 @@ func BenchmarkIssue8(b *testing.B) {
})
}
}

func TestDecodedPort(t *testing.T) {
u, err := Parse("http://example.com:8080")
if err != nil {
t.Errorf("Parse() error = '%v'", err)
return
}

if u.decodedPort != 8080 {
t.Errorf("decodedPort got = '%v', want '%v'", u.decodedPort, 8080)
}

relative, err := ParseRef(u.String(), "/")
if err != nil {
t.Errorf("ParseRef() error = '%v'", err)
return
}

if u.decodedPort != relative.decodedPort {
t.Errorf("decodedPort got = '%v', want '%v'", relative.decodedPort, u.decodedPort)
}
}
21 changes: 21 additions & 0 deletions url/url_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -460,3 +460,24 @@ func TestUrl_Getters(t *testing.T) {
})
}
}

// Test that DecodedPort returns the correct port number
func Test_DecodedPort(t *testing.T) {
u, _ := Parse("http://example.com:8080")

u.SetPort("8081")
if u.DecodedPort() != 8081 {
t.Errorf("DecodedPort() = %v, want %v", u.DecodedPort(), 8081)
}

u.SetPort("")

// Determine default port for scheme
defaultPortString := defaultSpecialSchemes[u.Scheme()]
defaultPort, _ := strconv.Atoi(defaultPortString)

// DecodedPort() should return 80 by default
if u.DecodedPort() != defaultPort {
t.Errorf("DecodedPort() = %v, want %v", u.DecodedPort(), defaultPort)
}
}

0 comments on commit 0507ad4

Please sign in to comment.