1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/content/base/test/TestGetURL.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,85 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +#include "TestHarness.h" 1.9 + 1.10 +#include "nsIPrincipal.h" 1.11 +#include "nsIScriptSecurityManager.h" 1.12 +#include "nsIXMLHttpRequest.h" 1.13 + 1.14 + 1.15 +#define TEST_ENSURE_BASE(_test, _msg) \ 1.16 + PR_BEGIN_MACRO \ 1.17 + if (_test) { \ 1.18 + fail(_msg); \ 1.19 + return NS_ERROR_FAILURE; \ 1.20 + } \ 1.21 + PR_END_MACRO 1.22 + 1.23 +#define TEST_ENSURE_SUCCESS(_rv, _msg) \ 1.24 + TEST_ENSURE_BASE(NS_FAILED(_rv), _msg) 1.25 + 1.26 +#define TEST_ENSURE_FAILED(_rv, _msg) \ 1.27 + TEST_ENSURE_BASE(NS_SUCCEEDED(_rv), _msg) 1.28 + 1.29 +nsresult TestGetURL(const nsCString& aURL) 1.30 +{ 1.31 + nsresult rv; 1.32 + 1.33 + nsCOMPtr<nsIXMLHttpRequest> xhr = 1.34 + do_CreateInstance(NS_XMLHTTPREQUEST_CONTRACTID, &rv); 1.35 + TEST_ENSURE_SUCCESS(rv, "Couldn't create nsIXMLHttpRequest instance!"); 1.36 + 1.37 + NS_NAMED_LITERAL_CSTRING(getString, "GET"); 1.38 + const nsAString& empty = EmptyString(); 1.39 + 1.40 + nsCOMPtr<nsIScriptSecurityManager> secman = 1.41 + do_GetService(NS_SCRIPTSECURITYMANAGER_CONTRACTID, &rv); 1.42 + TEST_ENSURE_SUCCESS(rv, "Couldn't get script security manager!"); 1.43 + 1.44 + nsCOMPtr<nsIPrincipal> systemPrincipal; 1.45 + rv = secman->GetSystemPrincipal(getter_AddRefs(systemPrincipal)); 1.46 + TEST_ENSURE_SUCCESS(rv, "Couldn't get system principal!"); 1.47 + 1.48 + rv = xhr->Init(systemPrincipal, nullptr, nullptr, nullptr); 1.49 + TEST_ENSURE_SUCCESS(rv, "Couldn't initialize the XHR!"); 1.50 + 1.51 + rv = xhr->Open(getString, aURL, false, empty, empty); 1.52 + TEST_ENSURE_SUCCESS(rv, "OpenRequest failed!"); 1.53 + 1.54 + rv = xhr->Send(nullptr); 1.55 + TEST_ENSURE_SUCCESS(rv, "Send failed!"); 1.56 + 1.57 + nsAutoString response; 1.58 + rv = xhr->GetResponseText(response); 1.59 + TEST_ENSURE_SUCCESS(rv, "GetResponse failed!"); 1.60 + 1.61 + nsAutoCString responseUTF8 = NS_ConvertUTF16toUTF8(response); 1.62 + printf("#BEGIN\n"); 1.63 + printf("%s", responseUTF8.get()); 1.64 + printf("\n#EOF\n"); 1.65 + 1.66 + return NS_OK; 1.67 +} 1.68 + 1.69 +int main(int argc, char** argv) 1.70 +{ 1.71 + if (argc < 2) { 1.72 + printf("Usage: TestGetURL <url>\n"); 1.73 + exit(0); 1.74 + } 1.75 + 1.76 + ScopedXPCOM xpcom("XMLHttpRequest"); 1.77 + if (xpcom.failed()) 1.78 + return 1; 1.79 + 1.80 + nsAutoCString targetURL(argv[1]); 1.81 + 1.82 + int retval = 0; 1.83 + if (NS_FAILED(TestGetURL(targetURL))) { 1.84 + retval = 1; 1.85 + } 1.86 + 1.87 + return retval; 1.88 +}