36 lines
1.1 KiB
Go
36 lines
1.1 KiB
Go
package main
|
|
|
|
import "testing"
|
|
|
|
func TestParseAuthorizeURLAllowedOrigins(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
api string
|
|
expected string
|
|
}{
|
|
{name: "localhost", api: "http%3A%2F%2Flocalhost", expected: "http://localhost"},
|
|
{name: "localhost trailing slash", api: "http%3A%2F%2Flocalhost%2F", expected: "http://localhost"},
|
|
{name: "lan address", api: "http%3A%2F%2F192.168.10.6", expected: "http://192.168.10.6"},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
raw := "dcauth://authorize?app=order_site&challenge_id=challenge-id&challenge=challenge-value&api=" + tt.api
|
|
target, err := parseAuthorizeURL(raw)
|
|
if err != nil {
|
|
t.Fatalf("parseAuthorizeURL() error = %v", err)
|
|
}
|
|
if target.APIBase != tt.expected {
|
|
t.Fatalf("APIBase = %q, want %q", target.APIBase, tt.expected)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestParseAuthorizeURLRejectsUnknownOrigin(t *testing.T) {
|
|
raw := "dcauth://authorize?app=order_site&challenge_id=challenge-id&challenge=challenge-value&api=http%3A%2F%2Fexample.com"
|
|
if _, err := parseAuthorizeURL(raw); err == nil {
|
|
t.Fatal("parseAuthorizeURL() accepted an unknown origin")
|
|
}
|
|
}
|