github.com/iosif02/goja_nodejs@v1.0.1/url/url_test.go (about)

     1  package url
     2  
     3  import (
     4  	_ "embed"
     5  	"testing"
     6  
     7  	"github.com/iosif02/goja"
     8  	"github.com/iosif02/goja_nodejs/require"
     9  )
    10  
    11  func TestURL(t *testing.T) {
    12  	vm := goja.New()
    13  	new(require.Registry).Enable(vm)
    14  	Enable(vm)
    15  
    16  	if c := vm.Get("URL"); c == nil {
    17  		t.Fatal("URL not found")
    18  	}
    19  
    20  	script := `const url = new URL("https://user:pass@sub.example.com:8080/p/a/t/h?query=string#hash");`
    21  
    22  	if _, err := vm.RunString(script); err != nil {
    23  		t.Fatal("Failed to process url script.", err)
    24  	}
    25  }
    26  
    27  func TestGetters(t *testing.T) {
    28  	vm := goja.New()
    29  	new(require.Registry).Enable(vm)
    30  	Enable(vm)
    31  
    32  	if c := vm.Get("URL"); c == nil {
    33  		t.Fatal("URL not found")
    34  	}
    35  
    36  	script := `
    37  		new URL("https://user:pass@sub.example.com:8080/p/a/t/h?query=string#hashed");
    38  	`
    39  
    40  	v, err := vm.RunString(script)
    41  	if err != nil {
    42  		t.Fatal("Failed to process url script.", err)
    43  	}
    44  
    45  	url := v.ToObject(vm)
    46  
    47  	tests := []struct {
    48  		prop     string
    49  		expected string
    50  	}{
    51  		{
    52  			prop:     "hash",
    53  			expected: "#hashed",
    54  		},
    55  		{
    56  			prop:     "host",
    57  			expected: "sub.example.com:8080",
    58  		},
    59  		{
    60  			prop:     "hostname",
    61  			expected: "sub.example.com",
    62  		},
    63  		{
    64  			prop:     "href",
    65  			expected: "https://user:pass@sub.example.com:8080/p/a/t/h?query=string#hashed",
    66  		},
    67  		{
    68  			prop:     "origin",
    69  			expected: "https://sub.example.com",
    70  		},
    71  		{
    72  			prop:     "password",
    73  			expected: "pass",
    74  		},
    75  		{
    76  			prop:     "username",
    77  			expected: "user",
    78  		},
    79  		{
    80  			prop:     "port",
    81  			expected: "8080",
    82  		},
    83  		{
    84  			prop:     "protocol",
    85  			expected: "https:",
    86  		},
    87  		{
    88  			prop:     "search",
    89  			expected: "?query=string",
    90  		},
    91  	}
    92  
    93  	for _, test := range tests {
    94  		v := url.Get(test.prop).String()
    95  		if v != test.expected {
    96  			t.Fatal("failed to match " + test.prop + " property. got: " + v + ", expected: " + test.expected)
    97  		}
    98  	}
    99  }
   100  
   101  //go:embed testdata/url_test.js
   102  var urlTest string
   103  
   104  func TestJs(t *testing.T) {
   105  	vm := goja.New()
   106  	new(require.Registry).Enable(vm)
   107  	Enable(vm)
   108  
   109  	if c := vm.Get("URL"); c == nil {
   110  		t.Fatal("URL not found")
   111  	}
   112  
   113  	// Script will throw an error on failed validation
   114  
   115  	_, err := vm.RunScript("testdata/url_test.js", urlTest)
   116  	if err != nil {
   117  		if ex, ok := err.(*goja.Exception); ok {
   118  			t.Fatal(ex.String())
   119  		}
   120  		t.Fatal("Failed to process url script.", err)
   121  	}
   122  }