1 #include "gtest/gtest.h"
4 #include "nsIURIMutator.h"
6 #include "nsThreadPool.h"
9 TEST(TestURIMutator
, Mutator
)
13 // This test instantiates a new nsStandardURL::Mutator (via contractID)
14 // and uses it to create a new URI.
16 nsresult rv
= NS_MutateURI(NS_STANDARDURLMUTATOR_CONTRACTID
)
17 .SetSpec("http://example.com"_ns
)
20 ASSERT_EQ(uri
->GetSpec(out
), NS_OK
);
21 ASSERT_TRUE(out
== "http://example.com/"_ns
);
23 // This test verifies that we can use NS_MutateURI to change a URI
24 rv
= NS_MutateURI(uri
)
26 .SetHost("mozilla.org"_ns
)
27 .SetPathQueryRef("/path?query#ref"_ns
)
30 ASSERT_EQ(uri
->GetSpec(out
), NS_OK
);
31 ASSERT_TRUE(out
== "ftp://mozilla.org/path?query#ref"_ns
);
33 // This test verifies that we can pass nsIURL to Finalize, and
35 rv
= NS_MutateURI(uri
).SetScheme("https"_ns
).Finalize(url
);
37 ASSERT_EQ(url
->GetSpec(out
), NS_OK
);
38 ASSERT_TRUE(out
== "https://mozilla.org/path?query#ref"_ns
);
40 // This test verifies that we can pass nsIURL** to Finalize.
41 // We need to use the explicit template because it's actually passing
43 nsCOMPtr
<nsIURL
> url2
;
44 rv
= NS_MutateURI(url
)
46 .Finalize
<nsIURL
>(getter_AddRefs(url2
));
48 ASSERT_EQ(url2
->GetSpec(out
), NS_OK
);
49 ASSERT_TRUE(out
== "https://mozilla.org/path?query#newref"_ns
);
51 // This test verifies that we can pass nsIURI** to Finalize.
52 // No need to be explicit.
53 auto functionSetRef
= [](nsIURI
* aURI
, nsIURI
** aResult
) -> nsresult
{
54 return NS_MutateURI(aURI
).SetRef("originalRef"_ns
).Finalize(aResult
);
57 nsCOMPtr
<nsIURI
> newURI
;
58 rv
= functionSetRef(url2
, getter_AddRefs(newURI
));
60 ASSERT_EQ(newURI
->GetSpec(out
), NS_OK
);
61 ASSERT_TRUE(out
== "https://mozilla.org/path?query#originalRef"_ns
);
63 // This test verifies that we can pass nsIURI** to Finalize.
64 nsCOMPtr
<nsIURI
> uri2
;
66 NS_MutateURI(url2
).SetQuery("newquery"_ns
).Finalize(getter_AddRefs(uri2
));
68 ASSERT_EQ(uri2
->GetSpec(out
), NS_OK
);
69 ASSERT_TRUE(out
== "https://mozilla.org/path?newquery#newref"_ns
);
71 // This test verifies that we can pass nsIURI** to Finalize.
72 // No need to be explicit.
73 auto functionSetQuery
= [](nsIURI
* aURI
, nsIURL
** aResult
) -> nsresult
{
74 return NS_MutateURI(aURI
).SetQuery("originalQuery"_ns
).Finalize(aResult
);
77 nsCOMPtr
<nsIURL
> newURL
;
78 rv
= functionSetQuery(uri2
, getter_AddRefs(newURL
));
80 ASSERT_EQ(newURL
->GetSpec(out
), NS_OK
);
81 ASSERT_TRUE(out
== "https://mozilla.org/path?originalQuery#newref"_ns
);
83 // Check that calling Finalize twice will fail.
84 NS_MutateURI
mutator(newURL
);
85 rv
= mutator
.SetQuery(""_ns
).Finalize(uri2
);
87 ASSERT_EQ(uri2
->GetSpec(out
), NS_OK
);
88 ASSERT_TRUE(out
== "https://mozilla.org/path#newref"_ns
);
89 nsCOMPtr
<nsIURI
> uri3
;
90 rv
= mutator
.Finalize(uri3
);
91 ASSERT_EQ(rv
, NS_ERROR_NOT_AVAILABLE
);
92 ASSERT_TRUE(uri3
== nullptr);
94 // Make sure changing scheme updates the default port
95 rv
= NS_NewURI(getter_AddRefs(uri
),
96 "https://example.org:80/path?query#ref"_ns
);
98 rv
= NS_MutateURI(uri
).SetScheme("http"_ns
).Finalize(uri
);
100 rv
= uri
->GetSpec(out
);
101 ASSERT_EQ(rv
, NS_OK
);
102 ASSERT_EQ(out
, "http://example.org/path?query#ref"_ns
);
104 rv
= uri
->GetPort(&port
);
105 ASSERT_EQ(rv
, NS_OK
);
107 rv
= uri
->GetFilePath(out
);
108 ASSERT_EQ(rv
, NS_OK
);
109 ASSERT_EQ(out
, "/path"_ns
);
110 rv
= uri
->GetQuery(out
);
111 ASSERT_EQ(rv
, NS_OK
);
112 ASSERT_EQ(out
, "query"_ns
);
113 rv
= uri
->GetRef(out
);
114 ASSERT_EQ(rv
, NS_OK
);
115 ASSERT_EQ(out
, "ref"_ns
);
117 // Make sure changing scheme does not change non-default port
118 rv
= NS_NewURI(getter_AddRefs(uri
), "https://example.org:123"_ns
);
119 ASSERT_EQ(rv
, NS_OK
);
120 rv
= NS_MutateURI(uri
).SetScheme("http"_ns
).Finalize(uri
);
121 ASSERT_EQ(rv
, NS_OK
);
122 rv
= uri
->GetSpec(out
);
123 ASSERT_EQ(rv
, NS_OK
);
124 ASSERT_EQ(out
, "http://example.org:123/"_ns
);
125 rv
= uri
->GetPort(&port
);
126 ASSERT_EQ(rv
, NS_OK
);
127 ASSERT_EQ(port
, 123);
130 extern MOZ_THREAD_LOCAL(uint32_t) gTlsURLRecursionCount
;
132 TEST(TestURIMutator
, OnAnyThread
)
134 nsCOMPtr
<nsIThreadPool
> pool
= new nsThreadPool();
135 pool
->SetThreadLimit(60);
137 pool
= new nsThreadPool();
138 for (int i
= 0; i
< 1000; ++i
) {
139 nsCOMPtr
<nsIRunnable
> task
=
140 NS_NewRunnableFunction("gtest-OnAnyThread", []() {
141 nsCOMPtr
<nsIURI
> uri
;
142 nsresult rv
= NS_NewURI(getter_AddRefs(uri
), "http://example.com"_ns
);
143 ASSERT_EQ(rv
, NS_OK
);
145 ASSERT_EQ(uri
->GetSpec(out
), NS_OK
);
146 ASSERT_TRUE(out
== "http://example.com/"_ns
);
150 pool
->Dispatch(task
, NS_DISPATCH_NORMAL
);
153 nsCOMPtr
<nsIURI
> uri
;
154 nsresult rv
= NS_NewURI(getter_AddRefs(uri
), "http://example.com"_ns
);
155 ASSERT_EQ(rv
, NS_OK
);
157 ASSERT_EQ(uri
->GetSpec(out
), NS_OK
);
158 ASSERT_TRUE(out
== "http://example.com/"_ns
);
162 ASSERT_EQ(gTlsURLRecursionCount
.get(), 0u);