代码之家  ›  专栏  ›  技术社区  ›  Kirsten

调用自动测试生成的代码使用者时出错。微软休息HttpOperationException:操作返回了无效的状态代码

  •  0
  • Kirsten  · 技术社区  · 5 年前

    我有一个单元测试,调用一个服务,该服务使用自动测试生成的代码来调用我的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抛出了一个内部服务器错误 然而,当我运行单元测试时,客户端代码中出现了一个错误。

    错误发生在

            // 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的getHeaders参数有关。执行同步

    ExecuteAsync

    [更新]

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

    ex1.Response.StatusCode
    

    但是我怎样才能得到错误描述呢?

    0 回复  |  直到 5 年前
        1
  •  1
  •   GraehamF    5 年前

    我得到错误描述的方法是将其转换为Autorest生成的一种错误类型。

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

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

    推荐文章