From 4cf8b530274cc460312dcd440f99e98504cc3cdc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89ric=20Lemoine?= Date: Tue, 9 Sep 2008 06:55:19 +0000 Subject: [PATCH] Fix for "extra '?' is added to the URL by the LoadURL method", review and final patch from tschaub (closes #1616) git-svn-id: http://svn.openlayers.org/trunk/openlayers@7984 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf --- lib/OpenLayers/Request.js | 8 ++++++-- tests/Request.html | 37 ++++++++++++++++++++++++++++++++++++- 2 files changed, 42 insertions(+), 3 deletions(-) diff --git a/lib/OpenLayers/Request.js b/lib/OpenLayers/Request.js index 2cf595d8d6..e0ca23a1b4 100644 --- a/lib/OpenLayers/Request.js +++ b/lib/OpenLayers/Request.js @@ -87,7 +87,11 @@ OpenLayers.Request = { var request = new OpenLayers.Request.XMLHttpRequest(); var url = config.url; if(config.params) { - url += "?" + OpenLayers.Util.getParameterString(config.params); + var paramString = OpenLayers.Util.getParameterString(config.params); + if(paramString.length > 0) { + var separator = (url.indexOf('?') > -1) ? '&' : '?'; + url += separator + paramString; + } } if(config.proxy && (url.indexOf("http") == 0)) { url = config.proxy + encodeURIComponent(url); @@ -265,4 +269,4 @@ OpenLayers.Request = { return OpenLayers.Request.issue(config); } -}; \ No newline at end of file +}; diff --git a/tests/Request.html b/tests/Request.html index 28484d2406..b1807e21f7 100644 --- a/tests/Request.html +++ b/tests/Request.html @@ -20,7 +20,7 @@ function test_issue(t) { setup(); - t.plan(19); + t.plan(22); var request, config; var proto = OpenLayers.Request.XMLHttpRequest.prototype; var issue = OpenLayers.Function.bind(OpenLayers.Request.issue, @@ -46,8 +46,43 @@ t.eq(async, config.async, "open called with correct async"); t.eq(user, config.user, "open called with correct user"); t.eq(password, config.password, "open called with correct password"); + }; + request = issue(config); + + // test that params are serialized as query string - 1 test + config = { + method: "GET", + url: "http://example.com/", + params: {"foo": "bar"} + }; + proto.open = function(method, url, async, user, password) { + t.eq(url, config.url + "?foo=bar", "params serialized as query string"); + }; + request = issue(config); + + // test that empty params object doesn't produce query string - 1 test + config = { + method: "GET", + url: "http://example.com/", + params: {} + }; + proto.open = function(method, url, async, user, password) { + t.eq(url, config.url, "empty params doesn't produce query string"); } request = issue(config); + + // test that query string doesn't get two ? separators + config = { + method: "GET", + url: "http://example.com/?existing=query", + params: {"foo": "bar"} + }; + proto.open = function(method, url, async, user, password) { + t.eq(url, config.url + "&foo=bar", "existing query string gets extended with &"); + } + request = issue(config); + + // reset open method proto.open = _open; // test that headers are correctly set - 4 tests