diff --git a/lib/OpenLayers/Protocol/HTTP.js b/lib/OpenLayers/Protocol/HTTP.js index 47a7d1bea9..45b4cb2ca2 100644 --- a/lib/OpenLayers/Protocol/HTTP.js +++ b/lib/OpenLayers/Protocol/HTTP.js @@ -55,6 +55,13 @@ OpenLayers.Protocol.HTTP = OpenLayers.Class(OpenLayers.Protocol, { */ scope: null, + /** + * Property: readWithPOST + * {Boolean} true if read operations are done with POST requests + * instead of GET, defaults to false. + */ + readWithPOST: false, + /** * Constructor: OpenLayers.Protocol.HTTP * A class for giving layers generic HTTP protocol. @@ -120,6 +127,7 @@ OpenLayers.Protocol.HTTP = OpenLayers.Class(OpenLayers.Protocol, { * serialized according to the OpenSearch Geo extension * (bbox=minx,miny,maxx,maxy). Note that a BBOX filter as the child * of a logical filter will not be serialized. + * readWithPOST - {Boolean} If the request should be done with POST. * * Returns: * {} A response object, whose "priv" property @@ -129,8 +137,10 @@ OpenLayers.Protocol.HTTP = OpenLayers.Class(OpenLayers.Protocol, { */ read: function(options) { options = OpenLayers.Util.applyDefaults(options, this.options); + var readWithPOST = (options.readWithPOST !== undefined) ? + options.readWithPOST : this.readWithPOST; var resp = new OpenLayers.Protocol.Response({requestType: "read"}); - + if(options.filter && options.filter instanceof OpenLayers.Filter.Spatial) { if(options.filter.type == OpenLayers.Filter.Spatial.BBOX) { options.params = OpenLayers.Util.extend(options.params, { @@ -139,12 +149,23 @@ OpenLayers.Protocol.HTTP = OpenLayers.Class(OpenLayers.Protocol, { } } - resp.priv = OpenLayers.Request.GET({ - url: options.url, - callback: this.createCallback(this.handleRead, resp, options), - params: options.params, - headers: options.headers - }); + if(readWithPOST) { + resp.priv = OpenLayers.Request.POST({ + url: options.url, + callback: this.createCallback(this.handleRead, resp, options), + data: OpenLayers.Util.getParameterString(options.params), + headers: { + "Content-Type": "application/x-www-form-urlencoded" + } + }); + } else { + resp.priv = OpenLayers.Request.GET({ + url: options.url, + callback: this.createCallback(this.handleRead, resp, options), + params: options.params, + headers: options.headers + }); + } return resp; }, diff --git a/tests/Protocol/HTTP.html b/tests/Protocol/HTTP.html index 3892754ae8..9b3beca025 100644 --- a/tests/Protocol/HTTP.html +++ b/tests/Protocol/HTTP.html @@ -106,6 +106,98 @@ OpenLayers.Request.GET = _get; } + function test_readWithPOST(t) { + t.plan(10); + var protocol = new OpenLayers.Protocol.HTTP({ + 'url': 'foo_url', + 'params': {'k': 'foo_param'} + }); + + // fake XHR request object + var request = {'status': 200}; + + // options to pass to read + var readOptions = { + 'url': 'bar_url', + 'params': {'k': 'bar_param'}, + 'scope': {'hello': 'world'}, + 'callback': function() {}, + 'readWithPOST': true + }; + + var response; + + protocol.handleResponse = function(resp, opt) { + // 4 tests + var req = resp.priv; + t.ok(this == protocol, + 'handleResponse called with correct scope'); + t.ok(opt == readOptions, + 'handleResponse called with correct options'); + t.eq(resp.CLASS_NAME, 'OpenLayers.Protocol.Response', + 'handleResponse called with a Response object'); + t.eq(req, request, + 'handleResponse called with correct request'); + + response = resp; + }; + + var _post = OpenLayers.Request.POST; + + OpenLayers.Request.POST = function(options) { + // 5 tests + t.eq(options.url, readOptions.url, + 'GET with POST called with correct url in options'); + t.eq(options.data, OpenLayers.Util.getParameterString(readOptions.params), + 'GET with POST called with correct params encoded in options'); + t.eq(options.headers, {"Content-Type": "application/x-www-form-urlencoded"}, + 'GET with POST called with correct headers (application/x-www-form-urlencoded)'); + t.eq(options.scope, undefined, + 'GET with POST called with correct scope in options'); + t.ok(typeof options.callback == 'function', + 'GET with POST called with a callback in options'); + t.delay_call(0.1, function() { + options.callback(request); + t.ok(resp == response, + 'read returns the expected response object'); + // cleanup + protocol.destroy(); + OpenLayers.Request.POST = _post; + }); + return request; + }; + + var resp = protocol.read(readOptions); + + OpenLayers.Request.POST = _post; + } + + function test_read_method(t) { + t.plan(4); + + var _post = OpenLayers.Request.POST; + OpenLayers.Request.POST = function(options) { return 'post'; } + var _get = OpenLayers.Request.GET; + OpenLayers.Request.GET = function(options) { return 'get'; } + + var protocol = new OpenLayers.Protocol.HTTP({}); + + t.eq(protocol.read({}).priv, 'get', + 'readWithPOST is false by default'); + t.eq(protocol.read({readWithPOST: true}).priv, 'post', + 'readWithPOST can be set in read options'); + + var protocol = new OpenLayers.Protocol.HTTP({readWithPOST: true}); + + t.eq(protocol.read({}).priv, 'post', + 'readWithPOST can be set in constructor'); + t.eq(protocol.read({readWithPOST: false}).priv, 'get', + 'readWithPOST can be overridden in read options'); + + OpenLayers.Request.POST = _post; + OpenLayers.Request.GET = _get; + } + function test_read_bbox(t) { t.plan(1); var protocol = new OpenLayers.Protocol.HTTP();