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 ae2153c
Show file tree
Hide file tree
Showing 2 changed files with 55 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
52 changes: 52 additions & 0 deletions service/domain/relay_address_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package domain

import (
"github.com/stretchr/testify/require"
"testing"
)

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 ae2153c

Please sign in to comment.