Skip to content

Commit

Permalink
add test for fixing the post/patch async operation
Browse files Browse the repository at this point in the history
  • Loading branch information
Qinyuan Wan committed Mar 23, 2016
1 parent f058543 commit b789377
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 6 deletions.
8 changes: 4 additions & 4 deletions ClientRuntimes/NodeJS/ms-rest-azure/lib/azureServiceClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ AzureServiceClient.prototype.getPutOrPatchOperationResult = function(resultOfIni
self._updateStateFromLocationHeader(pollingState, function(err) {
return callback(err);
});
} else if (resultOfInitialRequest.request.method === "PUT") {
} else if (resultOfInitialRequest.request.method === 'PUT') {
self._updateStateFromGetResourceOperation(resourceUrl, pollingState, function(err) {
return callback(err);
});
Expand All @@ -127,7 +127,7 @@ AzureServiceClient.prototype.getPutOrPatchOperationResult = function(resultOfIni
//when done
function(err) {
if (pollingState.status === LroStates.Succeeded) {
if ((pollingState.azureAsyncOperationHeaderLink || !pollingState.resource) && resultOfInitialRequest.request.method !== "DELETE" && resultOfInitialRequest.request.method !== "POST") {
if ((pollingState.azureAsyncOperationHeaderLink || !pollingState.resource) && resultOfInitialRequest.request.method !== 'DELETE' && resultOfInitialRequest.request.method !== 'POST') {
self._updateStateFromGetResourceOperation(resourceUrl, pollingState, function(err) {
return callback(err, pollingState.getOperationResponse());
});
Expand Down Expand Up @@ -201,7 +201,7 @@ AzureServiceClient.prototype.getPostOrDeleteOperationResult = function(resultOfI
self._updateStateFromLocationHeader(pollingState, function(err) {
return callback(err);
});
} else if (resultOfInitialRequest.request.method === "PUT") {
} else if (resultOfInitialRequest.request.method === 'PUT') {
self._updateStateFromGetResourceOperation(resourceUrl, pollingState, function(err) {
return callback(err);
});
Expand All @@ -213,7 +213,7 @@ AzureServiceClient.prototype.getPostOrDeleteOperationResult = function(resultOfI
//when done
function(err) {
if (pollingState.status === LroStates.Succeeded) {
if ((pollingState.azureAsyncOperationHeaderLink || !pollingState.resource) && resultOfInitialRequest.request.method !== "DELETE" && resultOfInitialRequest.request.method !== "POST") {
if ((pollingState.azureAsyncOperationHeaderLink || !pollingState.resource) && resultOfInitialRequest.request.method !== 'DELETE' && resultOfInitialRequest.request.method !== 'POST') {
self._updateStateFromGetResourceOperation(resourceUrl, pollingState, function(err) {
return callback(err, pollingState.getOperationResponse());
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,11 +156,56 @@ describe('AzureServiceClient', function () {
});
});

describe('Patch', function () {
resultOfInitialRequest.response.statusCode = 202;
resultOfInitialRequest.body.properties.provisioningState = LroStates.Succeeded;

it('works by polling from azure-asyncoperation header', function (done) {
resultOfInitialRequest.response.headers['azure-asyncoperation'] = '';
resultOfInitialRequest.response.headers['location'] = urlFromLocationHeader_Return200;
client.getPutOrPatchOperationResult(resultOfInitialRequest, function (err, result) {
should.not.exist(err);
JSON.parse(result.body).name.should.equal(testResourceName);
should.exist(result.response.randomFieldFromPollLocationHeader);
done();
});
});

it('works by polling from location header', function (done) {
resultOfInitialRequest.response.headers['azure-asyncoperation'] = urlFromAzureAsyncOPHeader_Return200;
resultOfInitialRequest.response.headers['location'] = '';
client.getPutOrPatchOperationResult(resultOfInitialRequest, function (err, result) {
should.not.exist(err);
JSON.parse(result.body).name.should.equal(testResourceName);
done();
});
});

it('returns error if failed to poll from the azure-asyncoperation header', function (done) {
resultOfInitialRequest.response.headers['azure-asyncoperation'] = url_ReturnError;
resultOfInitialRequest.response.headers['location'] = '';
client.getPutOrPatchOperationResult(resultOfInitialRequest, function (err, result) {
err.message.should.containEql(testError);
done();
});
});

it('returns error if failed to poll from the location header', function (done) {
resultOfInitialRequest.response.headers['azure-asyncoperation'] = '';
resultOfInitialRequest.response.headers['location'] = url_ReturnError;
client.getPutOrPatchOperationResult(resultOfInitialRequest, function (err, result) {
err.message.should.containEql(testError);
done();
});
});
});

describe('Post-or-Delete', function () {
resultOfInitialRequest.response.statusCode = 202;

resultOfInitialRequest.body.properties.provisioningState = LroStates.Succeeded;

it('throw on not Lro related status code', function (done) {
client.getPostOrDeleteOperationResult({ response: { statusCode: 201 } }, function (err, result) {
client.getPostOrDeleteOperationResult({ response: { statusCode: 203 }, request: {url: url_resource}}, function (err, result) {
err.message.should.containEql('Unexpected polling status code from long running operation');
done();
});
Expand All @@ -169,6 +214,7 @@ describe('AzureServiceClient', function () {
it('works by polling from the azure-asyncoperation header', function (done) {
resultOfInitialRequest.response.headers['azure-asyncoperation'] = urlFromAzureAsyncOPHeader_Return200;
resultOfInitialRequest.response.headers['location'] = '';
resultOfInitialRequest.request.method = 'POST';
client.getPostOrDeleteOperationResult(resultOfInitialRequest, function (err, result) {
should.not.exist(err);
should.exist(result.response.randomFieldFromPollAsyncOpHeader);
Expand Down

0 comments on commit b789377

Please sign in to comment.