Revision: 5619
Author: [email protected]
Date: Thu Oct 24 19:11:23 2013 UTC
Log: Convert fetching proxy tests to client-side tests.
https://codereview.appspot.com/16700043
This allows the tests to be reused for a proxy written for a different
server platform. Also added a test of Unicode-correctness, and found and
fixed an precedence-based error which made USE_AS_PROXY useless.
[email protected]
http://code.google.com/p/google-caja/source/detail?r=5619
Added:
/trunk/tests/com/google/caja/plugin/test-fetch-proxy-fixture-unicode.ujs
/trunk/tests/com/google/caja/plugin/test-fetch-proxy-fixture.css
/trunk/tests/com/google/caja/plugin/test-fetch-proxy.js
Modified:
/trunk/build.xml
/trunk/src/com/google/caja/plugin/caja.js
/trunk/tests/com/google/caja/plugin/browser-tests.json
/trunk/tests/com/google/caja/service/ProxyHandlerTest.java
/trunk/tests/com/google/caja/util/LocalServer.java
=======================================
--- /dev/null
+++
/trunk/tests/com/google/caja/plugin/test-fetch-proxy-fixture-unicode.ujs
Thu Oct 24 19:11:23 2013 UTC
@@ -0,0 +1,1 @@
+1२𐄉
=======================================
--- /dev/null
+++ /trunk/tests/com/google/caja/plugin/test-fetch-proxy-fixture.css Thu
Oct 24 19:11:23 2013 UTC
@@ -0,0 +1,1 @@
+body {}
=======================================
--- /dev/null
+++ /trunk/tests/com/google/caja/plugin/test-fetch-proxy.js Thu Oct 24
19:11:23 2013 UTC
@@ -0,0 +1,199 @@
+// Copyright (C) 2013 Google Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+/**
+ * @fileoverview Tests of the fetching proxy server.
+ *
+ * @author [email protected]
+ * @requires caja, jsunitRun, readyToTest, basicCajaConfig, location
+ */
+
+(function() {
+ 'use strict';
+
+ // xhr wrapper for exercising explicit requests, specialized for use in
tests
+ // note uses jsunitCallback and associates with the calling test
+ function tfetch(url, expectedMimeType, callback) {
+ var request = new XMLHttpRequest();
+ request.open('GET', url.toString(), true);
+ request.onreadystatechange = jsunitCallback(function() {
+ if (request.readyState === 4) {
+ if (request.status === 200) {
+ console.log('Response: ' + request.responseText);
+ assertEquals('response Content-Type', expectedMimeType,
+ request.getResponseHeader('Content-Type').split(';')[0]);
+ callback(request.responseText);
+ } else {
+ fail('Unexpected response: ' + request.statusText);
+ }
+ }
+ });
+ request.send();
+ }
+
+ var JSONP_RE = /^([a-zA-Z_]+)\((\{.*\})\);$/;
+
+ function docURL(name) {
+ return location.protocol + '//' + location.host +
+ '/ant-testlib/com/google/caja/plugin/' + name;
+ }
+
+ function docURLForURL(name) {
+ return encodeURIComponent(docURL(name));
+ }
+
+ function assertSuccessfulResponse(content, response) {
+ assertEquals('object', typeof response);
+ assertEquals(content, response.html);
+ if ('messages' in response) {
+ assertEquals(0, response.messages.length);
+ }
+ }
+
+ function assertErrorResponse(response) {
+ assertEquals('object', typeof response);
+ assertFalse('html' in response);
+ if ('messages' in response) {
+ assertTrue('error message present', response.messages.length > 0);
+ }
+ }
+
+ // --- What we're testing ---
+
+ // Note: If we need tests for a different server, add URL parameters to
+ // select it here.
+ var server = basicCajaConfig['cajaServer'];
+ var fetcher = caja.policy.net.fetcher.USE_AS_PROXY(server);
+
+
+ // TODO(kpreid): Test USE_AS_PROXY itself (e.g. exactly what URL it is
+ // constructing and sending to the server)
+
+
+ // --- Server tests ---
+ // Testing that the server behaves properly as an HTTP server
independent of
+ // our client.
+
+ jsunitRegister('testServerJsonp', function() {
+ tfetch(
+ server + '/cajole?url=' +
docURLForURL('test-fetch-proxy-fixture.css')
+ + '&input-mime-type=text/css'
+ + '&alt=json-in-script'
+ + '&callback=foo'
+ + '&transform=PROXY'
+ + '&build-version=' + cajaBuildVersion,
+ 'text/javascript',
+ function(response) {
+ var match = JSONP_RE.exec(response);
+ assertTrue('is JSONP', !!match);
+ assertEquals('foo', match[1]);
+ assertSuccessfulResponse('body {}', JSON.parse(match[2]));
+ jsunitPass();
+ });
+ });
+
+ // TODO(kpreid): We no longer care about JSON output; remove server
support
+ // for it and then remove this test
+ jsunitRegister('testServerJson', function() {
+ tfetch(
+ server + '/cajole?url=' +
docURLForURL('test-fetch-proxy-fixture.css')
+ + '&input-mime-type=text/css'
+ + '&alt=json'
+ + '&callback=foo'
+ + '&transform=PROXY'
+ + '&build-version=' + cajaBuildVersion,
+ 'application/json',
+ function(response) {
+ assertSuccessfulResponse('body {}', JSON.parse(response));
+ jsunitPass();
+ });
+ });
+
+ jsunitRegister('testServerJsonpAbsent', function() {
+ tfetch(
+ server + '/cajole?url=' +
docURLForURL('test-fetch-proxy-nonexistent.css')
+ + '&input-mime-type=text/css'
+ + '&alt=json-in-script'
+ + '&callback=foo'
+ + '&transform=PROXY'
+ + '&build-version=' + cajaBuildVersion,
+ 'text/javascript',
+ function(response) {
+ var match = JSONP_RE.exec(response);
+ assertTrue('is JSONP', !!match);
+ assertEquals('foo', match[1]);
+ assertErrorResponse(JSON.parse(match[2]));
+ jsunitPass();
+ });
+ });
+
+ jsunitRegister('testServerJsonAbsent', function() {
+ tfetch(
+ server + '/cajole?url=' +
docURLForURL('test-fetch-proxy-nonexistent.css')
+ + '&input-mime-type=text/css'
+ + '&alt=json'
+ + '&callback=foo'
+ + '&transform=PROXY'
+ + '&build-version=' + cajaBuildVersion,
+ 'application/json',
+ function(response) {
+ assertErrorResponse(JSON.parse(response));
+ jsunitPass();
+ });
+ });
+
+
+ // --- End-to-end tests ---
+ // Testing both proxy server behavior and the USE_AS_PROXY client glue.
+
+ jsunitRegister('testBasic', function() {
+ fetcher(docURL('test-fetch-proxy-fixture.css'), 'text/css',
+ jsunitCallback(function(response) {
+ console.log('Response:', response);
+ assertSuccessfulResponse('body {}', response);
+ jsunitPass();
+ }));
+ });
+
+ jsunitRegister('testError', function() {
+ fetcher(docURL('test-fetch-proxy-nonexistent.css'), 'text/css',
+ jsunitCallback(function(response) {
+ console.log('Response:', response);
+ assertErrorResponse(response);
+ jsunitPass();
+ }));
+ });
+
+ jsunitRegister('testUnexpectedMimeType', function() {
+ fetcher(docURL('test-fetch-proxy-fixture.css'), 'text/javascript',
+ jsunitCallback(function(response) {
+ console.log('Response:', response);
+ assertErrorResponse(response);
+ jsunitPass();
+ }));
+ });
+
+ jsunitRegister('testUnicode', function() {
+ // not actually JS, but the current fetcher only permits CSS and JS
+
fetcher(docURL('test-fetch-proxy-fixture-unicode.ujs'), 'text/javascript',
+ jsunitCallback(function(response) {
+ console.log('Response:', response);
+ assertSuccessfulResponse('1\u0968\ud800\udd09', response);
+ jsunitPass();
+ }));
+ });
+
+ readyToTest();
+ jsunitRun();
+})();
=======================================
--- /trunk/build.xml Thu Oct 17 22:14:55 2013 UTC
+++ /trunk/build.xml Thu Oct 24 19:11:23 2013 UTC
@@ -891,7 +891,7 @@
<target name="AllTests"
depends="CajolingServlet,PlaygroundBE,PlaygroundSupportingFiles">
<copy todir="${testlib}">
<fileset dir="${src}"
includes="**/*.html,**/*.js,**/*.css,**/*.jpg,**/*.png"/>
- <fileset dir="${tests}"
includes="**/*.html,**/*.js,**/*.css,**/*.jpg,**/*.png"/>
+ <fileset dir="${tests}"
includes="**/*.html,**/*.js,**/*.css,**/*.jpg,**/*.png,**/*.ujs"/>
<fileset dir="${src}" includes="**/apitaming/*"/>
<fileset dir="${tests}" includes="**/apitaming/*"/>
</copy>
=======================================
--- /trunk/src/com/google/caja/plugin/caja.js Tue Oct 8 16:46:28 2013 UTC
+++ /trunk/src/com/google/caja/plugin/caja.js Thu Oct 24 19:11:23 2013 UTC
@@ -58,7 +58,7 @@
// TODO(jasvir): Make it so this does not pollute the host page
// namespace but rather just the loaderFrame
installSyncScript(rndName,
- proxyServer ? String(proxyServer) : caja['server']
+ (proxyServer ? String(proxyServer) : caja['server'])
+ '/cajole?url=' + encodeURIComponent(url.toString())
+ '&input-mime-type=' + encodeURIComponent(mime)
+ '&transform=PROXY'
=======================================
--- /trunk/tests/com/google/caja/plugin/browser-tests.json Tue Oct 8
16:46:28 2013 UTC
+++ /trunk/tests/com/google/caja/plugin/browser-tests.json Thu Oct 24
19:11:23 2013 UTC
@@ -49,6 +49,7 @@
"expecting them should cause it to never make progress in load()
or",
"whenReady() calls."]
},
+ { "driver": "test-fetch-proxy.js" },
{ "driver": "test-client-uri-rewriting.js" },
{ "bare": "cajajs-bare-test.html" }
]},
=======================================
--- /trunk/tests/com/google/caja/service/ProxyHandlerTest.java Wed Apr 17
23:02:09 2013 UTC
+++ /trunk/tests/com/google/caja/service/ProxyHandlerTest.java Thu Oct 24
19:11:23 2013 UTC
@@ -17,6 +17,12 @@
import com.google.caja.reporting.BuildInfo;
/**
+ * Note: These tests are now redundant with browser-side test
+ * .../plugin/test-fetch-proxy.js. They have been left in because there's
no
+ * need to delete them and it's a little better to have Java tests for Java
+ * code, but in the event the implementation of the proxy is changed these
might
+ * as well be discarded.
+ *
* @author [email protected] (Kevin Reid)
*/
public class ProxyHandlerTest extends ServiceTestCase {
=======================================
--- /trunk/tests/com/google/caja/util/LocalServer.java Mon Jul 8 23:40:14
2013 UTC
+++ /trunk/tests/com/google/caja/util/LocalServer.java Thu Oct 24 19:11:23
2013 UTC
@@ -68,6 +68,8 @@
// static file serving for tests
final ResourceHandler resource_handler = new ResourceHandler();
resource_handler.setResourceBase(".");
+ resource_handler.getMimeTypes().addMimeMapping(
+ "ujs", "text/javascript;charset=utf-8");
// caja (=playground for now) server under /caja directory
final String subdir = "/caja";
--
---
You received this message because you are subscribed to the Google Groups "Google Caja Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.