Skip to content

Commit f6f63f3

Browse files
fix error during parsing of a batch response that contains requests with alternate keys; #61
1 parent 0d70184 commit f6f63f3

File tree

4 files changed

+142
-3
lines changed

4 files changed

+142
-3
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -673,7 +673,7 @@ var request = {
673673
dynamicsWebApi.retrieveMultipleRequest(request).then(function (response) {
674674

675675
var deltaLink = response.oDataDeltaLink;
676-
//make other requests to Web API
676+
//make other requests to Web API
677677
//...
678678

679679
//(2) only retrieve changes:

lib/requests/helpers/parseResponse.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,9 @@ function parseBatchResponse(response, parseParams, requestNumber) {
158158
var entityUrl = /OData-EntityId.+/i.exec(batchResponse);
159159

160160
if (entityUrl && entityUrl.length) {
161-
result.push(/([0-9A-F]{8}[-]?([0-9A-F]{4}[-]?){3}[0-9A-F]{12})\)$/i.exec(entityUrl[0])[1]);
161+
var guidResult = /([0-9A-F]{8}[-]?([0-9A-F]{4}[-]?){3}[0-9A-F]{12})\)$/i.exec(entityUrl[0]);
162+
163+
result.push(guidResult ? guidResult[1] : undefined);
162164
}
163165
else {
164166
result.push(undefined);
@@ -215,5 +217,7 @@ module.exports = function parseResponse(response, responseHeaders, parseParams)
215217
}
216218
}
217219

220+
parseParams.length = 0;
221+
218222
return parseResult;
219-
};
223+
}

tests/main-tests.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4975,6 +4975,62 @@ describe("promises -", function () {
49754975
expect(scope.isDone()).to.be.true;
49764976
});
49774977
});
4978+
4979+
describe("upsert / upsert / upsert with alternate keys", function () {
4980+
var scope;
4981+
var rBody = mocks.data.batchUpsertUpsertUpsertWithAlternateKeys;
4982+
var rBodys = rBody.split('\n');
4983+
var checkBody = '';
4984+
for (var i = 0; i < rBodys.length; i++) {
4985+
checkBody += rBodys[i];
4986+
}
4987+
before(function () {
4988+
var response = mocks.responses.batchUpsertUpsertUpsertWithAlternateKeys;
4989+
scope = nock(mocks.webApiUrl + '$batch')
4990+
.filteringRequestBody(function (body) {
4991+
body = body.replace(/dwa_batch_[\d\w]{8}-[\d\w]{4}-[\d\w]{4}-[\d\w]{4}-[\d\w]{12}/g, 'dwa_batch_XXX');
4992+
body = body.replace(/changeset_[\d\w]{8}-[\d\w]{4}-[\d\w]{4}-[\d\w]{4}-[\d\w]{12}/g, 'changeset_XXX');
4993+
var bodys = body.split('\n');
4994+
4995+
var resultBody = '';
4996+
for (var i = 0; i < bodys.length; i++) {
4997+
resultBody += bodys[i];
4998+
}
4999+
return resultBody;
5000+
})
5001+
.post("", checkBody)
5002+
.reply(response.status, response.responseText, response.responseHeaders);
5003+
});
5004+
5005+
after(function () {
5006+
nock.cleanAll();
5007+
});
5008+
5009+
it("returns a correct response", function (done) {
5010+
dynamicsWebApiTest.startBatch();
5011+
5012+
dynamicsWebApiTest.upsert("key='key1'", "records", { firstname: "Test", lastname: "Batch!" });
5013+
dynamicsWebApiTest.upsert("key='key2'", "records", { firstname: "Test", lastname: "Batch!" });
5014+
dynamicsWebApiTest.upsert("key='key3'", "records", { firstname: "Test", lastname: "Batch!" });
5015+
5016+
dynamicsWebApiTest.executeBatch()
5017+
.then(function (object) {
5018+
expect(object.length).to.be.eq(3);
5019+
5020+
expect(object[0]).to.be.undefined;
5021+
expect(object[1]).to.be.undefined;
5022+
expect(object[2]).to.be.undefined;
5023+
5024+
done();
5025+
}).catch(function (object) {
5026+
done(object);
5027+
});
5028+
});
5029+
5030+
it("all requests have been made", function () {
5031+
expect(scope.isDone()).to.be.true;
5032+
});
5033+
});
49785034
});
49795035

49805036
describe("dynamicsWebApi.constructor -", function () {

tests/stubs.js

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,43 @@ var dataStubs = {
214214
'Accept: application/json\n' +
215215
'\n' +
216216
'--dwa_batch_XXX--',
217+
batchUpsertUpsertUpsertWithAlternateKeys:
218+
'--dwa_batch_XXX\n' +
219+
'Content-Type: multipart/mixed;boundary=changeset_XXX\n' +
220+
'\n' +
221+
'--changeset_XXX\n' +
222+
'Content-Type: application/http\n' +
223+
'Content-Transfer-Encoding: binary\n' +
224+
'Content-ID: 100001\n' +
225+
'\n' +
226+
'PATCH ' + webApiUrl + 'records(key=\'key1\') HTTP/1.1\n' +
227+
'Content-Type: application/json\n' +
228+
'\n' +
229+
'{"firstname":"Test","lastname":"Batch!"}\n' +
230+
'\n' +
231+
'--changeset_XXX\n' +
232+
'Content-Type: application/http\n' +
233+
'Content-Transfer-Encoding: binary\n' +
234+
'Content-ID: 100002\n' +
235+
'\n' +
236+
'PATCH ' + webApiUrl + 'records(key=\'key2\') HTTP/1.1\n' +
237+
'Content-Type: application/json\n' +
238+
'\n' +
239+
'{"firstname":"Test","lastname":"Batch!"}\n' +
240+
'\n' +
241+
'--changeset_XXX\n' +
242+
'Content-Type: application/http\n' +
243+
'Content-Transfer-Encoding: binary\n' +
244+
'Content-ID: 100003\n' +
245+
'\n' +
246+
'PATCH ' + webApiUrl + 'records(key=\'key3\') HTTP/1.1\n' +
247+
'Content-Type: application/json\n' +
248+
'\n' +
249+
'{"firstname":"Test","lastname":"Batch!"}\n' +
250+
'\n' +
251+
'--changeset_XXX--\n' +
252+
'\n' +
253+
'--dwa_batch_XXX--',
217254
batchUpdateDelete:
218255
'--dwa_batch_XXX\n' +
219256
'Content-Type: multipart/mixed;boundary=changeset_XXX\n' +
@@ -649,6 +686,48 @@ var responseStubs = {
649686
JSON.stringify(dataStubs.multiple2) + '\r\n' +
650687
'--batchresponse_8b19b76e-c553-4c4c-af9d-b5521bfda1ae--'
651688
},
689+
batchUpsertUpsertUpsertWithAlternateKeys: {
690+
status: 200,
691+
responseText:
692+
'--batchresponse_8b19b76e-c553-4c4c-af9d-b5521bfda1ae\r\n' +
693+
'Content-Type: multipart/mixed; boundary=changesetresponse_08f5ebfd-5cee-4b64-bc51-ee16c02d47bd\r\n' +
694+
'\r\n' +
695+
'--changesetresponse_08f5ebfd-5cee-4b64-bc51-ee16c02d47bd\r\n' +
696+
'Content-Type: application/http\r\n' +
697+
'Content-Transfer-Encoding: binary\r\n' +
698+
'Content-ID: 100001' +
699+
700+
'HTTP/1.1 204 No Content\r\n' +
701+
'OData-Version: 4.0\r\n' +
702+
'Location: https://url.com/api/data/v8.2/tests(key=\'key1\')\r\n' +
703+
'OData-EntityId: https://url.com/api/data/v8.2/tests(key=\'key1\')\r\n' +
704+
'\r\n' +
705+
'\r\n' +
706+
'--changesetresponse_08f5ebfd-5cee-4b64-bc51-ee16c02d47bd\r\n' +
707+
'Content-Type: application/http\r\n' +
708+
'Content-Transfer-Encoding: binary\r\n' +
709+
'Content-ID: 100002' +
710+
711+
'HTTP/1.1 204 No Content\r\n' +
712+
'OData-Version: 4.0\r\n' +
713+
'Location: https://url.com/api/data/v8.2/tests(key=\'key2\')\r\n' +
714+
'OData-EntityId: https://url.com/api/data/v8.2/tests(key=\'key2\')\r\n' +
715+
'\r\n' +
716+
'\r\n' +
717+
'--changesetresponse_08f5ebfd-5cee-4b64-bc51-ee16c02d47bd\r\n' +
718+
'Content-Type: application/http\r\n' +
719+
'Content-Transfer-Encoding: binary\r\n' +
720+
'Content-ID: 100003' +
721+
722+
'HTTP/1.1 204 No Content\r\n' +
723+
'OData-Version: 4.0\r\n' +
724+
'Location: https://url.com/api/data/v8.2/tests(key=\'key3\')\r\n' +
725+
'OData-EntityId: https://url.com/api/data/v8.2/tests(key=\'key3\')\r\n' +
726+
'\r\n' +
727+
'\r\n' +
728+
'--changesetresponse_08f5ebfd-5cee-4b64-bc51-ee16c02d47bd--\r\n' +
729+
'--batchresponse_8b19b76e-c553-4c4c-af9d-b5521bfda1ae--'
730+
},
652731
batchUpdateDelete: {
653732
status: 200,
654733
responseText:

0 commit comments

Comments
 (0)