|
1 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
2 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
4 |
|
5 #include "TestHarness.h" |
|
6 |
|
7 #include "nsIDOMDocument.h" |
|
8 #include "nsIPrincipal.h" |
|
9 #include "nsIScriptSecurityManager.h" |
|
10 #include "nsIXMLHttpRequest.h" |
|
11 |
|
12 |
|
13 #define TEST_ENSURE_BASE(_test, _msg) \ |
|
14 PR_BEGIN_MACRO \ |
|
15 if (_test) { \ |
|
16 fail(_msg); \ |
|
17 return NS_ERROR_FAILURE; \ |
|
18 } \ |
|
19 PR_END_MACRO |
|
20 |
|
21 #define TEST_ENSURE_SUCCESS(_rv, _msg) \ |
|
22 TEST_ENSURE_BASE(NS_FAILED(_rv), _msg) |
|
23 |
|
24 #define TEST_ENSURE_FAILED(_rv, _msg) \ |
|
25 TEST_ENSURE_BASE(NS_SUCCEEDED(_rv), _msg) |
|
26 |
|
27 #define TEST_URL_PREFIX \ |
|
28 "data:text/xml," |
|
29 #define TEST_URL_CONTENT \ |
|
30 "<foo><bar></bar></foo>" |
|
31 |
|
32 #define TEST_URL \ |
|
33 TEST_URL_PREFIX TEST_URL_CONTENT |
|
34 |
|
35 nsresult TestNativeXMLHttpRequest() |
|
36 { |
|
37 nsresult rv; |
|
38 |
|
39 nsCOMPtr<nsIXMLHttpRequest> xhr = |
|
40 do_CreateInstance(NS_XMLHTTPREQUEST_CONTRACTID, &rv); |
|
41 TEST_ENSURE_SUCCESS(rv, "Couldn't create nsIXMLHttpRequest instance!"); |
|
42 |
|
43 NS_NAMED_LITERAL_CSTRING(getString, "GET"); |
|
44 NS_NAMED_LITERAL_CSTRING(testURL, TEST_URL); |
|
45 const nsAString& empty = EmptyString(); |
|
46 |
|
47 nsCOMPtr<nsIScriptSecurityManager> secman = |
|
48 do_GetService(NS_SCRIPTSECURITYMANAGER_CONTRACTID, &rv); |
|
49 TEST_ENSURE_SUCCESS(rv, "Couldn't get script security manager!"); |
|
50 |
|
51 nsCOMPtr<nsIPrincipal> systemPrincipal; |
|
52 rv = secman->GetSystemPrincipal(getter_AddRefs(systemPrincipal)); |
|
53 TEST_ENSURE_SUCCESS(rv, "Couldn't get system principal!"); |
|
54 |
|
55 rv = xhr->Init(systemPrincipal, nullptr, nullptr, nullptr); |
|
56 TEST_ENSURE_SUCCESS(rv, "Couldn't initialize the XHR!"); |
|
57 |
|
58 rv = xhr->Open(getString, testURL, false, empty, empty); |
|
59 TEST_ENSURE_SUCCESS(rv, "Open failed!"); |
|
60 |
|
61 rv = xhr->Send(nullptr); |
|
62 TEST_ENSURE_SUCCESS(rv, "Send failed!"); |
|
63 |
|
64 nsAutoString response; |
|
65 rv = xhr->GetResponseText(response); |
|
66 TEST_ENSURE_SUCCESS(rv, "GetResponse failed!"); |
|
67 |
|
68 if (!response.EqualsLiteral(TEST_URL_CONTENT)) { |
|
69 fail("Response text does not match!"); |
|
70 return NS_ERROR_FAILURE; |
|
71 } |
|
72 |
|
73 nsCOMPtr<nsIDOMDocument> dom; |
|
74 rv = xhr->GetResponseXML(getter_AddRefs(dom)); |
|
75 TEST_ENSURE_SUCCESS(rv, "GetResponseXML failed!"); |
|
76 |
|
77 if (!dom) { |
|
78 fail("No DOM document constructed!"); |
|
79 return NS_ERROR_FAILURE; |
|
80 } |
|
81 |
|
82 passed("Native XMLHttpRequest"); |
|
83 return NS_OK; |
|
84 } |
|
85 |
|
86 int main(int argc, char** argv) |
|
87 { |
|
88 ScopedXPCOM xpcom("XMLHttpRequest"); |
|
89 if (xpcom.failed()) |
|
90 return 1; |
|
91 |
|
92 int retval = 0; |
|
93 if (NS_FAILED(TestNativeXMLHttpRequest())) { |
|
94 retval = 1; |
|
95 } |
|
96 |
|
97 return retval; |
|
98 } |