diff --git a/test-app/app/src/main/assets/app/tests/testURLImpl.js b/test-app/app/src/main/assets/app/tests/testURLImpl.js index 08c340949..8bb1d4ff9 100644 --- a/test-app/app/src/main/assets/app/tests/testURLImpl.js +++ b/test-app/app/src/main/assets/app/tests/testURLImpl.js @@ -1,31 +1,62 @@ -describe("Test URL ", function () { - - it("Test invalid URL parsing", function(){ - var exceptionCaught = false; - try { - const url = new URL(''); - }catch(e){ - exceptionCaught = true; - } - expect(exceptionCaught).toBe(true); +describe("URL", function () { + it("throws on invalid URL", function () { + var exceptionCaught = false; + try { + const url = new URL(""); + } catch (e) { + exceptionCaught = true; + } + expect(exceptionCaught).toBe(true); }); - - it("Test valid URL parsing", function(){ - var exceptionCaught = false; - try { - const url = new URL('https://google.com'); - }catch(e){ - exceptionCaught = true; - } - expect(exceptionCaught).toBe(false); + + it("does not throw on valid URL", function () { + var exceptionCaught = false; + try { + const url = new URL("https://google.com"); + } catch (e) { + exceptionCaught = true; + } + expect(exceptionCaught).toBe(false); }); - - - it("Test URL fields", function(){ - var exceptionCaught = false; - const url = new URL('https://google.com'); - expect(url.protocol).toBe('https:'); - expect(url.hostname).toBe('google.com'); + + it("parses simple urls", function () { + const url = new URL("https://google.com"); + expect(url.protocol).toBe("https:"); + expect(url.hostname).toBe("google.com"); + expect(url.pathname).toBe("/"); + expect(url.port).toBe(""); + expect(url.search).toBe(""); + expect(url.hash).toBe(""); + expect(url.username).toBe(""); + expect(url.password).toBe(""); + expect(url.origin).toBe("https://google.com"); + expect(url.searchParams.size).toBe(0); }); - -}); + + it("parses with undefined base", function () { + const url = new URL("https://google.com", undefined); + expect(url.protocol).toBe("https:"); + expect(url.hostname).toBe("google.com"); + }); + + it("parses with null base", function () { + const url = new URL("https://google.com", null); + expect(url.protocol).toBe("https:"); + expect(url.hostname).toBe("google.com"); + }); + + it("parses query strings", function () { + const url = new URL("https://google.com?q=hello"); + expect(url.search).toBe("?q=hello"); + expect(url.searchParams.get("q")).toBe("hello"); + expect(url.pathname).toBe("/"); + }); + + it("parses query strings with pathname", function () { + const url = new URL("https://google.com/some/path?q=hello"); + expect(url.search).toBe("?q=hello"); + expect(url.searchParams.get("q")).toBe("hello"); + expect(url.pathname).toBe("/some/path"); + }); + }); + \ No newline at end of file diff --git a/test-app/runtime/src/main/cpp/URLImpl.cpp b/test-app/runtime/src/main/cpp/URLImpl.cpp index e876132f4..bc0f41a69 100644 --- a/test-app/runtime/src/main/cpp/URLImpl.cpp +++ b/test-app/runtime/src/main/cpp/URLImpl.cpp @@ -131,6 +131,16 @@ void URLImpl::Ctor(const v8::FunctionCallbackInfo &args) { auto result = ada::parse(url_string, &base_url.value()); + if (result) { + url = result.value(); + } else { + isolate->ThrowException( + v8::Exception::TypeError(v8::String::Empty(isolate))); + return; + } + } else { + // treat 2nd arg as undefined otherwise. + auto result = ada::parse(url_string, nullptr); if (result) { url = result.value(); } else { @@ -139,7 +149,6 @@ void URLImpl::Ctor(const v8::FunctionCallbackInfo &args) { return; } } - } else { auto result = ada::parse(url_string, nullptr); if (result) { @@ -149,7 +158,6 @@ void URLImpl::Ctor(const v8::FunctionCallbackInfo &args) { v8::Exception::TypeError(v8::String::Empty(isolate))); return; } - } auto ret = args.This(); @@ -162,7 +170,6 @@ void URLImpl::Ctor(const v8::FunctionCallbackInfo &args) { urlImpl->BindFinalizer(isolate, ret); args.GetReturnValue().Set(ret); - } @@ -178,7 +185,6 @@ void URLImpl::GetHash(v8::Local property, auto value = ptr->GetURL()->get_hash(); info.GetReturnValue().Set( ArgConverter::ConvertToV8String(isolate, value.data(), value.length())); - } void URLImpl::SetHash(v8::Local property, @@ -235,7 +241,6 @@ void URLImpl::GetHostName(v8::Local property, info.GetReturnValue().Set( ArgConverter::ConvertToV8String(isolate, value.data(), value.length())); - } void URLImpl::SetHostName(v8::Local property, @@ -265,7 +270,6 @@ void URLImpl::GetHref(v8::Local property, info.GetReturnValue().Set( ArgConverter::ConvertToV8String(isolate, value.data(), value.length())); - } void URLImpl::SetHref(v8::Local property, @@ -294,7 +298,6 @@ void URLImpl::GetOrigin(v8::Local property, info.GetReturnValue().Set( ArgConverter::ConvertToV8String(isolate, value.data(), value.length())); - } void URLImpl::GetPassword(v8::Local property, @@ -310,7 +313,6 @@ void URLImpl::GetPassword(v8::Local property, info.GetReturnValue().Set( ArgConverter::ConvertToV8String(isolate, value.data(), value.length())); - } void URLImpl::SetPassword(v8::Local property, @@ -339,7 +341,6 @@ void URLImpl::GetPathName(v8::Local property, info.GetReturnValue().Set( ArgConverter::ConvertToV8String(isolate, value.data(), value.length())); - } void URLImpl::SetPathName(v8::Local property, @@ -368,7 +369,6 @@ void URLImpl::GetPort(v8::Local property, info.GetReturnValue().Set( ArgConverter::ConvertToV8String(isolate, value.data(), value.length())); - } void URLImpl::SetPort(v8::Local property, @@ -397,7 +397,6 @@ void URLImpl::GetProtocol(v8::Local property, info.GetReturnValue().Set( ArgConverter::ConvertToV8String(isolate, value.data(), value.length())); - } void URLImpl::SetProtocol(v8::Local property, @@ -427,7 +426,6 @@ void URLImpl::GetSearch(v8::Local property, info.GetReturnValue().Set( ArgConverter::ConvertToV8String(isolate, value.data(), value.length())); - } void URLImpl::SetSearch(v8::Local property, @@ -457,7 +455,6 @@ void URLImpl::GetUserName(v8::Local property, info.GetReturnValue().Set( ArgConverter::ConvertToV8String(isolate, value.data(), value.length())); - } void URLImpl::SetUserName(v8::Local property, @@ -474,36 +471,36 @@ void URLImpl::SetUserName(v8::Local property, } -void URLImpl::ToString(const v8::FunctionCallbackInfo &args) { - URLImpl *ptr = GetPointer(args.This()); +void URLImpl::ToString(const v8::FunctionCallbackInfo &info) { + URLImpl *ptr = GetPointer(info.This()); if (ptr == nullptr) { - args.GetReturnValue().SetEmptyString(); + info.GetReturnValue().SetEmptyString(); return; } - auto isolate = args.GetIsolate(); + auto isolate = info.GetIsolate(); auto value = ptr->GetURL()->get_href(); auto ret = ArgConverter::ConvertToV8String(isolate, value.data(), value.length()); - args.GetReturnValue().Set(ret); + info.GetReturnValue().Set(ret); } -void URLImpl::CanParse(const v8::FunctionCallbackInfo &args) { +void URLImpl::CanParse(const v8::FunctionCallbackInfo &info) { bool value; - auto count = args.Length(); + auto count = info.Length(); if (count > 1) { - auto url_string = ArgConverter::ConvertToString(args[0].As()); - auto base_string = ArgConverter::ConvertToString(args[1].As()); + auto url_string = ArgConverter::ConvertToString(info[0].As()); + auto base_string = ArgConverter::ConvertToString(info[1].As()); std::string_view base_string_view(base_string.data(), base_string.length()); value = can_parse(url_string, &base_string_view); } else { - value = can_parse(ArgConverter::ConvertToString(args[0].As()).c_str(), nullptr); + value = can_parse(ArgConverter::ConvertToString(info[0].As()).c_str(), nullptr); } - args.GetReturnValue().Set(value); + info.GetReturnValue().Set(value); }