|
| 1 | +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
| 2 | +/* vim: set ts=8 sts=2 et sw=2 tw=80: */ |
| 3 | +/* This Source Code Form is subject to the terms of the Mozilla Public |
| 4 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 5 | + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 6 | + |
| 7 | +#include "TestHarness.h" |
| 8 | + |
| 9 | +#include "nsIDOMDocument.h" |
| 10 | +#include "nsIPrincipal.h" |
| 11 | +#include "nsIScriptSecurityManager.h" |
| 12 | +#include "nsIXMLHttpRequest.h" |
| 13 | + |
| 14 | + |
| 15 | +#define TEST_ENSURE_BASE(_test, _msg) \ |
| 16 | + PR_BEGIN_MACRO \ |
| 17 | + if (_test) { \ |
| 18 | + fail(_msg); \ |
| 19 | + return NS_ERROR_FAILURE; \ |
| 20 | + } \ |
| 21 | + PR_END_MACRO |
| 22 | + |
| 23 | +#define TEST_ENSURE_SUCCESS(_rv, _msg) \ |
| 24 | + TEST_ENSURE_BASE(NS_FAILED(_rv), _msg) |
| 25 | + |
| 26 | +#define TEST_ENSURE_FAILED(_rv, _msg) \ |
| 27 | + TEST_ENSURE_BASE(NS_SUCCEEDED(_rv), _msg) |
| 28 | + |
| 29 | +#define TEST_URL_PREFIX \ |
| 30 | + "data:text/xml," |
| 31 | +#define TEST_URL_CONTENT \ |
| 32 | + "<foo><bar></bar></foo>" |
| 33 | + |
| 34 | +#define TEST_URL \ |
| 35 | + TEST_URL_PREFIX TEST_URL_CONTENT |
| 36 | + |
| 37 | +nsresult TestNativeXMLHttpRequest() |
| 38 | +{ |
| 39 | + nsresult rv; |
| 40 | + |
| 41 | + nsCOMPtr<nsIXMLHttpRequest> xhr = |
| 42 | + do_CreateInstance(NS_XMLHTTPREQUEST_CONTRACTID, &rv); |
| 43 | + TEST_ENSURE_SUCCESS(rv, "Couldn't create nsIXMLHttpRequest instance!"); |
| 44 | + |
| 45 | + NS_NAMED_LITERAL_CSTRING(getString, "GET"); |
| 46 | + NS_NAMED_LITERAL_CSTRING(testURL, TEST_URL); |
| 47 | + const nsAString& empty = EmptyString(); |
| 48 | + |
| 49 | + nsCOMPtr<nsIScriptSecurityManager> secman = |
| 50 | + do_GetService(NS_SCRIPTSECURITYMANAGER_CONTRACTID, &rv); |
| 51 | + TEST_ENSURE_SUCCESS(rv, "Couldn't get script security manager!"); |
| 52 | + |
| 53 | + nsCOMPtr<nsIPrincipal> systemPrincipal; |
| 54 | + rv = secman->GetSystemPrincipal(getter_AddRefs(systemPrincipal)); |
| 55 | + TEST_ENSURE_SUCCESS(rv, "Couldn't get system principal!"); |
| 56 | + |
| 57 | + rv = xhr->Init(systemPrincipal, nullptr, nullptr, nullptr); |
| 58 | + TEST_ENSURE_SUCCESS(rv, "Couldn't initialize the XHR!"); |
| 59 | + |
| 60 | + rv = xhr->Open(getString, testURL, false, empty, empty); |
| 61 | + TEST_ENSURE_SUCCESS(rv, "Open failed!"); |
| 62 | + |
| 63 | + rv = xhr->Send(nullptr); |
| 64 | + TEST_ENSURE_SUCCESS(rv, "Send failed!"); |
| 65 | + |
| 66 | + nsAutoString response; |
| 67 | + rv = xhr->GetResponseText(response); |
| 68 | + TEST_ENSURE_SUCCESS(rv, "GetResponse failed!"); |
| 69 | + |
| 70 | + if (!response.EqualsLiteral(TEST_URL_CONTENT)) { |
| 71 | + fail("Response text does not match!"); |
| 72 | + return NS_ERROR_FAILURE; |
| 73 | + } |
| 74 | + |
| 75 | + nsCOMPtr<nsIDOMDocument> dom; |
| 76 | + rv = xhr->GetResponseXML(getter_AddRefs(dom)); |
| 77 | + TEST_ENSURE_SUCCESS(rv, "GetResponseXML failed!"); |
| 78 | + |
| 79 | + if (!dom) { |
| 80 | + fail("No DOM document constructed!"); |
| 81 | + return NS_ERROR_FAILURE; |
| 82 | + } |
| 83 | + |
| 84 | + passed("Native XMLHttpRequest"); |
| 85 | + return NS_OK; |
| 86 | +} |
| 87 | + |
| 88 | +int main(int argc, char** argv) |
| 89 | +{ |
| 90 | + ScopedXPCOM xpcom("XMLHttpRequest"); |
| 91 | + if (xpcom.failed()) |
| 92 | + return 1; |
| 93 | + |
| 94 | + int retval = 0; |
| 95 | + if (NS_FAILED(TestNativeXMLHttpRequest())) { |
| 96 | + retval = 1; |
| 97 | + } |
| 98 | + |
| 99 | + return retval; |
| 100 | +} |
0 commit comments