0

我有一个单元测试,调用一个使用 Autorest 生成的代码来调用我的 Api 的服务。

我希望我的单元测试显示我的 Api 抛出的错误,但服务的错误处理中似乎存在错误。

我正在使用以下命令生成代码来使用我的 api。

autorest --input-file=https://mywebsite.com.au:4433/myapi/api-docs/v1/swagger.json --output-folder=generated --csharp --namespace=MyConnector

生成的“客户端代码”包含

    /// <param name='request'>
    /// </param>
    /// <param name='customHeaders'>
    /// Headers that will be added to request.
    /// </param>
    /// <param name='cancellationToken'>
    /// The cancellation token.
    /// </param>
    /// <exception cref="HttpOperationException">
    /// Thrown when the operation returned an invalid status code
    /// </exception>
    /// <exception cref="SerializationException">
    /// Thrown when unable to deserialize the response
    /// </exception>
    /// <return>
    /// A response object containing the response body and response headers.
    /// </return>
    public async Task<HttpOperationResponse<GetAvailableCarriersResponse>> GetAvailableCarriersByJobHeaderIdWithHttpMessagesAsync(GetAvailableCarriersRequest request = default(GetAvailableCarriersRequest), 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("request", request);
            tracingParameters.Add("cancellationToken", cancellationToken);
            ServiceClientTracing.Enter(_invocationId, this, "GetAvailableCarriersByJobHeaderId", tracingParameters);
        }
        // Construct URL
        var _baseUrl = BaseUri.AbsoluteUri;
        var _url = new System.Uri(new System.Uri(_baseUrl + (_baseUrl.EndsWith("/") ? "" : "/")), "api/shipping-management/Get-Available-Carriers").ToString();
        // Create HTTP transport objects
        var _httpRequest = new HttpRequestMessage();
        HttpResponseMessage _httpResponse = null;
        _httpRequest.Method = new HttpMethod("POST");
        _httpRequest.RequestUri = new System.Uri(_url);
        // Set Headers


        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(request != null)
        {
            _requestContent = SafeJsonConvert.SerializeObject(request, SerializationSettings);
            _httpRequest.Content = new StringContent(_requestContent, System.Text.Encoding.UTF8);
            _httpRequest.Content.Headers.ContentType =System.Net.Http.Headers.MediaTypeHeaderValue.Parse("application/json-patch+json; charset=utf-8");
        }
        // Send Request
        if (_shouldTrace)
        {
            ServiceClientTracing.SendRequest(_invocationId, _httpRequest);
        }
        cancellationToken.ThrowIfCancellationRequested();
        _httpResponse = await 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)
        {
            var ex = new HttpOperationException(string.Format("Operation returned an invalid status code '{0}'", _statusCode));
            if (_httpResponse.Content != null) {
                _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);
            }
            else {
                _responseContent = string.Empty;
            }
            ex.Request = new HttpRequestMessageWrapper(_httpRequest, _requestContent);
            ex.Response = new HttpResponseMessageWrapper(_httpResponse, _responseContent);
            if (_shouldTrace)
            {
                ServiceClientTracing.Error(_invocationId, ex);
            }
            _httpRequest.Dispose();
            if (_httpResponse != null)
            {
                _httpResponse.Dispose();
            }
            throw ex;
        }
        // Create Result
        var _result = new HttpOperationResponse<GetAvailableCarriersResponse>();
        _result.Request = _httpRequest;
        _result.Response = _httpResponse;
        // Deserialize Response
        if ((int)_statusCode == 200)
        {
            _responseContent = await _httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);
            try
            {
                _result.Body = SafeJsonConvert.DeserializeObject<GetAvailableCarriersResponse>(_responseContent, 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;
    }

我有一个单元测试来调用生成的代码

var api = MakeApi();
var task=api.GetAvailableCarriersByJobHeaderIdWithHttpMessagesAsync(req);
var carriers  = task.Result.Body.Carriers;

在哪里

    private static MyApiService MakeApi()
    {
        var setting = new MyAPISettings(false);
        var api = new MyApiService(setting);
        return api;
    }

和 MyApiService 包含(具有更改的名称空间)

    public Task<HttpOperationResponse<GetAvailableCarriersResponse>> GetAvailableCarriersByJobHeaderIdWithHttpMessagesAsync(
        GetAvailableCarriersRequest request = default(GetAvailableCarriersRequest), Dictionary<string, List<string>> customHeaders = null,
        CancellationToken cancellationToken = default(CancellationToken))
    {
        return ApiCaller.ExecuteAsync(
            async headers => await API.GetAvailableCarriersByJobHeaderIdWithHttpMessagesAsync(request, headers, cancellationToken),
            async () => await GetTokenHeadersAsync(customHeaders));
    }

apicaller 在哪里

using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace MyServices
{
    public static class ApiCaller
    {



        private static Dictionary<string, List<string>> Headers { get; set; }

            private static string GetHeadersMessage()
            {
                string ret = "";

                if (Headers != null)
                {
                    foreach (string key in Headers.Keys)
                    {
                        if (Headers[key] != null)
                        {
                            foreach (string value in Headers[key])
                            {
                                ret = $"{key}-{value}\n";
                            }
                        }
                    }
                }

                return ret;
            }


        public async static Task<T> ExecuteAsync<T>(Func<Dictionary<string, List<string>>, Task<T>> f,
                Func<Task<Dictionary<string, List<string>>>> getHeaders)
            {
                T ret = default(T);

                try
                {
                    try
                    {
                        if (getHeaders != null && Headers == null)
                        {
                            Headers = await getHeaders();
                        }

                        ret = await f(Headers);
                    }
                    catch (Microsoft.Rest.HttpOperationException ex1)
                    {
                        if (ex1.Response?.StatusCode == System.Net.HttpStatusCode.Unauthorized && getHeaders != null)
                        {
                            Headers = await getHeaders();
                            ret = await f(Headers);
                        }
                        else
                        {
                            throw;
                        }
                    }
                }
                catch (Exception ex)
                {
                    //Log.Error(ex, $"... API CALL ERROR ...\nHEADERS:{GetHeadersMessage()}");
                    throw new Exception($"Error calling the API. {ex.Message}", ex);
                }

                return ret;
            }
        }
    }

我的 Api 引发 InternalServerError 但是,当我运行单元测试时,客户端代码中出现错误。

错误发生在

        // Create Result
        var _result = new HttpOperationResponse<GetAvailableCarriersResponse>();

并且是

System.Exception: Error calling the API. Operation returned an invalid status code 'InternalServerError' ---> Microsoft.Rest.HttpOperationException: Operation returned an invalid status code 'InternalServerError'
   at MyConnector.MyApi.<GetAvailableCarriersByJobHeaderIdWithHttpMessagesAsync>d__49.MoveNext() 

我该如何解决这个问题?

我注意到 HttpOperationResponse 的代码是

namespace Microsoft.Rest
{
  /// <summary>
  /// Represents the base return type of all ServiceClient REST operations.
  /// </summary>
  public class HttpOperationResponse<T> : HttpOperationResponse, IHttpOperationResponse<T>, IHttpOperationResponse
  {
    /// <summary>Gets or sets the response object.</summary>
    public T Body { get; set; }
  }
}

这是 GetAvailableCarriersResponse 的结构

using Newtonsoft.Json;
using System.Collections.Generic;

public partial class GetAvailableCarriersResponse
{
    public GetAvailableCarriersResponse()
    {
        CustomInit();
    }

    public GetAvailableCarriersResponse(IList<DeliverBy> carriers = default(IList<DeliverBy>))
    {
        Carriers = carriers;
        CustomInit();
    }

    partial void CustomInit();

    [JsonProperty(PropertyName = "carriers")]
    public IList<DeliverBy> Carriers { get; set; }

}

[更新]

在 ApiCaller ExecuteAsync 中执行以下操作。

throw;

如果我此时发现错误,它是(编辑的) ToString() 返回

"Microsoft.Rest.HttpOperationException: Operation returned an invalid status code 'InternalServerError' at MyAPI.

<GetAvailableCarriersByJobHeaderIdWithHttpMessagesAsync>d__49.MoveNext() in 
MyConnector\\generated\\MyAPI.cs:line 4018

End of stack trace from previous location where exception was thrown at 
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at
System.Runtime.CompilerServices.TaskAwaiter.
HandleNonSuccessAndDebuggerNotification(Task task) 
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at MyApiService.<>c__DisplayClass39_0.<<GetAvailableCarriersByJobHeaderIdWithHttpMessagesAsync>b__0>d.MoveNext() 
in MyApiService.cs:line 339
End of stack trace from previous location where exception was thrown  

at System.Runtime.ExceptionServices.ExceptionDispatch
Info.Throw() at System.Runtime.CompilerServices.TaskAwaiter.
HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()  
at MyServices.ApiCaller.<ExecuteAsync>d__5`1.MoveNext() 
in ApiCaller.cs:line 50"

我编辑了上面代码中的一些名称以简化和混淆。

[更新]

问题似乎与 ApiCaller.ExecuteAsync 的 getHeaders 参数有关

执行异步

[更新]

如果我检查 ExecuteAsync 中抛出的 ex1,我可以使用

ex1.Response.StatusCode

但我如何获得错误描述?

4

1 回答 1

1

我为获取错误描述所做的是将其转换为 Autorest 生成的错误类型之一。

if (myRawResponseObject is My422Response my422Response)
{
    // Response has HTTP Status Code 422
    Console.WriteLine($"Error Response Type: {my422Response.ToString()}");
}

如果您的 OpenAPI 文档为 422 响应定义了错误属性,那么您将在 My422Response 对象上找到它们。

于 2019-06-30T22:11:56.233 回答