-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Append 'ws://' prefix if url starts with localhost (#132)
* test: refactor NormalizeURL tests * feat(NormalizeURL): add 'ws://' prefix if url starts with 'localhost' * test(NormalizeURL): add 'ws://' prefix if url is localhost
- Loading branch information
1 parent
6ed112c
commit 8aaf5b8
Showing
2 changed files
with
35 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,36 @@ | ||
package nostr | ||
|
||
import "fmt" | ||
import ( | ||
"testing" | ||
) | ||
|
||
func ExampleNormalizeURL() { | ||
fmt.Println(NormalizeURL("")) | ||
fmt.Println(NormalizeURL("wss://x.com/y")) | ||
fmt.Println(NormalizeURL("wss://x.com/y/")) | ||
fmt.Println(NormalizeURL("http://x.com/y")) | ||
fmt.Println(NormalizeURL(NormalizeURL("http://x.com/y"))) | ||
fmt.Println(NormalizeURL("wss://x.com")) | ||
fmt.Println(NormalizeURL("wss://x.com/")) | ||
fmt.Println(NormalizeURL(NormalizeURL(NormalizeURL("wss://x.com/")))) | ||
fmt.Println(NormalizeURL("x.com")) | ||
fmt.Println(NormalizeURL("x.com/")) | ||
fmt.Println(NormalizeURL("x.com////")) | ||
fmt.Println(NormalizeURL("x.com/?x=23")) | ||
type urlTest struct { | ||
url, expected string | ||
} | ||
|
||
var urlTests = []urlTest{ | ||
{"", ""}, | ||
{"wss://x.com/y", "wss://x.com/y"}, | ||
{"wss://x.com/y/", "wss://x.com/y"}, | ||
{"http://x.com/y", "ws://x.com/y"}, | ||
{NormalizeURL("http://x.com/y"), "ws://x.com/y"}, | ||
{NormalizeURL("wss://x.com"), "wss://x.com"}, | ||
{NormalizeURL("wss://x.com/"), "wss://x.com"}, | ||
{NormalizeURL(NormalizeURL(NormalizeURL("wss://x.com/"))), "wss://x.com"}, | ||
{"wss://x.com", "wss://x.com"}, | ||
{"wss://x.com/", "wss://x.com"}, | ||
{"x.com////", "wss://x.com"}, | ||
{"x.com/?x=23", "wss://x.com?x=23"}, | ||
{"localhost:4036", "ws://localhost:4036"}, | ||
{"localhost:4036/relay", "ws://localhost:4036/relay"}, | ||
{NormalizeURL("localhost:4036/relay"), "ws://localhost:4036/relay"}, | ||
} | ||
|
||
func TestNormalizeURL(t *testing.T) { | ||
|
||
// Output: | ||
// | ||
// wss://x.com/y | ||
// wss://x.com/y | ||
// ws://x.com/y | ||
// ws://x.com/y | ||
// wss://x.com | ||
// wss://x.com | ||
// wss://x.com | ||
// wss://x.com | ||
// wss://x.com | ||
// wss://x.com | ||
// wss://x.com?x=23 | ||
for _, test := range urlTests { | ||
if output := NormalizeURL(test.url); output != test.expected { | ||
t.Errorf("Output '%s' not equal to expected '%s'", output, test.expected) | ||
} | ||
} | ||
} |