Skip to content
This repository has been archived by the owner on Jul 17, 2024. It is now read-only.

Commit

Permalink
Offset 0 for profile service (avoid ma) (#4310)
Browse files Browse the repository at this point in the history
  • Loading branch information
gjn authored and oterral committed May 8, 2018
1 parent cb98691 commit 3460166
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 36 deletions.
54 changes: 19 additions & 35 deletions src/components/profile/ProfileService.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,11 @@ goog.require('ga_urlutils_service');
};

this.$get = function($q, $http, $timeout, $translate, measureFilter,
gaTimeFormatFilter, $window, gaUrlUtils, gaBrowserSniffer,
gaGlobalOptions, gaGeomUtils) {
gaTimeFormatFilter, $window, gaUrlUtils, gaGlobalOptions,
gaGeomUtils) {

var d3LibUrl = this.d3libUrl;
var profileUrl = this.profileUrl;
var isIE = !!gaBrowserSniffer.msie;

var ProfileChart = function(options) {
options = options || {};
Expand Down Expand Up @@ -285,45 +284,30 @@ goog.require('ga_urlutils_service');
cancel();
canceler = $q.defer();

var formData;
if (!isIE || gaBrowserSniffer.msie > 9) {
formData = new FormData();
formData.append('geom', wkt);
formData.append('elevation_models', elevationModel);
} else {
formData = {
geom: wkt,
elevation_models: elevationModel
};
formData = $.param(formData);
}
// We don't use FormData because it makes the validity of data
// untestable. FormData is always an ampty object in tests.
var data = $.param({
geom: wkt,
elevation_models: elevationModel,
offset: 0
});

var params = {
method: 'POST',
url: profileUrl,
data: formData,
var config = {
cache: true,
timeout: canceler.promise
timeout: canceler.promise,
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
};

if (!isIE || gaBrowserSniffer.msie > 9) {
params.transformRequest = angular.identity;
params.headers = {'Content-Type': undefined};
} else {
params.headers = {'Content-Type':
'application/x-www-form-urlencoded'};
}

return $http(params).then(function(response) {
var data = response.data;
return $http.post(profileUrl, data, config).then(function(resp) {
// When all the geometry is outside switzerland
if (!data.length) {
data = emptyData;
if (!resp.data.length) {
return emptyData;
}
return data;
return resp.data;
}, function(response) {
// If request is canceled, statuscode is 0 and we don't announce
// it
// If request is canceled, statuscode is 0 and we don't announce it
if (response.status !== 0) {
// Display an empty profile
return emptyData;
Expand Down
16 changes: 15 additions & 1 deletion test/specs/profile/ProfileService.spec.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
/* eslint-disable max-len */
describe('ga_profile_service', function() {
var gaProfile, $q, $rootScope, $httpBackend, gaTimeFormat, gaGlobalOptions, gaGeomUtils;
var gaProfile, $q, $rootScope, $http, $httpBackend, gaTimeFormat, gaGlobalOptions, gaGeomUtils;

beforeEach(function() {

inject(function($injector) {
gaProfile = $injector.get('gaProfile');
gaTimeFormat = $injector.get('gaTimeFormatFilter');
$httpBackend = $injector.get('$httpBackend');
$http = $injector.get('$http');
$rootScope = $injector.get('$rootScope');
$q = $injector.get('$q');
gaGeomUtils = $injector.get('gaGeomUtils');
Expand Down Expand Up @@ -129,6 +130,19 @@ describe('ga_profile_service', function() {
$httpBackend.flush();
});

it('send correct POST parameters', function() {
$httpBackend.expectPOST(profileUrl).respond(goodResultToFormat);
var spy = sinon.spy($http, 'post');
gaProfile.create(feature);
expect(spy.callCount).to.be(1);
expect(spy.args[0][1]).to.be('geom=%7B%22type%22%3A%22LineString%22%2C%22coordinates%22%3A%5B%5B0.0%2C0.0%5D%2C%5B1.0%2C0.0%5D%2C%5B1.0%2C1.0%5D%5D%7D&elevation_models=COMB&offset=0');
var config = spy.args[0][2];
expect(config.cache).to.be(true);
expect(config.timeout).to.be.a(Object);
expect(config.headers['Content-Type']).to.be('application/x-www-form-urlencoded');
$httpBackend.flush();
});

it('formats the data', function(done) {
$httpBackend.expectPOST(profileUrl).respond(goodResultToFormat);
gaProfile.create(feature2).then(function(profile) {
Expand Down

0 comments on commit 3460166

Please sign in to comment.