content/base/test/TestGetURL.cpp

Thu, 15 Jan 2015 21:03:48 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 15 Jan 2015 21:03:48 +0100
branch
TOR_BUG_9701
changeset 11
deefc01c0e14
permissions
-rw-r--r--

Integrate friendly tips from Tor colleagues to make (or not) 4.5 alpha 3;
This includes removal of overloaded (but unused) methods, and addition of
a overlooked call to DataStruct::SetData(nsISupports, uint32_t, bool.)

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

mercurial