Skip to content

Commit

Permalink
Merge pull request #28837 from owncloud/stable10-fix-oc-backbone-webd…
Browse files Browse the repository at this point in the history
…av-report

[stable10] Fix REPORT method when going through Backbone-Webdav adapter
  • Loading branch information
Vincent Petry authored Aug 29, 2017
2 parents de6e2cd + 26fa079 commit 50d2f4a
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 2 deletions.
12 changes: 10 additions & 2 deletions core/js/oc-backbone-webdav.js
Original file line number Diff line number Diff line change
Expand Up @@ -252,12 +252,20 @@
}

function callMethod(client, options, model, headers) {
headers['Content-Type'] = 'application/json';
var data = options.data;
if (_.isObject(data)) {
headers['Content-Type'] = 'application/json';
data = JSON.stringify(data);
} else if (_.isString(data) && data.substr(0, 6) === '<?xml ') {
headers['Content-Type'] = 'application/xml';
} else {
headers['Content-Type'] = 'text/plain';
}
return client.request(
options.type,
options.url,
headers,
JSON.stringify(options.data)
data
).then(function(result) {
if (!isSuccessStatus(result.status)) {
if (_.isFunction(options.error)) {
Expand Down
67 changes: 67 additions & 0 deletions core/js/tests/specs/oc-backbone-webdavSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -898,5 +898,72 @@ describe('Backbone Webdav extension', function() {
expect(model.isNew()).toEqual(false);
});
});

describe('custom method', function() {
var TestModel;

beforeEach(function() {
TestModel = OC.Backbone.Model.extend({
url: 'http://example.com/owncloud/remote.php/test/1',
sync: OC.Backbone.davSync,
customCall: function(data, options) {
options = _.extend({}, options);
options.data = data;
return this.sync('REPORT', this, options);
}
});
});

it('serializes JSON if a JS object is passed as data', function() {
var model = new TestModel();
model.customCall({someData: '123'});

expect(davClientRequestStub.calledOnce).toEqual(true);
expect(davClientRequestStub.getCall(0).args[0])
.toEqual('REPORT');
expect(davClientRequestStub.getCall(0).args[1])
.toEqual('http://example.com/owncloud/remote.php/test/1');
expect(davClientRequestStub.getCall(0).args[2]['Content-Type'])
.toEqual('application/json');
expect(davClientRequestStub.getCall(0).args[2]['X-Requested-With'])
.toEqual('XMLHttpRequest');
expect(davClientRequestStub.getCall(0).args[3])
.toEqual(JSON.stringify({someData: '123'}));
});
it('does not serializes JSON if a string is passed as data', function() {
var model = new TestModel();
model.customCall('whatever');

expect(davClientRequestStub.calledOnce).toEqual(true);
expect(davClientRequestStub.getCall(0).args[0])
.toEqual('REPORT');
expect(davClientRequestStub.getCall(0).args[1])
.toEqual('http://example.com/owncloud/remote.php/test/1');
expect(davClientRequestStub.getCall(0).args[2]['Content-Type'])
.toEqual('text/plain');
expect(davClientRequestStub.getCall(0).args[2]['X-Requested-With'])
.toEqual('XMLHttpRequest');
expect(davClientRequestStub.getCall(0).args[3])
.toEqual('whatever');
});
it('sends XML mime type when passed data string is XML', function() {
var model = new TestModel();
var body = '<?xml version="1.0" encoding="utf-8" ?>';
body += '<root></root>';
model.customCall(body);

expect(davClientRequestStub.calledOnce).toEqual(true);
expect(davClientRequestStub.getCall(0).args[0])
.toEqual('REPORT');
expect(davClientRequestStub.getCall(0).args[1])
.toEqual('http://example.com/owncloud/remote.php/test/1');
expect(davClientRequestStub.getCall(0).args[2]['Content-Type'])
.toEqual('application/xml');
expect(davClientRequestStub.getCall(0).args[2]['X-Requested-With'])
.toEqual('XMLHttpRequest');
expect(davClientRequestStub.getCall(0).args[3])
.toEqual(body);
});
});
});

0 comments on commit 50d2f4a

Please sign in to comment.