Skip to content

Commit 7f1e2cb

Browse files
authored
Merge pull request #28836 from owncloud/fix-oc-backbone-webdav-report
Fix REPORT method when going through Backbone-Webdav adapter
2 parents 0ef08be + 3c06153 commit 7f1e2cb

File tree

2 files changed

+77
-2
lines changed

2 files changed

+77
-2
lines changed

core/js/oc-backbone-webdav.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -252,12 +252,20 @@
252252
}
253253

254254
function callMethod(client, options, model, headers) {
255-
headers['Content-Type'] = 'application/json';
255+
var data = options.data;
256+
if (_.isObject(data)) {
257+
headers['Content-Type'] = 'application/json';
258+
data = JSON.stringify(data);
259+
} else if (_.isString(data) && data.substr(0, 6) === '<?xml ') {
260+
headers['Content-Type'] = 'application/xml';
261+
} else {
262+
headers['Content-Type'] = 'text/plain';
263+
}
256264
return client.request(
257265
options.type,
258266
options.url,
259267
headers,
260-
JSON.stringify(options.data)
268+
data
261269
).then(function(result) {
262270
if (!isSuccessStatus(result.status)) {
263271
if (_.isFunction(options.error)) {

core/js/tests/specs/oc-backbone-webdavSpec.js

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -898,5 +898,72 @@ describe('Backbone Webdav extension', function() {
898898
expect(model.isNew()).toEqual(false);
899899
});
900900
});
901+
902+
describe('custom method', function() {
903+
var TestModel;
904+
905+
beforeEach(function() {
906+
TestModel = OC.Backbone.Model.extend({
907+
url: 'http://example.com/owncloud/remote.php/test/1',
908+
sync: OC.Backbone.davSync,
909+
customCall: function(data, options) {
910+
options = _.extend({}, options);
911+
options.data = data;
912+
return this.sync('REPORT', this, options);
913+
}
914+
});
915+
});
916+
917+
it('serializes JSON if a JS object is passed as data', function() {
918+
var model = new TestModel();
919+
model.customCall({someData: '123'});
920+
921+
expect(davClientRequestStub.calledOnce).toEqual(true);
922+
expect(davClientRequestStub.getCall(0).args[0])
923+
.toEqual('REPORT');
924+
expect(davClientRequestStub.getCall(0).args[1])
925+
.toEqual('http://example.com/owncloud/remote.php/test/1');
926+
expect(davClientRequestStub.getCall(0).args[2]['Content-Type'])
927+
.toEqual('application/json');
928+
expect(davClientRequestStub.getCall(0).args[2]['X-Requested-With'])
929+
.toEqual('XMLHttpRequest');
930+
expect(davClientRequestStub.getCall(0).args[3])
931+
.toEqual(JSON.stringify({someData: '123'}));
932+
});
933+
it('does not serializes JSON if a string is passed as data', function() {
934+
var model = new TestModel();
935+
model.customCall('whatever');
936+
937+
expect(davClientRequestStub.calledOnce).toEqual(true);
938+
expect(davClientRequestStub.getCall(0).args[0])
939+
.toEqual('REPORT');
940+
expect(davClientRequestStub.getCall(0).args[1])
941+
.toEqual('http://example.com/owncloud/remote.php/test/1');
942+
expect(davClientRequestStub.getCall(0).args[2]['Content-Type'])
943+
.toEqual('text/plain');
944+
expect(davClientRequestStub.getCall(0).args[2]['X-Requested-With'])
945+
.toEqual('XMLHttpRequest');
946+
expect(davClientRequestStub.getCall(0).args[3])
947+
.toEqual('whatever');
948+
});
949+
it('sends XML mime type when passed data string is XML', function() {
950+
var model = new TestModel();
951+
var body = '<?xml version="1.0" encoding="utf-8" ?>';
952+
body += '<root></root>';
953+
model.customCall(body);
954+
955+
expect(davClientRequestStub.calledOnce).toEqual(true);
956+
expect(davClientRequestStub.getCall(0).args[0])
957+
.toEqual('REPORT');
958+
expect(davClientRequestStub.getCall(0).args[1])
959+
.toEqual('http://example.com/owncloud/remote.php/test/1');
960+
expect(davClientRequestStub.getCall(0).args[2]['Content-Type'])
961+
.toEqual('application/xml');
962+
expect(davClientRequestStub.getCall(0).args[2]['X-Requested-With'])
963+
.toEqual('XMLHttpRequest');
964+
expect(davClientRequestStub.getCall(0).args[3])
965+
.toEqual(body);
966+
});
967+
});
901968
});
902969

0 commit comments

Comments
 (0)