1 #include "gtest/gtest.h"
2 #include "nsCOMPtr.h"
3 #include "nsNetCID.h"
4 #include "nsIURL.h"
5 #include "nsIURIMutator.h"
6
TEST(TestURIMutator,Mutator)7 TEST(TestURIMutator, Mutator) {
8 nsAutoCString out;
9
10 // This test instantiates a new nsStandardURL::Mutator (via contractID)
11 // and uses it to create a new URI.
12 nsCOMPtr<nsIURI> uri;
13 nsresult rv = NS_MutateURI(NS_STANDARDURLMUTATOR_CONTRACTID)
14 .SetSpec(NS_LITERAL_CSTRING("http://example.com"))
15 .Finalize(uri);
16 ASSERT_EQ(rv, NS_OK);
17 ASSERT_EQ(uri->GetSpec(out), NS_OK);
18 ASSERT_TRUE(out == NS_LITERAL_CSTRING("http://example.com/"));
19
20 // This test verifies that we can use NS_MutateURI to change a URI
21 rv = NS_MutateURI(uri)
22 .SetScheme(NS_LITERAL_CSTRING("ftp"))
23 .SetHost(NS_LITERAL_CSTRING("mozilla.org"))
24 .SetPathQueryRef(NS_LITERAL_CSTRING("/path?query#ref"))
25 .Finalize(uri);
26 ASSERT_EQ(rv, NS_OK);
27 ASSERT_EQ(uri->GetSpec(out), NS_OK);
28 ASSERT_TRUE(out == NS_LITERAL_CSTRING("ftp://mozilla.org/path?query#ref"));
29
30 // This test verifies that we can pass nsIURL to Finalize, and
31 nsCOMPtr<nsIURL> url;
32 rv = NS_MutateURI(uri).SetScheme(NS_LITERAL_CSTRING("https")).Finalize(url);
33 ASSERT_EQ(rv, NS_OK);
34 ASSERT_EQ(url->GetSpec(out), NS_OK);
35 ASSERT_TRUE(out == NS_LITERAL_CSTRING("https://mozilla.org/path?query#ref"));
36
37 // This test verifies that we can pass nsIURL** to Finalize.
38 // We need to use the explicit template because it's actually passing
39 // getter_AddRefs
40 nsCOMPtr<nsIURL> url2;
41 rv = NS_MutateURI(url)
42 .SetRef(NS_LITERAL_CSTRING("newref"))
43 .Finalize<nsIURL>(getter_AddRefs(url2));
44 ASSERT_EQ(rv, NS_OK);
45 ASSERT_EQ(url2->GetSpec(out), NS_OK);
46 ASSERT_TRUE(out ==
47 NS_LITERAL_CSTRING("https://mozilla.org/path?query#newref"));
48
49 // This test verifies that we can pass nsIURI** to Finalize.
50 // No need to be explicit.
51 auto functionSetRef = [](nsIURI* aURI, nsIURI** aResult) -> nsresult {
52 return NS_MutateURI(aURI)
53 .SetRef(NS_LITERAL_CSTRING("originalRef"))
54 .Finalize(aResult);
55 };
56
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 ==
62 NS_LITERAL_CSTRING("https://mozilla.org/path?query#originalRef"));
63
64 // This test verifies that we can pass nsIURI** to Finalize.
65 // We need to use the explicit template because it's actually passing
66 // getter_AddRefs
67 nsCOMPtr<nsIURI> uri2;
68 rv = NS_MutateURI(url2)
69 .SetQuery(NS_LITERAL_CSTRING("newquery"))
70 .Finalize<nsIURI>(getter_AddRefs(uri2));
71 ASSERT_EQ(rv, NS_OK);
72 ASSERT_EQ(uri2->GetSpec(out), NS_OK);
73 ASSERT_TRUE(out ==
74 NS_LITERAL_CSTRING("https://mozilla.org/path?newquery#newref"));
75
76 // This test verifies that we can pass nsIURI** to Finalize.
77 // No need to be explicit.
78 auto functionSetQuery = [](nsIURI* aURI, nsIURL** aResult) -> nsresult {
79 return NS_MutateURI(aURI)
80 .SetQuery(NS_LITERAL_CSTRING("originalQuery"))
81 .Finalize(aResult);
82 };
83
84 nsCOMPtr<nsIURL> newURL;
85 rv = functionSetQuery(uri2, getter_AddRefs(newURL));
86 ASSERT_EQ(rv, NS_OK);
87 ASSERT_EQ(newURL->GetSpec(out), NS_OK);
88 ASSERT_TRUE(out == NS_LITERAL_CSTRING(
89 "https://mozilla.org/path?originalQuery#newref"));
90
91 // Check that calling Finalize twice will fail.
92 NS_MutateURI mutator(newURL);
93 rv = mutator.SetQuery(EmptyCString()).Finalize(uri2);
94 ASSERT_EQ(rv, NS_OK);
95 ASSERT_EQ(uri2->GetSpec(out), NS_OK);
96 ASSERT_TRUE(out == NS_LITERAL_CSTRING("https://mozilla.org/path#newref"));
97 nsCOMPtr<nsIURI> uri3;
98 rv = mutator.Finalize(uri3);
99 ASSERT_EQ(rv, NS_ERROR_NOT_AVAILABLE);
100 ASSERT_TRUE(uri3 == nullptr);
101 }
102