예제 #1
0
        public static async Task ExecuteCommand(this HttpClient client, object command, Guid commandId, IMessageExecutionSettings settings)
        {
            string commandJson = settings.Serializer.Serialize(command);
            var httpContent = new StringContent(commandJson);
            httpContent.Headers.ContentType =
                MediaTypeHeaderValue.Parse("application/vnd.{0}.{1}+json".FormatWith(settings.Vendor, command.GetType().Name.ToLower()));

            var request = new HttpRequestMessage(HttpMethod.Put, settings.Path + "/{0}".FormatWith(commandId))
            {
                Content = httpContent
            };
            request.Headers.Accept.ParseAdd("application/json");
            HttpResponseMessage response = await client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead);
            if (response.StatusCode == HttpStatusCode.NotFound)
            {
                throw new InvalidOperationException("Got 404 Not Found for {0}".FormatWith(request.RequestUri));
            }
            if (response.StatusCode == HttpStatusCode.BadRequest)
            {
                var exceptionModel = await settings.Serializer.ReadObject<ExceptionModel>(response.Content);
                throw settings.ModelToExceptionConverter.Convert(exceptionModel);
            }
            if (response.StatusCode == HttpStatusCode.InternalServerError)
            {
                var exceptionModel = await settings.Serializer.ReadObject<ExceptionModel>(response.Content);
                throw settings.ModelToExceptionConverter.Convert(exceptionModel);
            }
        }
예제 #2
0
                public ScenarioBuilder(MidFunc midFunc, AppFunc next = null, string commandPath = null, string name = null)
                {
                    _name = name;
                    next  = next ?? (env =>
                    {
                        var context = new OwinContext(env);
                        context.Response.StatusCode = 404;
                        context.Response.ReasonPhrase = "Not Found";
                        return(Task.FromResult(true));
                    });

                    _messageExecutionSettings = new CommandExecutionSettings("vendor", path: commandPath);

                    _appFunc = midFunc(next);

                    _httpClients = new Dictionary <string, HttpClient>();

                    _runGiven = async() =>
                    {
                        foreach (IHttpClientRequest userCommand in _given)
                        {
                            await Send(userCommand);
                        }
                    };

                    _runWhen = () => Send(_when);

                    _runThen = async() =>
                    {
                        var results = await Task.WhenAll(_assertions.Select(assertion => assertion.Run()));

                        var failed = (from result in results
                                      from assertionResult in result
                                      where !assertionResult.Passed
                                      select assertionResult).ToList();

                        if (failed.Any())
                        {
                            throw new ScenarioException(this, "One or more assertions failed:"
                                                        + Environment.NewLine
                                                        + failed.Aggregate(new StringBuilder(), (builder, assertionResult) => builder.Append(assertionResult).AppendLine()));
                        }
                    };

                    _assertions = new List <IAssertion>();
                    _timer      = new Stopwatch();
                }
예제 #3
0
        public static async Task <TOutput> ExecuteQuery <TInput, TOutput>(this HttpClient client, TInput input, Guid queryId, IMessageExecutionSettings settings)
        {
            string queryJson   = settings.Serializer.Serialize(input);
            var    httpContent = new StringContent(queryJson);

            httpContent.Headers.ContentType =
                MediaTypeHeaderValue.Parse("application/vnd.{0}.{1}+json".FormatWith(settings.Vendor, typeof(TInput).Name).ToLower());

            var request = new HttpRequestMessage(HttpMethod.Get, settings.Path + "/{0}".FormatWith(typeof(TInput).Name).ToLower())
            {
                Content = httpContent,
            };

            request.Headers.Accept.ParseAdd("application/vnd.{0}.{1}+json".FormatWith(settings.Vendor, typeof(TOutput).Name).ToLower());

            HttpResponseMessage response = await client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead);

            await response.ThrowOnErrorStatus(request, settings);

            var jsonString = await response.Content.ReadAsStringAsync();

            return((TOutput)settings.Serializer.Deserialize(jsonString, typeof(TOutput)));
        }
예제 #4
0
        public static async Task ExecuteCommand(this HttpClient client, object command, Guid commandId, IMessageExecutionSettings settings)
        {
            string commandJson = settings.Serializer.Serialize(command);
            var    httpContent = new StringContent(commandJson);

            httpContent.Headers.ContentType =
                MediaTypeHeaderValue.Parse("application/vnd.{0}.{1}+json".FormatWith(settings.Vendor, command.GetType().Name.ToLower()));

            var request = new HttpRequestMessage(HttpMethod.Put, settings.Path + "/{0}".FormatWith(commandId))
            {
                Content = httpContent
            };

            request.Headers.Accept.ParseAdd("application/json");

            HttpResponseMessage response = await client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead);

            await response.ThrowOnErrorStatus(request, settings);
        }
예제 #5
0
        internal static async Task ThrowOnErrorStatus(this HttpResponseMessage response, HttpRequestMessage request, IMessageExecutionSettings options)
        {
            if (response.StatusCode == HttpStatusCode.NotFound)
            {
                throw new InvalidOperationException("Got 404 Not Found for {0}".FormatWith(request.RequestUri));
            }
            if ((int)response.StatusCode >= 400)
            {
                var exception = await options.Serializer.ReadException(response.Content, options.ModelToExceptionConverter);

                ExceptionDispatchInfo.Capture(exception).Throw();
            }
        }