Bug 1931425 - Limit how often moz-label's #setStyles runs r=reusable-components-revie...
[gecko.git] / netwerk / test / gtest / TestURIMutator.cpp
blob255ed640eb31e85f09ff430ac93c8f4639804200
1 #include "gtest/gtest.h"
2 #include "nsCOMPtr.h"
3 #include "nsNetCID.h"
4 #include "nsIURIMutator.h"
5 #include "nsIURL.h"
6 #include "nsThreadPool.h"
7 #include "nsNetUtil.h"
9 TEST(TestURIMutator, Mutator)
11 nsAutoCString out;
13 // This test instantiates a new nsStandardURL::Mutator (via contractID)
14 // and uses it to create a new URI.
15 nsCOMPtr<nsIURI> uri;
16 nsresult rv = NS_MutateURI(NS_STANDARDURLMUTATOR_CONTRACTID)
17 .SetSpec("http://example.com"_ns)
18 .Finalize(uri);
19 ASSERT_EQ(rv, NS_OK);
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)
25 .SetScheme("ftp"_ns)
26 .SetHost("mozilla.org"_ns)
27 .SetPathQueryRef("/path?query#ref"_ns)
28 .Finalize(uri);
29 ASSERT_EQ(rv, NS_OK);
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
34 nsCOMPtr<nsIURL> url;
35 rv = NS_MutateURI(uri).SetScheme("https"_ns).Finalize(url);
36 ASSERT_EQ(rv, NS_OK);
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
42 // getter_AddRefs
43 nsCOMPtr<nsIURL> url2;
44 rv = NS_MutateURI(url)
45 .SetRef("newref"_ns)
46 .Finalize<nsIURL>(getter_AddRefs(url2));
47 ASSERT_EQ(rv, NS_OK);
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));
59 ASSERT_EQ(rv, NS_OK);
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;
65 rv =
66 NS_MutateURI(url2).SetQuery("newquery"_ns).Finalize(getter_AddRefs(uri2));
67 ASSERT_EQ(rv, NS_OK);
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));
79 ASSERT_EQ(rv, NS_OK);
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);
86 ASSERT_EQ(rv, NS_OK);
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);
97 ASSERT_EQ(rv, NS_OK);
98 rv = NS_MutateURI(uri).SetScheme("http"_ns).Finalize(uri);
99 ASSERT_EQ(rv, NS_OK);
100 rv = uri->GetSpec(out);
101 ASSERT_EQ(rv, NS_OK);
102 ASSERT_EQ(out, "http://example.org/path?query#ref"_ns);
103 int32_t port;
104 rv = uri->GetPort(&port);
105 ASSERT_EQ(rv, NS_OK);
106 ASSERT_EQ(port, -1);
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);
144 nsAutoCString out;
145 ASSERT_EQ(uri->GetSpec(out), NS_OK);
146 ASSERT_TRUE(out == "http://example.com/"_ns);
148 EXPECT_TRUE(task);
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);
156 nsAutoCString out;
157 ASSERT_EQ(uri->GetSpec(out), NS_OK);
158 ASSERT_TRUE(out == "http://example.com/"_ns);
160 pool->Shutdown();
162 ASSERT_EQ(gTlsURLRecursionCount.get(), 0u);