1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/content/base/test/TestNativeXMLHttpRequest.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,98 @@ 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 "nsIDOMDocument.h" 1.11 +#include "nsIPrincipal.h" 1.12 +#include "nsIScriptSecurityManager.h" 1.13 +#include "nsIXMLHttpRequest.h" 1.14 + 1.15 + 1.16 +#define TEST_ENSURE_BASE(_test, _msg) \ 1.17 + PR_BEGIN_MACRO \ 1.18 + if (_test) { \ 1.19 + fail(_msg); \ 1.20 + return NS_ERROR_FAILURE; \ 1.21 + } \ 1.22 + PR_END_MACRO 1.23 + 1.24 +#define TEST_ENSURE_SUCCESS(_rv, _msg) \ 1.25 + TEST_ENSURE_BASE(NS_FAILED(_rv), _msg) 1.26 + 1.27 +#define TEST_ENSURE_FAILED(_rv, _msg) \ 1.28 + TEST_ENSURE_BASE(NS_SUCCEEDED(_rv), _msg) 1.29 + 1.30 +#define TEST_URL_PREFIX \ 1.31 + "data:text/xml," 1.32 +#define TEST_URL_CONTENT \ 1.33 + "<foo><bar></bar></foo>" 1.34 + 1.35 +#define TEST_URL \ 1.36 + TEST_URL_PREFIX TEST_URL_CONTENT 1.37 + 1.38 +nsresult TestNativeXMLHttpRequest() 1.39 +{ 1.40 + nsresult rv; 1.41 + 1.42 + nsCOMPtr<nsIXMLHttpRequest> xhr = 1.43 + do_CreateInstance(NS_XMLHTTPREQUEST_CONTRACTID, &rv); 1.44 + TEST_ENSURE_SUCCESS(rv, "Couldn't create nsIXMLHttpRequest instance!"); 1.45 + 1.46 + NS_NAMED_LITERAL_CSTRING(getString, "GET"); 1.47 + NS_NAMED_LITERAL_CSTRING(testURL, TEST_URL); 1.48 + const nsAString& empty = EmptyString(); 1.49 + 1.50 + nsCOMPtr<nsIScriptSecurityManager> secman = 1.51 + do_GetService(NS_SCRIPTSECURITYMANAGER_CONTRACTID, &rv); 1.52 + TEST_ENSURE_SUCCESS(rv, "Couldn't get script security manager!"); 1.53 + 1.54 + nsCOMPtr<nsIPrincipal> systemPrincipal; 1.55 + rv = secman->GetSystemPrincipal(getter_AddRefs(systemPrincipal)); 1.56 + TEST_ENSURE_SUCCESS(rv, "Couldn't get system principal!"); 1.57 + 1.58 + rv = xhr->Init(systemPrincipal, nullptr, nullptr, nullptr); 1.59 + TEST_ENSURE_SUCCESS(rv, "Couldn't initialize the XHR!"); 1.60 + 1.61 + rv = xhr->Open(getString, testURL, false, empty, empty); 1.62 + TEST_ENSURE_SUCCESS(rv, "Open failed!"); 1.63 + 1.64 + rv = xhr->Send(nullptr); 1.65 + TEST_ENSURE_SUCCESS(rv, "Send failed!"); 1.66 + 1.67 + nsAutoString response; 1.68 + rv = xhr->GetResponseText(response); 1.69 + TEST_ENSURE_SUCCESS(rv, "GetResponse failed!"); 1.70 + 1.71 + if (!response.EqualsLiteral(TEST_URL_CONTENT)) { 1.72 + fail("Response text does not match!"); 1.73 + return NS_ERROR_FAILURE; 1.74 + } 1.75 + 1.76 + nsCOMPtr<nsIDOMDocument> dom; 1.77 + rv = xhr->GetResponseXML(getter_AddRefs(dom)); 1.78 + TEST_ENSURE_SUCCESS(rv, "GetResponseXML failed!"); 1.79 + 1.80 + if (!dom) { 1.81 + fail("No DOM document constructed!"); 1.82 + return NS_ERROR_FAILURE; 1.83 + } 1.84 + 1.85 + passed("Native XMLHttpRequest"); 1.86 + return NS_OK; 1.87 +} 1.88 + 1.89 +int main(int argc, char** argv) 1.90 +{ 1.91 + ScopedXPCOM xpcom("XMLHttpRequest"); 1.92 + if (xpcom.failed()) 1.93 + return 1; 1.94 + 1.95 + int retval = 0; 1.96 + if (NS_FAILED(TestNativeXMLHttpRequest())) { 1.97 + retval = 1; 1.98 + } 1.99 + 1.100 + return retval; 1.101 +}