|
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 "nsIPrincipal.h" |
|
8 #include "nsIScriptSecurityManager.h" |
|
9 #include "nsIXMLHttpRequest.h" |
|
10 |
|
11 |
|
12 #define TEST_ENSURE_BASE(_test, _msg) \ |
|
13 PR_BEGIN_MACRO \ |
|
14 if (_test) { \ |
|
15 fail(_msg); \ |
|
16 return NS_ERROR_FAILURE; \ |
|
17 } \ |
|
18 PR_END_MACRO |
|
19 |
|
20 #define TEST_ENSURE_SUCCESS(_rv, _msg) \ |
|
21 TEST_ENSURE_BASE(NS_FAILED(_rv), _msg) |
|
22 |
|
23 #define TEST_ENSURE_FAILED(_rv, _msg) \ |
|
24 TEST_ENSURE_BASE(NS_SUCCEEDED(_rv), _msg) |
|
25 |
|
26 nsresult TestGetURL(const nsCString& aURL) |
|
27 { |
|
28 nsresult rv; |
|
29 |
|
30 nsCOMPtr<nsIXMLHttpRequest> xhr = |
|
31 do_CreateInstance(NS_XMLHTTPREQUEST_CONTRACTID, &rv); |
|
32 TEST_ENSURE_SUCCESS(rv, "Couldn't create nsIXMLHttpRequest instance!"); |
|
33 |
|
34 NS_NAMED_LITERAL_CSTRING(getString, "GET"); |
|
35 const nsAString& empty = EmptyString(); |
|
36 |
|
37 nsCOMPtr<nsIScriptSecurityManager> secman = |
|
38 do_GetService(NS_SCRIPTSECURITYMANAGER_CONTRACTID, &rv); |
|
39 TEST_ENSURE_SUCCESS(rv, "Couldn't get script security manager!"); |
|
40 |
|
41 nsCOMPtr<nsIPrincipal> systemPrincipal; |
|
42 rv = secman->GetSystemPrincipal(getter_AddRefs(systemPrincipal)); |
|
43 TEST_ENSURE_SUCCESS(rv, "Couldn't get system principal!"); |
|
44 |
|
45 rv = xhr->Init(systemPrincipal, nullptr, nullptr, nullptr); |
|
46 TEST_ENSURE_SUCCESS(rv, "Couldn't initialize the XHR!"); |
|
47 |
|
48 rv = xhr->Open(getString, aURL, false, empty, empty); |
|
49 TEST_ENSURE_SUCCESS(rv, "OpenRequest failed!"); |
|
50 |
|
51 rv = xhr->Send(nullptr); |
|
52 TEST_ENSURE_SUCCESS(rv, "Send failed!"); |
|
53 |
|
54 nsAutoString response; |
|
55 rv = xhr->GetResponseText(response); |
|
56 TEST_ENSURE_SUCCESS(rv, "GetResponse failed!"); |
|
57 |
|
58 nsAutoCString responseUTF8 = NS_ConvertUTF16toUTF8(response); |
|
59 printf("#BEGIN\n"); |
|
60 printf("%s", responseUTF8.get()); |
|
61 printf("\n#EOF\n"); |
|
62 |
|
63 return NS_OK; |
|
64 } |
|
65 |
|
66 int main(int argc, char** argv) |
|
67 { |
|
68 if (argc < 2) { |
|
69 printf("Usage: TestGetURL <url>\n"); |
|
70 exit(0); |
|
71 } |
|
72 |
|
73 ScopedXPCOM xpcom("XMLHttpRequest"); |
|
74 if (xpcom.failed()) |
|
75 return 1; |
|
76 |
|
77 nsAutoCString targetURL(argv[1]); |
|
78 |
|
79 int retval = 0; |
|
80 if (NS_FAILED(TestGetURL(targetURL))) { |
|
81 retval = 1; |
|
82 } |
|
83 |
|
84 return retval; |
|
85 } |