Skip to content

Commit

Permalink
Merge pull request #359 from jgravois/task-cors-check
Browse files Browse the repository at this point in the history
Task cors check
  • Loading branch information
patrickarlt committed Oct 11, 2014
2 parents 4e121dc + 16cdbf9 commit a2f41e6
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 3 deletions.
12 changes: 12 additions & 0 deletions spec/Tasks/FindSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,4 +194,16 @@ describe('L.esri.Tasks.Find', function () {

server.respond();
});

it('should use JSONP to execute without a service', function(done){
var myTask = L.esri.Tasks.find(url, {useCors:false});

var request = myTask.layers('0').text('Site').run(function(error, featureCollection, raw){
expect(featureCollection).to.deep.equal(sampleFeatureCollection);
expect(raw).to.deep.equal(sampleResponse);
done();
});

L.esri._callback[request.id](sampleResponse);
});
});
66 changes: 66 additions & 0 deletions spec/Tasks/QuerySpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -478,4 +478,70 @@ describe('L.esri.Tasks.Query', function () {
server.respond();
});

it('should make GET queries with no service', function(done){
server.respondWith('GET', mapServiceUrl + '0/query?returnGeometry=true&where=1%3D1&outSr=4326&outFields=*&f=json', JSON.stringify(sampleMapServiceQueryResponse));

var queryTask = new L.esri.Tasks.Query(mapServiceUrl + '0');

var request = queryTask.where("1=1").run(function(error, featureCollection, raw){
expect(featureCollection).to.deep.equal(sampleMapServiceCollection);
expect(raw).to.deep.equal(sampleMapServiceQueryResponse);
done();
});

server.respond();
});

it('query tasks without services should make GET requests w/ JSONP', function(done){
var queryTask = new L.esri.Tasks.Query(mapServiceUrl + '0');
queryTask.options.useCors = false;

var request = queryTask.where("1=1").run(function(error, featureCollection, raw){
expect(featureCollection).to.deep.equal(sampleMapServiceCollection);
expect(raw).to.deep.equal(sampleMapServiceQueryResponse);
done();
});

L.esri._callback[request.id](sampleMapServiceQueryResponse);
});

it('query tasks without services should make POST requests', function(done){
server.respondWith('POST', mapServiceUrl + '0/query', JSON.stringify(sampleMapServiceQueryResponse));
var queryTask = new L.esri.Tasks.Query(mapServiceUrl + '0');
var request = queryTask.where(
"this is a dumb way to make sure the request is more than 2000 characters" +
"this is a dumb way to make sure the request is more than 2000 characters" +
"this is a dumb way to make sure the request is more than 2000 characters" +
"this is a dumb way to make sure the request is more than 2000 characters" +
"this is a dumb way to make sure the request is more than 2000 characters" +
"this is a dumb way to make sure the request is more than 2000 characters" +
"this is a dumb way to make sure the request is more than 2000 characters" +
"this is a dumb way to make sure the request is more than 2000 characters" +
"this is a dumb way to make sure the request is more than 2000 characters" +
"this is a dumb way to make sure the request is more than 2000 characters" +
"this is a dumb way to make sure the request is more than 2000 characters" +
"this is a dumb way to make sure the request is more than 2000 characters" +
"this is a dumb way to make sure the request is more than 2000 characters" +
"this is a dumb way to make sure the request is more than 2000 characters" +
"this is a dumb way to make sure the request is more than 2000 characters" +
"this is a dumb way to make sure the request is more than 2000 characters" +
"this is a dumb way to make sure the request is more than 2000 characters" +
"this is a dumb way to make sure the request is more than 2000 characters" +
"this is a dumb way to make sure the request is more than 2000 characters" +
"this is a dumb way to make sure the request is more than 2000 characters" +
"this is a dumb way to make sure the request is more than 2000 characters" +
"this is a dumb way to make sure the request is more than 2000 characters" +
"this is a dumb way to make sure the request is more than 2000 characters" +
"this is a dumb way to make sure the request is more than 2000 characters" +
"this is a dumb way to make sure the request is more than 2000 characters" +
"this is a dumb way to make sure the request is more than 2000 characters").
run(function(error, featureCollection, raw){
expect(featureCollection).to.deep.equal(sampleMapServiceCollection);
expect(raw).to.deep.equal(sampleMapServiceQueryResponse);
done();
});

server.respond();
});

});
21 changes: 18 additions & 3 deletions src/Tasks/Task.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
L.esri.Tasks.Task = L.Class.extend({

options: {
useCors: true
},

generateSetter: function(param, context){
var isArray = param.match(/([a-zA-Z]+)\[\]/);

Expand All @@ -21,7 +26,7 @@ L.esri.Tasks.Task = L.Class.extend({
}, context);
}
},
initialize: function(endpoint){
initialize: function(endpoint, options){
if(endpoint.url && endpoint.get){
this._service = endpoint;
this.url = endpoint.url;
Expand All @@ -37,6 +42,8 @@ L.esri.Tasks.Task = L.Class.extend({
this[setter] = this.generateSetter(param, this);
}
}

L.Util.setOptions(this, options);
},
token: function(token){
if(this._service){
Expand All @@ -50,7 +57,15 @@ L.esri.Tasks.Task = L.Class.extend({
if(this._service){
return this._service.request(this.path, this.params, callback, context);
} else {
return L.esri.request(this.url + this.path, this.params, callback, context);
return this._request('request', this.path, this.params, callback, context);
}
},
_request: function(method, path, params, callback, context){
var url = (this.options.proxy) ? this.options.proxy + '?' + this.url + path : this.url + path;
if((method === 'get' || method === 'request') && !this.options.useCors){
return L.esri.Request.get.JSONP(url, params, callback, context);
} else{
return L.esri[method](url, params, callback, context);
}
}
});
});

0 comments on commit a2f41e6

Please sign in to comment.