Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Initial commit.

  • Loading branch information...
commit b635a756cb4d2960711c65e266dee2c10e1535a7 0 parents
Conrad Irwin authored September 19, 2011
7  LICENSE.MIT
... ...
@@ -0,0 +1,7 @@
  1
+Copyright (c) 2011 Conrad Irwin <conrad@rapportive.com>
  2
+
  3
+Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
  4
+
  5
+The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
  6
+
  7
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
5  README.md
Source Rendered
... ...
@@ -0,0 +1,5 @@
  1
+This is the code powering [https://cors-test.appspot.com/](https://cors-test.appspot.com/) and [https://test-cors.appspot.com/](https://cors-test.appspot.com/).
  2
+
  3
+Feel free to use those sites, or to roll your own versions using this code.
  4
+
  5
+Pull requests and bug reports are most welcome.
13  cors-test/app.yaml
... ...
@@ -0,0 +1,13 @@
  1
+application: cors-test
  2
+version: 1
  3
+runtime: python
  4
+api_version: 1
  5
+
  6
+handlers:
  7
+
  8
+- url: /
  9
+  static_files: index.html
  10
+  upload: index.html
  11
+
  12
+- url: /test
  13
+  script: cors_test.py
32  cors-test/cors_test.py
... ...
@@ -0,0 +1,32 @@
  1
+from google.appengine.ext import webapp
  2
+from google.appengine.ext.webapp.util import run_wsgi_app
  3
+
  4
+class CORSEnabledHandler(webapp.RequestHandler):
  5
+    def get(self):
  6
+        add_header = self.response.headers.add_header
  7
+        if 'Origin' in self.request.headers:
  8
+            add_header("Access-Control-Allow-Origin", self.request.headers['Origin'])
  9
+        else:
  10
+            add_header("Access-Control-Allow-Origin", '*')
  11
+
  12
+        if 'Access-Control-Request-Headers' in self.request.headers:
  13
+            add_header('Access-Control-Allow-Headers', self.request.headers['Access-Control-Request-Headers'])
  14
+
  15
+        if 'Access-Control-Request-Method' in self.request.headers:
  16
+            add_header('Access-Control-Allow-Methods', self.request.headers['Access-Control-Request-Method'])
  17
+
  18
+        add_header('Access-Control-Max-Age', '0')
  19
+
  20
+        add_header("Access-Control-Allow-Credentials", "true")
  21
+
  22
+        add_header('Cache-Control', 'no-cache')
  23
+        add_header('Expires', 'Fri, 01 Jan 1990 00:00:00 GMT')
  24
+        self.response.headers['Content-Type'] = 'application/json'
  25
+        self.response.out.write('{"status":"ok"}')
  26
+
  27
+    post = put = head = options = delete = trace = get
  28
+
  29
+application = webapp.WSGIApplication([('/test', CORSEnabledHandler)], debug=True)
  30
+
  31
+if __name__ == "__main__":
  32
+    run_wsgi_app(application)
36  cors-test/index.html
... ...
@@ -0,0 +1,36 @@
  1
+<html>
  2
+    <head>
  3
+        <title>Cors testing app</title>
  4
+        <style>body { font-family: sans-serif; }</style>
  5
+    </head>
  6
+    <body>
  7
+
  8
+        <h1>Cors testing</h1>
  9
+
  10
+        <p><b>See <a href="https://test-cors.appspot.com/#technical">https://test-cors.appspot.com/</a> to run
  11
+            the test in your browser.</b></p>
  12
+
  13
+        <p>Make cors requests to <code>https://cors-test.appspot.com/test</code>. They will respond
  14
+        with <code>{"status":"ok"}</code>.</p>
  15
+
  16
+        <p>If you include an <code>Origin</code> header, it will be echoed in the
  17
+        <code>Access-Control-Allow-Origin</code> response, otherwise <code>*</code> is used.</p>
  18
+
  19
+        <p>If you include any <code>Access-Control-Request-Headers</code>, they will be echoed in an
  20
+        <code>Access-Control-Allow-Headers</code> response.</p>
  21
+
  22
+        <p>If you include any <code>Access-Control-Request-Method</code>, it will be echoed in an
  23
+        <code>Access-Control-Allow-Method</code> response.</p>
  24
+
  25
+        <p>For testing purposes, we also always send: <code>Access-Control-Max-Age: 0</code>, and
  26
+        <code>Set-Cookie: test=test</code>.
  27
+
  28
+        <hr>
  29
+        <p>For more information about CORS see <a
  30
+            href="https://developer.mozilla.org/en/HTTP_access_control">https://developer.mozilla.org/en/HTTP_access_control</a>
  31
+        and <a href="http://enable-cors.org/">http://enable-cors.org/</a>.</p>
  32
+
  33
+        <p>Code at <a
  34
+            href="https://github.com/rapportive-oss/cors-test">https://github.com/rapportive-oss/cors-test</a>, contact <a href="mailto:conrad@rapportive.com"/>conrad@rapportive.com</a></p>
  35
+    </body>
  36
+</html>
16  test-cors/app.yaml
... ...
@@ -0,0 +1,16 @@
  1
+application: test-cors
  2
+version: 1
  3
+runtime: python
  4
+api_version: 1
  5
+
  6
+default_expiration: "1d"
  7
+
  8
+handlers:
  9
+
  10
+- url: /
  11
+  static_files: index.html
  12
+  upload: index.html
  13
+
  14
+- url: /simple.json
  15
+  static_files: simple.json
  16
+  upload: simple.json
86  test-cors/index.html
... ...
@@ -0,0 +1,86 @@
  1
+<!DOCTYPE html>
  2
+<html>
  3
+
  4
+    <head>
  5
+        <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
  6
+        <title>Test CORS support</title>
  7
+
  8
+        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
  9
+
  10
+        <script>
  11
+            var technical = window.location.hash === "#technical";
  12
+
  13
+            function logged(msg, opts) {
  14
+                var li = jQuery("<li>").text(technical ? msg : 'testing...').appendTo(jQuery(".results")).addClass("unknown"),
  15
+                    d = jQuery.ajax(opts)
  16
+                             .done(function () {
  17
+                                 li.addClass("good");
  18
+                                 li.text((technical ?  msg : 'passed') + ' ✓');
  19
+                             })
  20
+                             .fail(function () {
  21
+                                 li.addClass("bad");
  22
+                                 li.text((technical ?  msg : 'failed') + ' ✗');
  23
+                             });
  24
+
  25
+                    setTimeout(function () {
  26
+                        if (!d.isResolved()) {
  27
+                            li.text(technical ? "timed out" : "not tested :(");
  28
+                        }
  29
+                    }, 10000);
  30
+            }
  31
+
  32
+            jQuery(function () {
  33
+
  34
+                if (technical) {
  35
+                    jQuery(".friendly").hide();
  36
+                    jQuery(".technical").show();
  37
+                }
  38
+
  39
+                var url = "https://cors-test.appspot.com/test";
  40
+
  41
+                jQuery(".useragent").text(navigator.userAgent);
  42
+
  43
+                logged("Simple local ajax", "simple.json");
  44
+
  45
+                logged("remote GET request", url);
  46
+
  47
+                logged("remote POST request", {url: url, type: 'POST'});
  48
+
  49
+                logged("remote DELETE request", {url: url, type: 'DELETE'});
  50
+
  51
+                logged("remote request with Header", {url: url, headers: {'X-Test-Header': 'test'}});
  52
+
  53
+                logged("remote request with credentials", {url: url, xhrFields: {withCredentials: true}});
  54
+
  55
+                logged("with HTTP basic auth", {url: url, username: 'foo', password: 'bar', xhrFields: {withCredentials: true}});
  56
+
  57
+                logged("all features combined", {url: url, type: 'HEAD', headers: {'X-Test-Header': 'test'}, xhrFields: {withCredentials: true}});
  58
+            });
  59
+        </script>
  60
+
  61
+        <style>
  62
+            body{ font-family: sans-serif; }
  63
+            .unknown{ color: #888; }
  64
+            .good{ color: #080; }
  65
+            .bad{ color: #800; }
  66
+            .technical{ display: none }
  67
+        </style>
  68
+    </head>
  69
+
  70
+    <body>
  71
+        <p class="useragent"></p>
  72
+        <ol class="results"></ol>
  73
+        <hr/>
  74
+
  75
+        <p class="technical">For a less technical view please see <a
  76
+            href="javascript:window.open('https://test-cors.appspot.com/#friendly')">https://test-cors.appspot.com/#friendly</a>.</p>
  77
+
  78
+        <p class="technical">This test suite makes requests to <a href="https://cors-test.appspot.com/">https://cors-test.appspot.com/</a>, see that page
  79
+        for implementation details. The code is on github at <a href="https://github.com/rapportive-oss/cors-test">https://github.com/rapportive-oss/cors-test</a></p>
  80
+
  81
+        <p>CORS testing service provided by <a href="http://rapportive.com">Rapportive</a>, contact <a href="mailto:conrad@rapportive.com">conrad@rapportive.com</a>.</p>
  82
+
  83
+        <p class="friendly">For technical details, see <a
  84
+            href="javascript:window.open('https://test-cors.appspot.com#technical')">https://test-cors.appspot.com/#technical</a>.</p>
  85
+    </body>
  86
+</html>
1  test-cors/simple.json
... ...
@@ -0,0 +1 @@
  1
+{"status":"ok"}

0 notes on commit b635a75

Please sign in to comment.
Something went wrong with that request. Please try again.