提供者状态错误

时间:2019-07-31 09:17:26

标签: c# pact

我的使用者运行良好,并且生成了pact文件。问题出在提供者身上,这是下面的问题。

Failure/Error: set_up_provider_states interaction.provider_states, 
options[:consumer]

Pact::ProviderVerifier::SetUpProviderStateError:
    Error setting up provider state 'New' for consumer 'Consumer' at http://localhost:9001/provider-states. response status=500 response body=

下面是我的ProviderStates代码。我现在不需要任何提供程序状态,但是doc说我们必须实现它,因此是空白方法。

public class ProviderStateMiddleware
 {
    private const string ConsumerName = "Consumer";
    private readonly RequestDelegate _next;
    private readonly IDictionary<string, Action> _providerStates;

    public ProviderStateMiddleware(RequestDelegate next)
    {
        _next = next;
        _providerStates = new Dictionary<string, Action>
        {
            {
                "New",
                getSessionID
            }
        };
    }


private void getSessionID()
{


}

   public async Task Invoke(HttpContext context)
    {
        if (context.Request.Path.Value == "/provider-states")
        {
            this.HandleProviderStatesRequest(context);
            await context.Response.WriteAsync(String.Empty);
        }
        else
        {
            await this._next(context);
        }
    }

    private void HandleProviderStatesRequest(HttpContext context)
    {
        context.Response.StatusCode = (int)HttpStatusCode.OK;

        if (context.Request.Method.ToUpper() == HttpMethod.Post.ToString().ToUpper() &&
            context.Request.Body != null)
        {
            string jsonRequestBody = String.Empty;
            using (var reader = new StreamReader(context.Request.Body, Encoding.UTF8))
            {
                jsonRequestBody = reader.ReadToEnd();
            }

            var providerState = JsonConvert.DeserializeObject<ProviderState>(jsonRequestBody);

            //A null or empty provider state key must be handled
            if (providerState != null && !String.IsNullOrEmpty(providerState.State) &&
                providerState.Consumer == ConsumerName)
            {
                _providerStates[providerState.State].Invoke();
            }
        }
    }
}

请让我知道。

0 个答案:

没有答案