1 : /* ***** BEGIN LICENSE BLOCK *****
2 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3 : *
4 : * The contents of this file are subject to the Mozilla Public License Version
5 : * 1.1 (the "License"); you may not use this file except in compliance with
6 : * the License. You may obtain a copy of the License at
7 : * http://www.mozilla.org/MPL/
8 : *
9 : * Software distributed under the License is distributed on an "AS IS" basis,
10 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 : * for the specific language governing rights and limitations under the
12 : * License.
13 : *
14 : * The Original Code is Mozilla XMLHttpRequest Tests.
15 : *
16 : * The Initial Developer of the Original Code is
17 : * Adam Barth
18 : * Portions created by the Initial Developer are Copyright (C) 2009
19 : * the Initial Developer. All Rights Reserved.
20 : *
21 : * Alternatively, the contents of this file may be used under the terms of
22 : * either the GNU General Public License Version 2 or later (the "GPL"), or
23 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
24 : * in which case the provisions of the GPL or the LGPL are applicable instead
25 : * of those above. If you wish to allow use of your version of this file only
26 : * under the terms of either the GPL or the LGPL, and not to allow others to
27 : * use your version of this file under the terms of the MPL, indicate your
28 : * decision by deleting the provisions above and replace them with the notice
29 : * and other provisions required by the GPL or the LGPL. If you do not delete
30 : * the provisions above, a recipient may use your version of this file under
31 : * the terms of any one of the MPL, the GPL or the LGPL.
32 : *
33 : * ***** END LICENSE BLOCK ***** */
34 :
35 : #include "TestHarness.h"
36 :
37 : #include "nsIDOMDocument.h"
38 : #include "nsIPrincipal.h"
39 : #include "nsIScriptSecurityManager.h"
40 : #include "nsIXMLHttpRequest.h"
41 :
42 :
43 : #define TEST_ENSURE_BASE(_test, _msg) \
44 : PR_BEGIN_MACRO \
45 : if (_test) { \
46 : fail(_msg); \
47 : return NS_ERROR_FAILURE; \
48 : } \
49 : PR_END_MACRO
50 :
51 : #define TEST_ENSURE_SUCCESS(_rv, _msg) \
52 : TEST_ENSURE_BASE(NS_FAILED(_rv), _msg)
53 :
54 : #define TEST_ENSURE_FAILED(_rv, _msg) \
55 : TEST_ENSURE_BASE(NS_SUCCEEDED(_rv), _msg)
56 :
57 0 : nsresult TestGetURL(const nsCString& aURL)
58 : {
59 : nsresult rv;
60 :
61 : nsCOMPtr<nsIXMLHttpRequest> xhr =
62 0 : do_CreateInstance(NS_XMLHTTPREQUEST_CONTRACTID, &rv);
63 0 : TEST_ENSURE_SUCCESS(rv, "Couldn't create nsIXMLHttpRequest instance!");
64 :
65 0 : NS_NAMED_LITERAL_CSTRING(getString, "GET");
66 0 : const nsAString& empty = EmptyString();
67 :
68 : nsCOMPtr<nsIScriptSecurityManager> secman =
69 0 : do_GetService(NS_SCRIPTSECURITYMANAGER_CONTRACTID, &rv);
70 0 : TEST_ENSURE_SUCCESS(rv, "Couldn't get script security manager!");
71 :
72 0 : nsCOMPtr<nsIPrincipal> systemPrincipal;
73 0 : rv = secman->GetSystemPrincipal(getter_AddRefs(systemPrincipal));
74 0 : TEST_ENSURE_SUCCESS(rv, "Couldn't get system principal!");
75 :
76 0 : rv = xhr->Init(systemPrincipal, nsnull, nsnull, nsnull);
77 0 : TEST_ENSURE_SUCCESS(rv, "Couldn't initialize the XHR!");
78 :
79 0 : rv = xhr->Open(getString, aURL, false, empty, empty);
80 0 : TEST_ENSURE_SUCCESS(rv, "OpenRequest failed!");
81 :
82 0 : rv = xhr->Send(nsnull);
83 0 : TEST_ENSURE_SUCCESS(rv, "Send failed!");
84 :
85 0 : nsAutoString response;
86 0 : rv = xhr->GetResponseText(response);
87 0 : TEST_ENSURE_SUCCESS(rv, "GetResponse failed!");
88 :
89 0 : nsCAutoString responseUTF8 = NS_ConvertUTF16toUTF8(response);
90 0 : printf("#BEGIN\n");
91 0 : printf("%s", responseUTF8.get());
92 0 : printf("\n#EOF\n");
93 :
94 0 : return NS_OK;
95 : }
96 :
97 1 : int main(int argc, char** argv)
98 : {
99 1 : if (argc < 2) {
100 1 : printf("Usage: TestGetURL <url>\n");
101 1 : exit(0);
102 : }
103 :
104 0 : ScopedXPCOM xpcom("XMLHttpRequest");
105 0 : if (xpcom.failed())
106 0 : return 1;
107 :
108 0 : nsCAutoString targetURL(argv[1]);
109 :
110 0 : int retval = 0;
111 0 : if (NS_FAILED(TestGetURL(targetURL))) {
112 0 : retval = 1;
113 : }
114 :
115 0 : return retval;
116 : }
|