Permalink
Browse files

Initial commit.

  • Loading branch information...
0 parents commit b635a756cb4d2960711c65e266dee2c10e1535a7 @ConradIrwin ConradIrwin committed Sep 19, 2011
Showing with 196 additions and 0 deletions.
  1. +7 −0 LICENSE.MIT
  2. +5 −0 README.md
  3. +13 −0 cors-test/app.yaml
  4. +32 −0 cors-test/cors_test.py
  5. +36 −0 cors-test/index.html
  6. +16 −0 test-cors/app.yaml
  7. +86 −0 test-cors/index.html
  8. +1 −0 test-cors/simple.json
7 LICENSE.MIT
@@ -0,0 +1,7 @@
+Copyright (c) 2011 Conrad Irwin <conrad@rapportive.com>
+
+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:
+
+The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
+
+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
@@ -0,0 +1,5 @@
+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/).
+
+Feel free to use those sites, or to roll your own versions using this code.
+
+Pull requests and bug reports are most welcome.
13 cors-test/app.yaml
@@ -0,0 +1,13 @@
+application: cors-test
+version: 1
+runtime: python
+api_version: 1
+
+handlers:
+
+- url: /
+ static_files: index.html
+ upload: index.html
+
+- url: /test
+ script: cors_test.py
32 cors-test/cors_test.py
@@ -0,0 +1,32 @@
+from google.appengine.ext import webapp
+from google.appengine.ext.webapp.util import run_wsgi_app
+
+class CORSEnabledHandler(webapp.RequestHandler):
+ def get(self):
+ add_header = self.response.headers.add_header
+ if 'Origin' in self.request.headers:
+ add_header("Access-Control-Allow-Origin", self.request.headers['Origin'])
+ else:
+ add_header("Access-Control-Allow-Origin", '*')
+
+ if 'Access-Control-Request-Headers' in self.request.headers:
+ add_header('Access-Control-Allow-Headers', self.request.headers['Access-Control-Request-Headers'])
+
+ if 'Access-Control-Request-Method' in self.request.headers:
+ add_header('Access-Control-Allow-Methods', self.request.headers['Access-Control-Request-Method'])
+
+ add_header('Access-Control-Max-Age', '0')
+
+ add_header("Access-Control-Allow-Credentials", "true")
+
+ add_header('Cache-Control', 'no-cache')
+ add_header('Expires', 'Fri, 01 Jan 1990 00:00:00 GMT')
+ self.response.headers['Content-Type'] = 'application/json'
+ self.response.out.write('{"status":"ok"}')
+
+ post = put = head = options = delete = trace = get
+
+application = webapp.WSGIApplication([('/test', CORSEnabledHandler)], debug=True)
+
+if __name__ == "__main__":
+ run_wsgi_app(application)
36 cors-test/index.html
@@ -0,0 +1,36 @@
+<html>
+ <head>
+ <title>Cors testing app</title>
+ <style>body { font-family: sans-serif; }</style>
+ </head>
+ <body>
+
+ <h1>Cors testing</h1>
+
+ <p><b>See <a href="https://test-cors.appspot.com/#technical">https://test-cors.appspot.com/</a> to run
+ the test in your browser.</b></p>
+
+ <p>Make cors requests to <code>https://cors-test.appspot.com/test</code>. They will respond
+ with <code>{"status":"ok"}</code>.</p>
+
+ <p>If you include an <code>Origin</code> header, it will be echoed in the
+ <code>Access-Control-Allow-Origin</code> response, otherwise <code>*</code> is used.</p>
+
+ <p>If you include any <code>Access-Control-Request-Headers</code>, they will be echoed in an
+ <code>Access-Control-Allow-Headers</code> response.</p>
+
+ <p>If you include any <code>Access-Control-Request-Method</code>, it will be echoed in an
+ <code>Access-Control-Allow-Method</code> response.</p>
+
+ <p>For testing purposes, we also always send: <code>Access-Control-Max-Age: 0</code>, and
+ <code>Set-Cookie: test=test</code>.
+
+ <hr>
+ <p>For more information about CORS see <a
+ href="https://developer.mozilla.org/en/HTTP_access_control">https://developer.mozilla.org/en/HTTP_access_control</a>
+ and <a href="http://enable-cors.org/">http://enable-cors.org/</a>.</p>
+
+ <p>Code at <a
+ 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>
+ </body>
+</html>
16 test-cors/app.yaml
@@ -0,0 +1,16 @@
+application: test-cors
+version: 1
+runtime: python
+api_version: 1
+
+default_expiration: "1d"
+
+handlers:
+
+- url: /
+ static_files: index.html
+ upload: index.html
+
+- url: /simple.json
+ static_files: simple.json
+ upload: simple.json
86 test-cors/index.html
@@ -0,0 +1,86 @@
+<!DOCTYPE html>
+<html>
+
+ <head>
+ <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
+ <title>Test CORS support</title>
+
+ <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
+
+ <script>
+ var technical = window.location.hash === "#technical";
+
+ function logged(msg, opts) {
+ var li = jQuery("<li>").text(technical ? msg : 'testing...').appendTo(jQuery(".results")).addClass("unknown"),
+ d = jQuery.ajax(opts)
+ .done(function () {
+ li.addClass("good");
+ li.text((technical ? msg : 'passed') + '');
+ })
+ .fail(function () {
+ li.addClass("bad");
+ li.text((technical ? msg : 'failed') + '');
+ });
+
+ setTimeout(function () {
+ if (!d.isResolved()) {
+ li.text(technical ? "timed out" : "not tested :(");
+ }
+ }, 10000);
+ }
+
+ jQuery(function () {
+
+ if (technical) {
+ jQuery(".friendly").hide();
+ jQuery(".technical").show();
+ }
+
+ var url = "https://cors-test.appspot.com/test";
+
+ jQuery(".useragent").text(navigator.userAgent);
+
+ logged("Simple local ajax", "simple.json");
+
+ logged("remote GET request", url);
+
+ logged("remote POST request", {url: url, type: 'POST'});
+
+ logged("remote DELETE request", {url: url, type: 'DELETE'});
+
+ logged("remote request with Header", {url: url, headers: {'X-Test-Header': 'test'}});
+
+ logged("remote request with credentials", {url: url, xhrFields: {withCredentials: true}});
+
+ logged("with HTTP basic auth", {url: url, username: 'foo', password: 'bar', xhrFields: {withCredentials: true}});
+
+ logged("all features combined", {url: url, type: 'HEAD', headers: {'X-Test-Header': 'test'}, xhrFields: {withCredentials: true}});
+ });
+ </script>
+
+ <style>
+ body{ font-family: sans-serif; }
+ .unknown{ color: #888; }
+ .good{ color: #080; }
+ .bad{ color: #800; }
+ .technical{ display: none }
+ </style>
+ </head>
+
+ <body>
+ <p class="useragent"></p>
+ <ol class="results"></ol>
+ <hr/>
+
+ <p class="technical">For a less technical view please see <a
+ href="javascript:window.open('https://test-cors.appspot.com/#friendly')">https://test-cors.appspot.com/#friendly</a>.</p>
+
+ <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
+ 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>
+
+ <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>
+
+ <p class="friendly">For technical details, see <a
+ href="javascript:window.open('https://test-cors.appspot.com#technical')">https://test-cors.appspot.com/#technical</a>.</p>
+ </body>
+</html>
1 test-cors/simple.json
@@ -0,0 +1 @@
+{"status":"ok"}

0 comments on commit b635a75

Please sign in to comment.