From: Manolo Carrasco Date: Sun, 5 Jan 2014 15:45:17 +0000 (+0100) Subject: Fix timeout and CORS implementation of JVM X-Git-Tag: gwtquery-project-1.4.3~44^2~24 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=aca58c8c0c56f28b7038012832b4c37c9f2abf5a;p=gwtquery.git Fix timeout and CORS implementation of JVM --- diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/GQ.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/GQ.java index 1e6aa304..aa10ce6c 100644 --- a/gwtquery-core/src/main/java/com/google/gwt/query/client/GQ.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/GQ.java @@ -15,15 +15,20 @@ */ package com.google.gwt.query.client; -import com.google.gwt.core.shared.GWT; +import com.google.gwt.core.client.GWT; import com.google.gwt.query.client.builders.JsonBuilder; import com.google.gwt.query.client.builders.JsonFactory; import com.google.gwt.query.client.plugins.ajax.Ajax.AjaxTransport; import com.google.gwt.query.client.plugins.ajax.AjaxTransportJs; import com.google.gwt.query.vm.AjaxTransportJre; import com.google.gwt.query.vm.JsonFactoryJre; +import com.google.gwt.user.client.Window; -public class GQ { +public abstract class GQ { + + public static String domain = GWT.isClient() + ? (Window.Location.getProtocol() + Window.Location.getHost()) + : "http://127.0.0.1"; private static JsonFactory jsonFactory; private static AjaxTransport ajaxTransport; diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/ajax/Ajax.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/ajax/Ajax.java index ecf5b626..9df0f582 100644 --- a/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/ajax/Ajax.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/ajax/Ajax.java @@ -33,7 +33,6 @@ import com.google.gwt.query.client.plugins.Plugin; public class Ajax extends GQuery { public static interface AjaxTransport { - Promise getJsonP(Settings settings); Promise getLoadScript(Settings settings); diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/deferred/PromiseReqBuilder.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/deferred/PromiseReqBuilder.java index 5b687e54..e6bc6d63 100644 --- a/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/deferred/PromiseReqBuilder.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/plugins/deferred/PromiseReqBuilder.java @@ -130,6 +130,7 @@ public class PromiseReqBuilder extends DeferredPromiseImpl implements RequestCal JsUtils.prop(xmlHttpRequest, "withCredentials", true); final Request request = createRequestVltr(xmlHttpRequest, settings.getTimeout(), this); + System.out.println("REQ timeout " + settings.getTimeout()); xmlHttpRequest.setOnReadyStateChange(new ReadyStateChangeHandler() { public void onReadyStateChange(XMLHttpRequest xhr) { diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/vm/AjaxTransportJre.java b/gwtquery-core/src/main/java/com/google/gwt/query/vm/AjaxTransportJre.java index 822da4c2..b5f6d40f 100644 --- a/gwtquery-core/src/main/java/com/google/gwt/query/vm/AjaxTransportJre.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/vm/AjaxTransportJre.java @@ -13,6 +13,7 @@ import com.google.gwt.http.client.Response; import com.google.gwt.query.client.Binder; import com.google.gwt.query.client.Function; import com.google.gwt.query.client.GQ; +import com.google.gwt.query.client.GQuery; import com.google.gwt.query.client.Promise; import com.google.gwt.query.client.plugins.ajax.Ajax.AjaxTransport; import com.google.gwt.query.client.plugins.ajax.Ajax.Settings; @@ -24,31 +25,48 @@ import com.google.gwt.user.server.Base64Utils; */ public class AjaxTransportJre implements AjaxTransport { + public AjaxTransportJre() { + System.setProperty("sun.net.http.allowRestrictedHeaders", "true"); + } + private final String USER_AGENT = "Mozilla/5.0"; + private final String jsonpCbRexp = "(?ms)^.*jre_callback\\((.*)\\).*$"; public Promise getJsonP(final Settings settings) { String url = settings.getUrl().replaceFirst("callback=[^&]*", ""); url += (url.contains("?") ? "&" : "?") + "callback=jre_callback"; settings.setUrl(url); - return getXhr(settings) + if (settings.getTimeout() < 1) { + settings.setTimeout(10000); + } + + return getXhr(settings, false) .then(new Function() { public Object f(Object... args) { - ResponseJre response = arguments(0); - return GQ.create(response.getText().replaceFirst("jre_callback\\((.*)\\)", "$1")); + Response response = arguments(0); + if (response.getText().matches(jsonpCbRexp)) { + return GQ.create(response.getText().replaceFirst(jsonpCbRexp, "$1")); + } else { + return GQuery.Deferred().reject().promise(); + } } }); } public Promise getLoadScript(Settings settings) { - return getXhr(settings); + return getXhr(settings, false); } - + public Promise getXhr(final Settings settings) { + return getXhr(settings, true); + } + + private Promise getXhr(final Settings settings, final boolean cors) { return new PromiseFunction() { public void f(Deferred dfd) { try { - Response response = httpClient(settings); + Response response = httpClient(settings, cors); int status = response.getStatusCode(); if (status <= 0 || status >= 400) { String statusText = status <= 0 ? "Bad CORS" : response.getStatusText(); @@ -63,10 +81,12 @@ public class AjaxTransportJre implements AjaxTransport { }; } - private Response httpClient(Settings s) throws Exception { - - URL u = new URL(s.getUrl()); - + private Response httpClient(Settings s, boolean cors) throws Exception { + String url = s.getUrl(); + if (!url.toLowerCase().startsWith("http")) { + url = GQ.domain + (url.startsWith("/") ? "" : "/") + url; + } + URL u = new URL(url); HttpURLConnection c = (HttpURLConnection) u.openConnection(); c.setRequestMethod(s.getType()); c.setRequestProperty("User-Agent", USER_AGENT); @@ -74,6 +94,31 @@ public class AjaxTransportJre implements AjaxTransport { c.setRequestProperty ("Authorization", "Basic " + Base64Utils.toBase64((s.getUsername() + ":" + s.getPassword()).getBytes())); } + boolean isCORS = cors && !s.getUrl().contains(GQ.domain); + if (isCORS) { + // TODO: fetch options previously to the request + // >> OPTIONS + // Origin: http://127.0.0.1:8888 + // Access-Control-Allow-Origin: http://127.0.0.1:8888 + // Access-Control-Allow-Credentials: true + // Access-Control-Request-Headers: content-type + // Access-Control-Allow-Headers + // Access-Control-Request-Method + // Access-Control-Allow-Methods: POST, GET + // Allow: GET, HEAD, POST, PUT, DELETE, TRACE, OPTIONS + + // >> POST/GET + // Origin: http://127.0.0.1:8888 + // Access-Control-Allow-Origin: http://127.0.0.1:8888 + // Access-Control-Allow-Credentials: true + c.setRequestProperty("Origin", GQ.domain); + } + + if (s.getTimeout() > 0) { + c.setConnectTimeout(s.getTimeout()); + c.setReadTimeout(s.getTimeout()); + } + Binder headers = s.getHeaders(); if (headers != null) { for (String h : headers.getFieldNames()) { @@ -94,13 +139,17 @@ public class AjaxTransportJre implements AjaxTransport { wr.flush(); wr.close(); } - + int code = c.getResponseCode(); + if (isCORS && !GQ.domain.equals(c.getHeaderField("Access-Control-Allow-Origin"))) { + code = 0; + } + BufferedReader in = new BufferedReader(new InputStreamReader(c.getInputStream())); String inputLine; StringBuffer response = new StringBuffer(); while ((inputLine = in.readLine()) != null) { - response.append(inputLine); + response.append(inputLine + "\n"); } in.close(); diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/vm/JsonFactoryJre.java b/gwtquery-core/src/main/java/com/google/gwt/query/vm/JsonFactoryJre.java index b81ca2d4..de07995a 100644 --- a/gwtquery-core/src/main/java/com/google/gwt/query/vm/JsonFactoryJre.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/vm/JsonFactoryJre.java @@ -11,9 +11,11 @@ import java.util.Date; import java.util.List; import org.json.JSONArray; +import org.json.JSONException; import org.json.JSONObject; import com.google.gwt.query.client.Binder; +import com.google.gwt.query.client.Function; import com.google.gwt.query.client.Properties; import com.google.gwt.query.client.builders.JsonBuilder; import com.google.gwt.query.client.builders.JsonFactory; @@ -40,7 +42,7 @@ public class JsonFactoryJre implements JsonFactory { } @SuppressWarnings("unchecked") - private Object jsonArrayToList(JSONArray j, Class ctype, boolean isArray) throws Throwable { + private Object jsonArrayToList(JSONArray j, Class ctype, boolean isArray) { List l = new ArrayList(); for (int i = 0; j != null && i < j.length() ; i++) { l.add((T)getValue(j, i, null, null, ctype, null)); @@ -95,12 +97,15 @@ public class JsonFactoryJre implements JsonFactory { } } else { ret = obj != null ? obj.get(attr): arr.get(idx); - if (ret instanceof JSONObject && Binder.class.isAssignableFrom(clz) && !clz.isAssignableFrom(ret.getClass())) { - ret = jsonFactory.create(clz, (JSONObject)ret); + if (ret instanceof JSONObject) { + if (clz == Object.class) { + ret = jsonFactory.createBinder((JSONObject)ret); + } else if (Binder.class.isAssignableFrom(clz) && !clz.isAssignableFrom(ret.getClass())) { + ret = jsonFactory.create(clz, (JSONObject)ret); + } } } - } catch (Throwable e) { - System.out.println(this.getClass().getSimpleName() + " ERROR getting attr=" + attr + " idx=" + idx + " Exception=" + e.getMessage()); + } catch (JSONException e) { } return ret; } @@ -138,7 +143,9 @@ public class JsonFactoryJre implements JsonFactory { JSONArray a = listToJsonArray(arg); return obj != null ? obj.put(attr, a) : arr.put(a); } else { - System.out.println("Unkown setter object " + attr + " " + o.getClass().getName() + " " + o); + if (!(o instanceof Function)) { + System.out.println("Unkown setter object " + attr + " " + o.getClass().getName() + " " + o); + } return obj != null ? obj.put(attr, o) : arr.put(o); } } catch (Throwable e) { @@ -267,7 +274,12 @@ public class JsonFactoryJre implements JsonFactory { InvocationHandler handler = new JsonBuilderHandler(); return (Binder)Proxy.newProxyInstance(Binder.class.getClassLoader(), new Class[] {Binder.class}, handler); } - + + public Binder createBinder(JSONObject jso) { + InvocationHandler handler = new JsonBuilderHandler(jso); + return (Binder)Proxy.newProxyInstance(Binder.class.getClassLoader(), new Class[] {Binder.class}, handler); + } + @SuppressWarnings("unchecked") public T create(String s) { Binder ret = createBinder(); diff --git a/gwtquery-core/src/main/super/com/google/gwt/query/super/com/google/gwt/query/client/GQ.java b/gwtquery-core/src/main/super/com/google/gwt/query/super/com/google/gwt/query/client/GQ.java index d694afa5..f4211249 100644 --- a/gwtquery-core/src/main/super/com/google/gwt/query/super/com/google/gwt/query/client/GQ.java +++ b/gwtquery-core/src/main/super/com/google/gwt/query/super/com/google/gwt/query/client/GQ.java @@ -23,8 +23,11 @@ import com.google.gwt.query.client.plugins.ajax.AjaxTransportJs; import com.google.gwt.query.client.plugins.ajax.Ajax.AjaxTransport; import com.google.gwt.query.vm.AjaxTransportJre; import com.google.gwt.query.vm.JsonFactoryJre; +import com.google.gwt.user.client.Window; public class GQ { + + public static final String domain = Window.Location.getHost(); private static JsonFactory jsonFactory; private static AjaxTransport ajaxTransport; diff --git a/gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryAjaxTestGwt.java b/gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryAjaxTestGwt.java index 945a7fa4..f22ce1e3 100644 --- a/gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryAjaxTestGwt.java +++ b/gwtquery-core/src/test/java/com/google/gwt/query/client/GQueryAjaxTestGwt.java @@ -167,21 +167,6 @@ public class GQueryAjaxTestGwt extends GWTTestCase { assertEquals("AName", f.getEntry()[0].getAuthor().getName().getText()); } - public void testJsonValidService() { - delayTestFinish(5000); - // Use a public json service - String testJsonpUrl = "https://www.googleapis.com/blogger/v2/blogs/user_id/posts/post_id?callback=?&key=NO-KEY"; - Ajax.getJSONP(testJsonpUrl, new Function(){ - public void f() { - Properties p = getDataProperties(); - // It should return error since we do not use a valid key - // {"error":{"errors":[{"domain":"usageLimits","reason":"keyInvalid","message":"Bad Request"}],"code":400,"message":"Bad Request"}} - assertEquals(400, p.getJavaScriptObject("error").cast().getInt("code")); - finishTest(); - } - }, null, 0); - } - @DoNotRunWith({Platform.HtmlUnitLayout}) public void testJsonNonCallbackResponse() { delayTestFinish(5000); @@ -194,73 +179,4 @@ public class GQueryAjaxTestGwt extends GWTTestCase { } }, 500); } - - public void testJsonpTimeout() { - delayTestFinish(5000); - String nonJsonpUrl = "http://127.0.0.1/nopage"; - - Settings s = Ajax.createSettings(); - s.setTimeout(1000); - s.setSuccess(new Function(){ - public void f() { - fail(); - } - }); - s.setError(new Function(){ - public void f() { - finishTest(); - } - }); - s.setDataType("jsonp"); - s.setUrl(nonJsonpUrl); - - Ajax.ajax(s); - } - - public void testAjaxError() { - delayTestFinish(5000); - String url = "http://127.0.0.1/nopage"; - - Ajax.ajax(Ajax.createSettings().setTimeout(1000).setUrl(url)) - .done(new Function(){ - public void f() { - fail(); - } - }).fail(new Function(){ - public void f() { - finishTest(); - } - }); - } - - public void testGetScript() { - delayTestFinish(5000); - String url = "http://code.jquery.com/jquery-2.0.3.min.js"; - Ajax.loadScript(url) - .done(new Function(){ - public void f() { - finishTest(); - } - }).fail(new Function(){ - public void f() { - fail(); - } - }); - } - - public void testGetScriptFail() { - delayTestFinish(5000); - String url = "http://127.0.0.1/nopage"; - Ajax.getScript(url) - .done(new Function(){ - public void f() { - fail(); - } - }).fail(new Function(){ - public void f() { - finishTest(); - } - }); - } - } diff --git a/gwtquery-core/src/test/java/com/google/gwt/query/client/ajax/AjaxCommon.java b/gwtquery-core/src/test/java/com/google/gwt/query/client/ajax/AjaxCommon.java index 2ff30953..fa790236 100644 --- a/gwtquery-core/src/test/java/com/google/gwt/query/client/ajax/AjaxCommon.java +++ b/gwtquery-core/src/test/java/com/google/gwt/query/client/ajax/AjaxCommon.java @@ -15,45 +15,61 @@ */ package com.google.gwt.query.client.ajax; -import static com.google.gwt.query.client.GQuery.*; - -import org.mortbay.jetty.Server; - -import net.sourceforge.htmlunit.corejs.javascript.Context; -import net.sourceforge.htmlunit.corejs.javascript.Scriptable; - -import com.gargoylesoftware.htmlunit.javascript.host.xml.XMLHttpRequest; -import com.google.gwt.core.client.GWT; +import com.google.gwt.http.client.Response; +import com.google.gwt.junit.DoNotRunWith; +import com.google.gwt.junit.Platform; import com.google.gwt.junit.client.GWTTestCase; import com.google.gwt.query.client.Binder; import com.google.gwt.query.client.Function; import com.google.gwt.query.client.GQ; -import com.google.gwt.query.client.Properties; +import com.google.gwt.query.client.Promise; import com.google.gwt.query.client.plugins.ajax.Ajax; import com.google.gwt.query.client.plugins.ajax.Ajax.Settings; /** - * Tests for Deferred which can run either in JVM and GWT + * Common Tests for Data Binding and Ajax which can run either in JVM and GWT */ public abstract class AjaxCommon extends GWTTestCase { - public String getModuleName() { - return null; - } - - protected String echoUrl, corsUrl; - protected Binder json, jsonData; + protected String echoUrl, echoUrlCORS; + protected Binder json, jsonGET; protected String servletPath = "test.json"; - private void performAjaxJsonTest(Settings s) { - delayTestFinish(5000); - Ajax.ajax(s).done(new Function(){public void f() { - Binder p = arguments(0); - assertEquals("abc", p.get("a")); - finishTest(); - }}).fail(new Function(){public void f() { + private Function failFunction = new Function() { + public void f() { fail(); - }}); + } + }; + + private Function finishFunction = new Function() { + public void f() { + finishTest(); + } + }; + + public AjaxCommon() { + jsonGET = GQ.create("data: {a: abc, d: def}"); + json = GQ.create("a: abc, d: def"); + } + + private Promise performAjaxJsonTest(Settings s) { + delayTestFinish(5000); + return Ajax.ajax(s) + .done(new Function(){public void f() { + Binder p = arguments(0); + assertEquals("abc", p.get("a")); + finishTest(); + }}) + .fail(failFunction); + } + + private Promise performAjaxJsonTest_CORS(Settings s) { + return performAjaxJsonTest(s) + .done(new Function() {public void f() { + Response r = arguments(3); + assertNotNull(r.getHeader("Access-Control-Allow-Origin")); + assertTrue(r.getHeader("Access-Control-Allow-Origin").contains(GQ.domain)); + }}); } public void testAjaxJsonPost() { @@ -63,26 +79,26 @@ public abstract class AjaxCommon extends GWTTestCase { .setData(json) .setDataType("json") .setUsername("testuser") - .setPassword("testpassword") - ; + .setPassword("testpassword"); + performAjaxJsonTest(s); } public void testAjaxJsonPost_CORS() { delayTestFinish(5000); Settings s = Ajax.createSettings() - .setUrl(corsUrl) + .setUrl(echoUrlCORS) .setData(json) .setDataType("json"); - performAjaxJsonTest(s); + performAjaxJsonTest_CORS(s); } public void testAjaxJsonGet() { Settings s = Ajax.createSettings() .setType("get") .setUrl(echoUrl) - .setData(jsonData) + .setData(jsonGET) .setDataType("json"); performAjaxJsonTest(s); @@ -91,66 +107,136 @@ public abstract class AjaxCommon extends GWTTestCase { public void testAjaxJsonGet_CORS() { Settings s = Ajax.createSettings() .setType("get") - .setUrl(corsUrl) - .setData(jsonData) + .setUrl(echoUrlCORS) + .setData(jsonGET) .setDataType("json"); - performAjaxJsonTest(s); + performAjaxJsonTest_CORS(s); } public void testAjaxGetJsonP() { delayTestFinish(5000); Settings s = Ajax.createSettings() .setType("post") - .setUrl(echoUrl) - .setData(jsonData) + .setUrl(echoUrlCORS) + .setData(jsonGET) .setDataType("jsonp"); performAjaxJsonTest(s); } - public void testAjaxGetJsonP_CORS() { + public void testJsonValidService() { + delayTestFinish(5000); + // Use a public json service supporting callback parameter + Ajax.getJSONP("https://www.googleapis.com/blogger/v2/blogs/user_id/posts/post_id?callback=?&key=NO-KEY") + .done(new Function(){ + public void f() { + Binder p = arguments(0); + // It should return error since we do not use a valid key + // {"error":{"errors":[{"domain":"usageLimits","reason":"keyInvalid","message":"Bad Request"}],"code":400,"message":"Bad Request"}} + assertEquals(400, p.get("error").get("code").intValue()); + finishTest(); + } + }) + .fail(failFunction); + } + + public void testInvalidOrigin() { delayTestFinish(5000); Settings s = Ajax.createSettings() - .setType("post") - .setUrl(corsUrl) - .setData(jsonData) - .setDataType("jsonp"); + // Use a public json service non CORS enabled + .setUrl("https://www.googleapis.com/blogger/v2/blogs/user_id/posts/post_id?key=NO-KEY") + .setDataType("json") + .setTimeout(1000); + + Ajax.ajax(s) + .done(failFunction) + .fail(finishFunction); + } + + public void testJsonInvalidService() { + delayTestFinish(5000); + Settings s = Ajax.createSettings() + // Use a valid javascript which does not wrap content in a callback + .setUrl("http://ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js") + .setDataType("jsonp") + .setTimeout(1000); + + Ajax.ajax(s) + .done(failFunction) + .fail(finishFunction); + } + + @DoNotRunWith(Platform.HtmlUnitBug) + public void testAjaxTimeout() { + delayTestFinish(5000); + Settings s = Ajax.createSettings() + .setTimeout(100) + .setType("get") + // Connecting to private networks out of our LAN raises a timeout because + // there is no route for them in public networks. + .setUrl("http://10.32.45.67:7654"); - performAjaxJsonTest(s); + Ajax.ajax(s) + .done(failFunction) + .fail(finishFunction); } + public void testJsonpTimeout() { + delayTestFinish(5000); + Settings s = Ajax.createSettings() + .setTimeout(1000) + .setDataType("jsonp") + .setUrl(echoUrl + "?timeout=2000"); + + Ajax.ajax(s) + .done(failFunction) + .fail(finishFunction); + } + public void testAjaxError() { + delayTestFinish(5000); + String url = "http://127.0.0.1/nopage"; + + Ajax.ajax(Ajax.createSettings().setTimeout(1000).setUrl(url)) + .done(new Function(){ + public void f() { + fail(); + } + }).fail(new Function(){ + public void f() { + finishTest(); + } + }); + } -// public void testAjaxJson() { -// delayTestFinish(5000); -// Settings s = Ajax.createSettings() -// .setType("get") -// .setUrl(GWT.getModuleBaseURL() + "test.json") -// .setData($$("data: {a: abc, d: ddd}")) -// .setDataType("json"); -// -// Ajax.ajax(s).done(new Function(){public void f() { -// Binder p = arguments(0); -// assertEquals("abc", p.get("a")); -// finishTest(); -// }}).fail(new Function(){public void f() { -// fail(); -// }}); -// } - public void testJsonValidService() { + public void testLoadScript() { delayTestFinish(5000); - // Use a public json service - String testJsonpUrl = "https://www.googleapis.com/blogger/v2/blogs/user_id/posts/post_id?callback=?&key=NO-KEY"; - Ajax.getJSONP(testJsonpUrl, new Function(){ - public void f() { - Binder p = arguments(0); - // It should return error since we do not use a valid key - // {"error":{"errors":[{"domain":"usageLimits","reason":"keyInvalid","message":"Bad Request"}],"code":400,"message":"Bad Request"}} - assertEquals(400, p.get("error").get("code")); - finishTest(); - } - }, null, 0); + String url = "http://code.jquery.com/jquery-2.0.3.min.js"; + Ajax.loadScript(url) + .done(new Function(){ + public void f() { + finishTest(); + } + }).fail(new Function(){ + public void f() { + fail(); + } + }); } + public void testGetScriptFail() { + delayTestFinish(5000); + String url = "http://127.0.0.1/nopage"; + Ajax.getScript(url) + .done(new Function(){ + public void f() { + fail(); + } + }).fail(new Function(){ + public void f() { + finishTest(); + } + }); + } } \ No newline at end of file diff --git a/gwtquery-core/src/test/java/com/google/gwt/query/client/ajax/AjaxTest.java b/gwtquery-core/src/test/java/com/google/gwt/query/client/ajax/AjaxTest.java index 45636766..031e5303 100644 --- a/gwtquery-core/src/test/java/com/google/gwt/query/client/ajax/AjaxTest.java +++ b/gwtquery-core/src/test/java/com/google/gwt/query/client/ajax/AjaxTest.java @@ -17,6 +17,7 @@ package com.google.gwt.query.client.ajax; import java.util.HashMap; import java.util.Map; +import java.util.Random; import javax.servlet.Servlet; @@ -26,30 +27,27 @@ import org.mortbay.jetty.servlet.DefaultServlet; import org.mortbay.jetty.webapp.WebAppClassLoader; import org.mortbay.jetty.webapp.WebAppContext; -import com.google.gwt.query.client.GQ; import com.google.gwt.query.servlet.GQAjaxTestServlet; /** - * Tests for Deferred which can run either in JVM and GWT + * Tests for Data Binders and Ajax run in the JVM */ public class AjaxTest extends AjaxCommon { static Server server; - int port = 3333; + static int port = new Random().nextInt(1000) + 2000; public String getModuleName() { return null; } - protected void gwtSetUp() throws Exception { + public AjaxTest() throws Exception { echoUrl = "http://127.0.0.1:" + port + "/" + servletPath; - corsUrl = "http://localhost:" + port + "/" + servletPath; - jsonData = GQ.create("data: {a: abc, d: ddd}"); - json = GQ.create("a: abc, d: ddd"); - startWebServer(); + echoUrlCORS = "http://localhost:" + port + "/" + servletPath + "?cors=true"; + startWebServer(port); } - protected void startWebServer() throws Exception { + protected void startWebServer(int port) throws Exception { if (server == null) { final Map> servlets = new HashMap>(); servlets.put("/" + servletPath, GQAjaxTestServlet.class); diff --git a/gwtquery-core/src/test/java/com/google/gwt/query/client/ajax/AjaxTestGwt.java b/gwtquery-core/src/test/java/com/google/gwt/query/client/ajax/AjaxTestGwt.java index 022c84b1..0e6bf736 100644 --- a/gwtquery-core/src/test/java/com/google/gwt/query/client/ajax/AjaxTestGwt.java +++ b/gwtquery-core/src/test/java/com/google/gwt/query/client/ajax/AjaxTestGwt.java @@ -16,23 +16,20 @@ package com.google.gwt.query.client.ajax; import com.google.gwt.core.client.GWT; -import com.google.gwt.query.client.GQ; /** - * Test for data binding shared code run in gwt + * Test for data binding and Ajax which is run in gwt */ public class AjaxTestGwt extends AjaxCommon { + @Override public String getModuleName() { return "com.google.gwt.query.QueryTest"; } - @Override - protected void gwtSetUp() throws Exception { + public AjaxTestGwt() { echoUrl = (GWT.isClient() ? GWT.getHostPageBaseURL() : "http://localhost:3333/") + servletPath; - corsUrl = echoUrl.replaceFirst("http://[\\d\\.]+:", "http://localhost:") + "?cors=true"; - jsonData = GQ.create("data: {a: abc, d: ddd}"); - json = GQ.create("a: abc, d: ddd"); + echoUrlCORS = echoUrl.replaceFirst("http://[\\d\\.]+:", "http://localhost:") + "?cors=true"; } } diff --git a/gwtquery-core/src/test/java/com/google/gwt/query/servlet/GQAjaxTestServlet.java b/gwtquery-core/src/test/java/com/google/gwt/query/servlet/GQAjaxTestServlet.java index 6ba602da..6f4fc1a4 100644 --- a/gwtquery-core/src/test/java/com/google/gwt/query/servlet/GQAjaxTestServlet.java +++ b/gwtquery-core/src/test/java/com/google/gwt/query/servlet/GQAjaxTestServlet.java @@ -33,6 +33,8 @@ public class GQAjaxTestServlet extends HttpServlet { Thread.sleep(ms); } catch (Exception e) { } + System.out.println(name + "timeout"); + return; } String data = "";