1
0
Fork 0

Adding upstream version 0.0~git20250409.f7acab6.

Signed-off-by: Daniel Baumann <daniel@debian.org>
This commit is contained in:
Daniel Baumann 2025-05-22 11:36:18 +02:00
parent b9b5d88025
commit 21b930d007
Signed by: daniel
GPG key ID: FBB4F0E80A80222F
51 changed files with 11229 additions and 0 deletions

122
url/url_test.go Normal file
View file

@ -0,0 +1,122 @@
package url
import (
_ "embed"
"testing"
"github.com/dop251/goja"
"github.com/dop251/goja_nodejs/require"
)
func TestURL(t *testing.T) {
vm := goja.New()
new(require.Registry).Enable(vm)
Enable(vm)
if c := vm.Get("URL"); c == nil {
t.Fatal("URL not found")
}
script := `const url = new URL("https://user:pass@sub.example.com:8080/p/a/t/h?query=string#hash");`
if _, err := vm.RunString(script); err != nil {
t.Fatal("Failed to process url script.", err)
}
}
func TestGetters(t *testing.T) {
vm := goja.New()
new(require.Registry).Enable(vm)
Enable(vm)
if c := vm.Get("URL"); c == nil {
t.Fatal("URL not found")
}
script := `
new URL("https://user:pass@sub.example.com:8080/p/a/t/h?query=string#hashed");
`
v, err := vm.RunString(script)
if err != nil {
t.Fatal("Failed to process url script.", err)
}
url := v.ToObject(vm)
tests := []struct {
prop string
expected string
}{
{
prop: "hash",
expected: "#hashed",
},
{
prop: "host",
expected: "sub.example.com:8080",
},
{
prop: "hostname",
expected: "sub.example.com",
},
{
prop: "href",
expected: "https://user:pass@sub.example.com:8080/p/a/t/h?query=string#hashed",
},
{
prop: "origin",
expected: "https://sub.example.com",
},
{
prop: "password",
expected: "pass",
},
{
prop: "username",
expected: "user",
},
{
prop: "port",
expected: "8080",
},
{
prop: "protocol",
expected: "https:",
},
{
prop: "search",
expected: "?query=string",
},
}
for _, test := range tests {
v := url.Get(test.prop).String()
if v != test.expected {
t.Fatal("failed to match " + test.prop + " property. got: " + v + ", expected: " + test.expected)
}
}
}
//go:embed testdata/url_test.js
var urlTest string
func TestJs(t *testing.T) {
vm := goja.New()
new(require.Registry).Enable(vm)
Enable(vm)
if c := vm.Get("URL"); c == nil {
t.Fatal("URL not found")
}
// Script will throw an error on failed validation
_, err := vm.RunScript("testdata/url_test.js", urlTest)
if err != nil {
if ex, ok := err.(*goja.Exception); ok {
t.Fatal(ex.String())
}
t.Fatal("Failed to process url script.", err)
}
}