Explorar o código

Add cross-origin resource sharing (CORS) test

bel %!s(int64=11) %!d(string=hai) anos
pai
achega
c498dde851
Modificáronse 2 ficheiros con 73 adicións e 0 borrados
  1. 66 0
      test/cors.html
  2. 7 0
      test/cors.reply.html

+ 66 - 0
test/cors.html

@@ -0,0 +1,66 @@
+<!DOCTYPE html>
+<html>
+<head>
+<title>CORS test</title>
+<style>
+ html,body{font:normal 1em arial,helvetica;}
+</style>
+
+<script> // http://www.html5rocks.com/en/tutorials/cors/
+
+// Create the XHR object.
+function createCORSRequest(method, url) {
+  var xhr = new XMLHttpRequest();
+  if ("withCredentials" in xhr) {
+    // XHR for Chrome/Firefox/Opera/Safari.
+    xhr.open(method, url, true);
+  } else if (typeof XDomainRequest != "undefined") {
+    // XDomainRequest for IE.
+    xhr = new XDomainRequest();
+    xhr.open(method, url);
+  } else {
+    // CORS not supported.
+    xhr = null;
+  }
+  return xhr;
+}
+
+// Helper method to parse the title tag from the response.
+function getTitle(text) {
+  return text.match('<title>(.*)?</title>')[1];
+}
+
+// Make the actual CORS request.
+function makeCorsRequest() {
+  var url = "http://localhost/cors.reply.html"; 
+  var xhr = createCORSRequest('GET', url);
+  if (!xhr) {
+    alert('CORS not supported');
+    return;
+  }
+
+  // Response handlers.
+  xhr.onload = function() {
+    var text = xhr.responseText;
+    var title = getTitle(text);
+    alert('Response from CORS request to ' + url + ': ' + title);
+  };
+
+  xhr.onerror = function() {
+    alert('Woops, there was an error making the request.');
+  };
+
+  xhr.send();
+}
+
+function start() {}
+
+</script>
+
+</head>
+<body onload="start()">
+ <h1>Cross-origin resource sharing test</h1>
+ <button onclick="makeCorsRequest()">Run CORS request</button>
+ <p>More information on CORS: See <a href="http://enable-cors.org/">enable-cors.org</a> and <a href="http://www.html5rocks.com/en/tutorials/cors/">html5rocks.com</a>.</p>
+</body>
+</html>

+ 7 - 0
test/cors.reply.html

@@ -0,0 +1,7 @@
+<!DOCTYPE html>
+<html>
+<head><title>CORS test reply - test OK</title></head>
+<body>
+Do not load this page directly - use cors.html instead!
+</body>
+</html>