Skip to content

Commit

Permalink
Normalize relay addresses
Browse files Browse the repository at this point in the history
  • Loading branch information
boreq committed Nov 20, 2023
1 parent ebe7340 commit 2cc60dc
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
3 changes: 3 additions & 0 deletions service/domain/relay_address.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ type RelayAddress struct {
}

func NewRelayAddress(s string) (RelayAddress, error) {
s = strings.TrimSpace(s)
s = strings.TrimRight(s, "/")

if !strings.HasPrefix(s, "ws://") && !strings.HasPrefix(s, "wss://") {
return RelayAddress{}, errors.New("invalid protocol")
}
Expand Down
53 changes: 53 additions & 0 deletions service/domain/relay_address_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package domain

import (
"testing"

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

func TestRelayAddress(t *testing.T) {
testCases := []struct {
Input string
Output string
}{
{
Input: "wss://example.com",
Output: "wss://example.com",
},
{
Input: "ws://example.com",
Output: "ws://example.com",
},

{
Input: " wss://example.com",
Output: "wss://example.com",
},
{
Input: "wss://example.com ",
Output: "wss://example.com",
},
{
Input: " wss://example.com ",
Output: "wss://example.com",
},

{
Input: "wss://example.com/",
Output: "wss://example.com",
},
{
Input: "wss://example.com/ ",
Output: "wss://example.com",
},
}

for _, testCase := range testCases {
t.Run(testCase.Input, func(t *testing.T) {
result, err := NewRelayAddress(testCase.Input)
require.NoError(t, err)
require.Equal(t, testCase.Output, result.String())
})
}
}

0 comments on commit 2cc60dc

Please sign in to comment.