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