Skip to content

Commit

Permalink
Added a LRO acceptance test to verify that a CloudException is thrown…
Browse files Browse the repository at this point in the history
… when a JSONException occurs during Deserialization of the response content
  • Loading branch information
John-Hart committed Apr 19, 2016
1 parent 3622e2b commit d41d44c
Show file tree
Hide file tree
Showing 6 changed files with 357 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,10 @@ public void LroSadPathTests()
Assert.Equal("Error from the server", exception.Body.Message);
Assert.NotNull(exception.Request);
Assert.NotNull(exception.Response);
exception =
Assert.Throws<CloudException>(() => client.LROSADs.PutNonRetry201Creating400InvalidJson(new Product { Location = "West US" }));
Assert.Null(exception.Body);
Assert.Equal("Long running operation failed with status 'BadRequest'.", exception.Message);
exception =
Assert.Throws<CloudException>(
() => client.LROSADs.PutAsyncRelativeRetry400(new Product {Location = "West US"}));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,34 @@ public partial interface ILROSADsOperations
/// </param>
Task<AzureOperationResponse<Product>> BeginPutNonRetry201Creating400WithHttpMessagesAsync(Product product = default(Product), Dictionary<string, List<string>> customHeaders = null, CancellationToken cancellationToken = default(CancellationToken));
/// <summary>
/// Long running put request, service returns a Product with
/// 'ProvisioningState' = 'Creating' and 201 response code
/// </summary>
/// <param name='product'>
/// Product to put
/// </param>
/// <param name='customHeaders'>
/// The headers that will be added to request.
/// </param>
/// <param name='cancellationToken'>
/// The cancellation token.
/// </param>
Task<AzureOperationResponse<Product>> PutNonRetry201Creating400InvalidJsonWithHttpMessagesAsync(Product product = default(Product), Dictionary<string, List<string>> customHeaders = null, CancellationToken cancellationToken = default(CancellationToken));
/// <summary>
/// Long running put request, service returns a Product with
/// 'ProvisioningState' = 'Creating' and 201 response code
/// </summary>
/// <param name='product'>
/// Product to put
/// </param>
/// <param name='customHeaders'>
/// The headers that will be added to request.
/// </param>
/// <param name='cancellationToken'>
/// The cancellation token.
/// </param>
Task<AzureOperationResponse<Product>> BeginPutNonRetry201Creating400InvalidJsonWithHttpMessagesAsync(Product product = default(Product), Dictionary<string, List<string>> customHeaders = null, CancellationToken cancellationToken = default(CancellationToken));
/// <summary>
/// Long running put request, service returns a 200 with
/// ProvisioningState=’Creating’. Poll the endpoint indicated in the
/// Azure-AsyncOperation header for operation status
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,209 @@ internal LROSADsOperations(AutoRestLongRunningOperationTestService client)
return _result;
}

/// <summary>
/// Long running put request, service returns a Product with
/// 'ProvisioningState' = 'Creating' and 201 response code
/// </summary>
/// <param name='product'>
/// Product to put
/// </param>
/// <param name='customHeaders'>
/// The headers that will be added to request.
/// </param>
/// <param name='cancellationToken'>
/// The cancellation token.
/// </param>
public async Task<AzureOperationResponse<Product>> PutNonRetry201Creating400InvalidJsonWithHttpMessagesAsync(Product product = default(Product), Dictionary<string, List<string>> customHeaders = null, CancellationToken cancellationToken = default(CancellationToken))
{
// Send Request
AzureOperationResponse<Product> _response = await BeginPutNonRetry201Creating400InvalidJsonWithHttpMessagesAsync(
product, customHeaders, cancellationToken);
return await this.Client.GetPutOrPatchOperationResultAsync(_response,
customHeaders,
cancellationToken);
}

/// <summary>
/// Long running put request, service returns a Product with
/// 'ProvisioningState' = 'Creating' and 201 response code
/// </summary>
/// <param name='product'>
/// Product to put
/// </param>
/// <param name='customHeaders'>
/// Headers that will be added to request.
/// </param>
/// <param name='cancellationToken'>
/// The cancellation token.
/// </param>
/// <return>
/// A response object containing the response body and response headers.
/// </return>
public async Task<AzureOperationResponse<Product>> BeginPutNonRetry201Creating400InvalidJsonWithHttpMessagesAsync(Product product = default(Product), Dictionary<string, List<string>> customHeaders = null, CancellationToken cancellationToken = default(CancellationToken))
{
// Tracing
bool _shouldTrace = ServiceClientTracing.IsEnabled;
string _invocationId = null;
if (_shouldTrace)
{
_invocationId = ServiceClientTracing.NextInvocationId.ToString();
Dictionary<string, object> tracingParameters = new Dictionary<string, object>();
tracingParameters.Add("product", product);
tracingParameters.Add("cancellationToken", cancellationToken);
ServiceClientTracing.Enter(_invocationId, this, "BeginPutNonRetry201Creating400InvalidJson", tracingParameters);
}
// Construct URL
var _baseUrl = this.Client.BaseUri.AbsoluteUri;
var _url = new Uri(new Uri(_baseUrl + (_baseUrl.EndsWith("/") ? "" : "/")), "lro/nonretryerror/put/201/creating/400/invalidjson").ToString();
List<string> _queryParameters = new List<string>();
if (_queryParameters.Count > 0)
{
_url += "?" + string.Join("&", _queryParameters);
}
// Create HTTP transport objects
HttpRequestMessage _httpRequest = new HttpRequestMessage();
HttpResponseMessage _httpResponse = null;
_httpRequest.Method = new HttpMethod("PUT");
_httpRequest.RequestUri = new Uri(_url);
// Set Headers
if (this.Client.GenerateClientRequestId != null && this.Client.GenerateClientRequestId.Value)
{
_httpRequest.Headers.TryAddWithoutValidation("x-ms-client-request-id", Guid.NewGuid().ToString());
}
if (this.Client.AcceptLanguage != null)
{
if (_httpRequest.Headers.Contains("accept-language"))
{
_httpRequest.Headers.Remove("accept-language");
}
_httpRequest.Headers.TryAddWithoutValidation("accept-language", this.Client.AcceptLanguage);
}
if (customHeaders != null)
{
foreach(var _header in customHeaders)
{
if (_httpRequest.Headers.Contains(_header.Key))
{
_httpRequest.Headers.Remove(_header.Key);
}
_httpRequest.Headers.TryAddWithoutValidation(_header.Key, _header.Value);
}
}

// Serialize Request
string _requestContent = null;
if(product != null)
{
_requestContent = SafeJsonConvert.SerializeObject(product, this.Client.SerializationSettings);
_httpRequest.Content = new StringContent(_requestContent, Encoding.UTF8);
_httpRequest.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json; charset=utf-8");
}
// Set Credentials
if (this.Client.Credentials != null)
{
cancellationToken.ThrowIfCancellationRequested();
await this.Client.Credentials.ProcessHttpRequestAsync(_httpRequest, cancellationToken).ConfigureAwait(false);
}
// Send Request
if (_shouldTrace)
{
ServiceClientTracing.SendRequest(_invocationId, _httpRequest);
}
cancellationToken.ThrowIfCancellationRequested();
_httpResponse = await this.Client.HttpClient.SendAsync(_httpRequest, cancellationToken).ConfigureAwait(false);
if (_shouldTrace)
{
ServiceClientTracing.ReceiveResponse(_invocationId, _httpResponse);
}
HttpStatusCode _statusCode = _httpResponse.StatusCode;
cancellationToken.ThrowIfCancellationRequested();
string _responseContent = null;
if ((int)_statusCode != 200 && (int)_statusCode != 201)
{
var ex = new CloudException(string.Format("Operation returned an invalid status code '{0}'", _statusCode));
try
{
_responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);
CloudError _errorBody = SafeJsonConvert.DeserializeObject<CloudError>(_responseContent, this.Client.DeserializationSettings);
if (_errorBody != null)
{
ex = new CloudException(_errorBody.Message);
ex.Body = _errorBody;
}
}
catch (JsonException)
{
// Ignore the exception
}
ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent);
ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent);
if (_httpResponse.Headers.Contains("x-ms-request-id"))
{
ex.RequestId = _httpResponse.Headers.GetValues("x-ms-request-id").FirstOrDefault();
}
if (_shouldTrace)
{
ServiceClientTracing.Error(_invocationId, ex);
}
_httpRequest.Dispose();
if (_httpResponse != null)
{
_httpResponse.Dispose();
}
throw ex;
}
// Create Result
var _result = new AzureOperationResponse<Product>();
_result.Request = _httpRequest;
_result.Response = _httpResponse;
if (_httpResponse.Headers.Contains("x-ms-request-id"))
{
_result.RequestId = _httpResponse.Headers.GetValues("x-ms-request-id").FirstOrDefault();
}
// Deserialize Response
if ((int)_statusCode == 200)
{
_responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);
try
{
_result.Body = SafeJsonConvert.DeserializeObject<Product>(_responseContent, this.Client.DeserializationSettings);
}
catch (JsonException ex)
{
_httpRequest.Dispose();
if (_httpResponse != null)
{
_httpResponse.Dispose();
}
throw new SerializationException("Unable to deserialize the response.", _responseContent, ex);
}
}
// Deserialize Response
if ((int)_statusCode == 201)
{
_responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);
try
{
_result.Body = SafeJsonConvert.DeserializeObject<Product>(_responseContent, this.Client.DeserializationSettings);
}
catch (JsonException ex)
{
_httpRequest.Dispose();
if (_httpResponse != null)
{
_httpResponse.Dispose();
}
throw new SerializationException("Unable to deserialize the response.", _responseContent, ex);
}
}
if (_shouldTrace)
{
ServiceClientTracing.Exit(_invocationId, _result);
}
return _result;
}

/// <summary>
/// Long running put request, service returns a 200 with
/// ProvisioningState=’Creating’. Poll the endpoint indicated in the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,78 @@ public static partial class LROSADsOperationsExtensions
}
}

/// <summary>
/// Long running put request, service returns a Product with
/// 'ProvisioningState' = 'Creating' and 201 response code
/// </summary>
/// <param name='operations'>
/// The operations group for this extension method.
/// </param>
/// <param name='product'>
/// Product to put
/// </param>
public static Product PutNonRetry201Creating400InvalidJson(this ILROSADsOperations operations, Product product = default(Product))
{
return Task.Factory.StartNew(s => ((ILROSADsOperations)s).PutNonRetry201Creating400InvalidJsonAsync(product), operations, CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default).Unwrap().GetAwaiter().GetResult();
}

/// <summary>
/// Long running put request, service returns a Product with
/// 'ProvisioningState' = 'Creating' and 201 response code
/// </summary>
/// <param name='operations'>
/// The operations group for this extension method.
/// </param>
/// <param name='product'>
/// Product to put
/// </param>
/// <param name='cancellationToken'>
/// The cancellation token.
/// </param>
public static async Task<Product> PutNonRetry201Creating400InvalidJsonAsync(this ILROSADsOperations operations, Product product = default(Product), CancellationToken cancellationToken = default(CancellationToken))
{
using (var _result = await operations.PutNonRetry201Creating400InvalidJsonWithHttpMessagesAsync(product, null, cancellationToken).ConfigureAwait(false))
{
return _result.Body;
}
}

/// <summary>
/// Long running put request, service returns a Product with
/// 'ProvisioningState' = 'Creating' and 201 response code
/// </summary>
/// <param name='operations'>
/// The operations group for this extension method.
/// </param>
/// <param name='product'>
/// Product to put
/// </param>
public static Product BeginPutNonRetry201Creating400InvalidJson(this ILROSADsOperations operations, Product product = default(Product))
{
return Task.Factory.StartNew(s => ((ILROSADsOperations)s).BeginPutNonRetry201Creating400InvalidJsonAsync(product), operations, CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default).Unwrap().GetAwaiter().GetResult();
}

/// <summary>
/// Long running put request, service returns a Product with
/// 'ProvisioningState' = 'Creating' and 201 response code
/// </summary>
/// <param name='operations'>
/// The operations group for this extension method.
/// </param>
/// <param name='product'>
/// Product to put
/// </param>
/// <param name='cancellationToken'>
/// The cancellation token.
/// </param>
public static async Task<Product> BeginPutNonRetry201Creating400InvalidJsonAsync(this ILROSADsOperations operations, Product product = default(Product), CancellationToken cancellationToken = default(CancellationToken))
{
using (var _result = await operations.BeginPutNonRetry201Creating400InvalidJsonWithHttpMessagesAsync(product, null, cancellationToken).ConfigureAwait(false))
{
return _result.Body;
}
}

/// <summary>
/// Long running put request, service returns a 200 with
/// ProvisioningState=’Creating’. Poll the endpoint indicated in the
Expand Down
10 changes: 10 additions & 0 deletions AutoRest/TestServer/server/routes/lros.js
Original file line number Diff line number Diff line change
Expand Up @@ -940,6 +940,16 @@ var lros = function (coverage) {
res.status(400).end('{ "message" : "Error from the server" }');
});

coverage['LRONonRetryPut201Creating400InvalidJson'] = 0;
router.put('/nonretryerror/put/201/creating/400/invalidjson', function (req, res, next) {
res.status(201).end('{ "properties": { "provisioningState": "Creating"}, "id": "100", "name": "foo" }');
});

router.get('/nonretryerror/put/201/creating/400/invalidjson', function (req, res, next) {
coverage['LRONonRetryPut201Creating400InvalidJson']++;
res.status(400).end('<{ "message" : "Error from the server" }');
});

coverage['LRONonRetryPutAsyncRetry400'] = 0;
router.put('/nonretryerror/putasync/retry/400', function (req, res, next) {
var pollingUri = 'http://localhost.:' + utils.getPort() + '/lro/nonretryerror/putasync/retry/failed/operationResults/400';
Expand Down
40 changes: 40 additions & 0 deletions AutoRest/TestServer/swagger/lro.json
Original file line number Diff line number Diff line change
Expand Up @@ -1772,6 +1772,46 @@
}
}
}
},
"/lro/nonretryerror/put/201/creating/400/invalidjson": {
"put": {
"x-ms-long-running-operation": true,
"operationId": "LROSADs_putNonRetry201Creating400InvalidJson",
"description": "Long running put request, service returns a Product with 'ProvisioningState' = 'Creating' and 201 response code",
"tags": [
"LROSAD Operations"
],
"parameters": [
{
"name": "product",
"description": "Product to put",
"in": "body",
"schema": {
"$ref": "#/definitions/Product"
}
}
],
"responses": {
"200": {
"description": "Response after completion, with ProvisioningState='Succeeded'",
"schema": {
"$ref": "#/definitions/Product"
}
},
"201": {
"description": "Initial response, with ProvisioningState = 'Creating'",
"schema": {
"$ref": "#/definitions/Product"
}
},
"default": {
"description": "Unexpected error",
"schema": {
"$ref": "#/definitions/CloudError"
}
}
}
}
},
"/lro/nonretryerror/putasync/retry/400": {
"put": {
Expand Down

0 comments on commit d41d44c

Please sign in to comment.