private ApiResult <T> PostModel <T>(T model, string url) { var mediaTypeFormatter = GetFormatter(); using (var server = new WebApiHttpServer(mediaTypeFormatter)) using (var client = server.CreateClient()) using (HttpResponseMessage response = client.PostAsync(url, model, mediaTypeFormatter).Result) { ApiResult <T> resultModel; if (response.StatusCode != HttpStatusCode.OK) { var error = response.Content.ReadAsAsync <List <ResponseErrorItem> >(new[] { mediaTypeFormatter }).Result; var err = error.Where(m => m.Key == "ExceptionMessage").Select(m => m.Value).FirstOrDefault(); if (String.IsNullOrWhiteSpace(err)) { var responseContent = response.Content.ReadAsStringAsync().Result; throw new XunitException(responseContent); } resultModel = new ApiResult <T>() { ErrorMessage = err }; } else { resultModel = response.Content.ReadAsAsync <ApiResult <T> >(new[] { mediaTypeFormatter }).Result; } return(resultModel); } }
private ApiResult <PersonModel> PostPersonModelHttpContent(HttpContent httpContent, MediaTypeFormatter mediaTypeFormatter = null) { mediaTypeFormatter = mediaTypeFormatter ?? GetFormatter(); using (var server = new WebApiHttpServer(mediaTypeFormatter)) using (var client = server.CreateClient()) using (HttpResponseMessage response = client.PostAsync("TestApi/PostPerson", httpContent).Result) { if (response.StatusCode != HttpStatusCode.OK) { var err = response.Content.ReadAsStringAsync().Result; throw new XunitException(err); } var resultModel = response.Content.ReadAsAsync <ApiResult <PersonModel> >(new[] { mediaTypeFormatter }).Result; return(resultModel); } }