mirror of
https://github.com/nbd-wtf/go-nostr.git
synced 2025-07-03 12:02:30 +02:00
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
This commit is contained in:
@ -15,6 +15,10 @@ func NormalizeURL(u string) string {
|
|||||||
u = strings.TrimSpace(u)
|
u = strings.TrimSpace(u)
|
||||||
u = strings.ToLower(u)
|
u = strings.ToLower(u)
|
||||||
|
|
||||||
|
if strings.HasPrefix(u, "localhost") == true {
|
||||||
|
u = "ws://" + u
|
||||||
|
}
|
||||||
|
|
||||||
if !strings.HasPrefix(u, "http") && !strings.HasPrefix(u, "ws") {
|
if !strings.HasPrefix(u, "http") && !strings.HasPrefix(u, "ws") {
|
||||||
u = "wss://" + u
|
u = "wss://" + u
|
||||||
}
|
}
|
||||||
|
@ -1,32 +1,36 @@
|
|||||||
package nostr
|
package nostr
|
||||||
|
|
||||||
import "fmt"
|
import (
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
func ExampleNormalizeURL() {
|
type urlTest struct {
|
||||||
fmt.Println(NormalizeURL(""))
|
url, expected string
|
||||||
fmt.Println(NormalizeURL("wss://x.com/y"))
|
}
|
||||||
fmt.Println(NormalizeURL("wss://x.com/y/"))
|
|
||||||
fmt.Println(NormalizeURL("http://x.com/y"))
|
var urlTests = []urlTest{
|
||||||
fmt.Println(NormalizeURL(NormalizeURL("http://x.com/y")))
|
{"", ""},
|
||||||
fmt.Println(NormalizeURL("wss://x.com"))
|
{"wss://x.com/y", "wss://x.com/y"},
|
||||||
fmt.Println(NormalizeURL("wss://x.com/"))
|
{"wss://x.com/y/", "wss://x.com/y"},
|
||||||
fmt.Println(NormalizeURL(NormalizeURL(NormalizeURL("wss://x.com/"))))
|
{"http://x.com/y", "ws://x.com/y"},
|
||||||
fmt.Println(NormalizeURL("x.com"))
|
{NormalizeURL("http://x.com/y"), "ws://x.com/y"},
|
||||||
fmt.Println(NormalizeURL("x.com/"))
|
{NormalizeURL("wss://x.com"), "wss://x.com"},
|
||||||
fmt.Println(NormalizeURL("x.com////"))
|
{NormalizeURL("wss://x.com/"), "wss://x.com"},
|
||||||
fmt.Println(NormalizeURL("x.com/?x=23"))
|
{NormalizeURL(NormalizeURL(NormalizeURL("wss://x.com/"))), "wss://x.com"},
|
||||||
|
{"wss://x.com", "wss://x.com"},
|
||||||
// Output:
|
{"wss://x.com/", "wss://x.com"},
|
||||||
//
|
{"x.com////", "wss://x.com"},
|
||||||
// wss://x.com/y
|
{"x.com/?x=23", "wss://x.com?x=23"},
|
||||||
// wss://x.com/y
|
{"localhost:4036", "ws://localhost:4036"},
|
||||||
// ws://x.com/y
|
{"localhost:4036/relay", "ws://localhost:4036/relay"},
|
||||||
// ws://x.com/y
|
{NormalizeURL("localhost:4036/relay"), "ws://localhost:4036/relay"},
|
||||||
// wss://x.com
|
}
|
||||||
// wss://x.com
|
|
||||||
// wss://x.com
|
func TestNormalizeURL(t *testing.T) {
|
||||||
// wss://x.com
|
|
||||||
// wss://x.com
|
for _, test := range urlTests {
|
||||||
// wss://x.com
|
if output := NormalizeURL(test.url); output != test.expected {
|
||||||
// wss://x.com?x=23
|
t.Errorf("Output '%s' not equal to expected '%s'", output, test.expected)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user