3 <script src=
"inspector-test.js"></script>
4 <script type=
"text/javascript">
5 function sendXHR(url
, forcePreflight
, async
, callback
)
7 var xhr
= new XMLHttpRequest();
9 xhr
.onreadystatechange = function()
11 if (xhr
.readyState
=== XMLHttpRequest
.DONE
) {
12 if (typeof(callback
) === "function")
17 xhr
.open("POST", url
, async
);
18 xhr
.setRequestHeader("Content-Type", forcePreflight
? "application/xml" : "text/plain");
20 xhr
.send("<xml></xml>"); // Denied requests will cause exceptions.
25 var baseTargetURL
= "http://localhost:8000/inspector/resources/cors-target.php";
27 function targetURL(id
, deny
, addTimestamp
)
29 var result
= baseTargetURL
+ "?id=" + id
;
31 result
+= "&deny=yes";
33 result
+= "&date=" + Date
.now();
37 function doCrossOriginXHR()
39 // Failed POSTs with no preflight check should result in a POST request being logged
40 sendXHR(targetURL(0, true, false), false, false);
41 // Failed POSTs with preflight check should result in an OPTIONS request being logged
42 sendXHR(targetURL(1, true, false), true, false);
43 // Successful POSTs with preflight check should result in an OPTIONS request followed by POST request being logged
44 // Generate request name based on timestamp to defeat OPTIONS request caching (this is only relevant for repeated invocations of the test in signle instance of DRT)
45 sendXHR(targetURL(2, false, true), true, false);
47 // And now send the same requests asynchronously
48 // Add redundant async parameter to ensure this request differs from the one above.
49 sendXHR(targetURL(3, true, false), false, true, step2
);
54 sendXHR(targetURL(4, true, false), true, true, step3
);
59 sendXHR(targetURL(5, false, true), true, true);
64 var requestMessages
= [];
65 var postRequestsCount
= 0;
66 function onRequest(event
)
68 var request
= event
.data
;
69 var idMatch
= /\?id=([0-9]*)&/.exec(request
.url
);
70 var requestId
= idMatch
[1];
71 var requestMessage
= requestId
+ " " + request
.resourceType() + ":" + request
.requestMethod
+ " " + request
.url
.replace(/[&?]date=\d+/, "");
72 requestMessages
.push(requestMessage
);
73 if (request
.requestMethod
=== "POST" && ++postRequestsCount
=== 4) {
74 requestMessages
.sort();
75 for (var i
= 0; i
< requestMessages
.length
; i
++)
76 InspectorTest
.addResult(requestMessages
[i
]);
77 InspectorTest
.completeTest();
80 InspectorTest
.networkManager
.addEventListener(WebInspector
.NetworkManager
.EventTypes
.RequestFinished
, onRequest
);
81 InspectorTest
.evaluateInPage("doCrossOriginXHR();");
85 <body onload=
"runTest()">
86 <p>Tests that preflight OPTIONS requests appear in Network resources
</p>
87 <a href=
"https://bugs.webkit.org/show_bug.cgi?id=63712">Bug
63712</a>