Skip to content

Commit

Permalink
test: cleanup of PR 2998 to simplify (#3051)
Browse files Browse the repository at this point in the history
<!--
Thank you for submitting a pull request!

Please make sure you have reviewed our contributors guide before
submitting your
first PR.

Please ensure you've addressed or included references to any related
issues.

Tips:
- Use keywords like "closes" or "fixes" followed by an issue number to
automatically close related issues when the PR is merged (e.g., "closes
#123" or "fixes #123").
- Describe the changes made in the PR.
- Ensure the PR has one of the required tags (kind:fix, kind:misc,
kind:break!, kind:refactor, kind:feat, kind:deps, kind:docs, kind:ci,
kind:chore, kind:testing)

-->

in kind replacement of
#2998 with the
simplified require assertion

---------

Co-authored-by: Håvard Anda Estensen <haavard.ae@gmail.com>
  • Loading branch information
2 people authored and renaynay committed Jan 15, 2024
1 parent d0fcfde commit e383fca
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
5 changes: 4 additions & 1 deletion libs/utils/address.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package utils

import (
"errors"
"fmt"
"net"
"strings"
)

var ErrInvalidIP = errors.New("invalid IP address or hostname given")

// SanitizeAddr trims leading protocol scheme and port from the given
// IP address or hostname if present.
func SanitizeAddr(addr string) (string, error) {
Expand All @@ -16,7 +19,7 @@ func SanitizeAddr(addr string) (string, error) {
addr = strings.TrimSuffix(addr, "/")
addr = strings.Split(addr, ":")[0]
if addr == "" {
return "", fmt.Errorf("invalid IP address or hostname given: %s", original)
return "", fmt.Errorf("%w: %s", ErrInvalidIP, original)
}
return addr, nil
}
Expand Down
5 changes: 4 additions & 1 deletion libs/utils/address_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ func TestSanitizeAddr(t *testing.T) {
var tests = []struct {
addr string
want string
err error
}{
// Testcase: trims protocol prefix
{addr: "http://celestia.org", want: "celestia.org"},
Expand All @@ -20,13 +21,15 @@ func TestSanitizeAddr(t *testing.T) {
{addr: "tcp://192.168.42.42:5050/", want: "192.168.42.42"},
// Testcase: invariant ip
{addr: "192.168.42.42", want: "192.168.42.42"},
// Testcase: empty addr
{addr: "", want: "", err: ErrInvalidIP},
}

for _, tt := range tests {
t.Run(tt.addr, func(t *testing.T) {
got, err := SanitizeAddr(tt.addr)
require.NoError(t, err)
require.Equal(t, tt.want, got)
require.ErrorIs(t, err, tt.err)
})
}
}
Expand Down

0 comments on commit e383fca

Please sign in to comment.