Ejemplo n.º 1
0
        public T PostAsync <T>(string urlConfig, object requestModel)
        {
            if (Convert.ToBoolean(Configuration["MockData.Enabled"]))
            {
                MockWebApi = new MockWebApi();

                var incoming  = JsonConvert.SerializeObject(requestModel).ToUpper();
                var responses = JsonConvert.SerializeObject(MockWebApi.Responses.First(x => x.WepApiUrl == urlConfig).RequestModel).ToUpper();

                if (MockWebApi.Responses.Any(x => x.WepApiUrl == urlConfig))
                {
                    return((T)MockWebApi.Responses.FirstOrDefault(x => x.WepApiUrl == urlConfig && JsonConvert.SerializeObject(x.RequestModel).ToUpper() == JsonConvert.SerializeObject(requestModel).ToUpper()).ResponseContent);
                }
            }

            var webApiUrl = Configuration[urlConfig];

            var CurrentUser = SecurityHelper.GetSessionUser(CookieHelper.GetCookie <UserModel>("CurrentUser"));

            if (CurrentUser != null)
            {
                webApiUrl += "?token=" + CurrentUser.ApiSessionToken;
            }

            var webApiResponse = PostRequest(webApiUrl, requestModel);

            Exception innerException = null;

            try
            {
                innerException = JsonConvert.DeserializeObject <Exception>(webApiResponse.Result.ResponseContent);
            }
            catch { }

            if (webApiResponse.Result.ResponseCode != ((int)HttpStatusCode.OK).ToString())
            {
                throw new Exception("Error occurred at " + urlConfig.ToLower() + " - " + innerException?.Message, innerException);
            }

            return(JsonConvert.DeserializeObject <T>(webApiResponse.Result.ResponseContent));
        }
        public void TaskPipelineTest()
        {
            var operationContent = new StringContent("ToUpper");
            var cd = new ContentDispositionHeaderValue("inline");

            cd.Name = "operation";
            operationContent.Headers.ContentDisposition = cd;

            var fileContent = new ByteArrayContent(File.ReadAllBytes(@"..\..\Source.txt"));

            cd          = new ContentDispositionHeaderValue("attachment");
            cd.Name     = "file";
            cd.FileName = "Source.txt";
            fileContent.Headers.ContentDisposition = cd;
            var md = new MediaTypeHeaderValue("text/plain");

            fileContent.Headers.ContentType = md;

            var multipartContent = new MultipartContent();

            multipartContent.Add(operationContent);
            multipartContent.Add(fileContent);

            using (var server = MockWebApi.CreateServer())
            {
                var response = server.CreateRequest("/api/ProcessingTask")
                               .And(r => r.Content = multipartContent)
                               .PostAsync()
                               .GetAwaiter()
                               .GetResult();

                Assert.IsTrue(HttpStatusCode.OK == response.StatusCode, "StatusCode should be OK");

                ProcessingTask task = response.Content.ReadAsAsync <ProcessingTask>().GetAwaiter().GetResult();

                string taskId = task.Id;
                Assert.IsTrue(TaskStatus.Enqueued == task.Status, "Task should be enqueued");

                response = server.CreateRequest("/api/ProcessingTaskStatus?taskId=" + taskId)
                           .GetAsync()
                           .GetAwaiter()
                           .GetResult();

                Assert.IsTrue(HttpStatusCode.OK == response.StatusCode, "StatusCode should be OK");
                task = response.Content.ReadAsAsync <ProcessingTask>().GetAwaiter().GetResult();

                Assert.IsTrue(TaskStatus.Enqueued == task.Status, "Task should be still enqueued");

                var converters = new Dictionary <string, IFileProcessor>();
                converters["ToUpper"] = new ToUpperFileProcessor();

                ILifetimeAwareComponent <IFileProcessingWorker> app =
                    MockWebApi.CreateFileProcessingWorker(converters);
                using (app.LifetimeScope)
                {
                    app.Service.DoWorkAsync().GetAwaiter().GetResult();
                }

                response = server.CreateRequest("/api/ProcessingTaskStatus?taskId=" + taskId)
                           .GetAsync()
                           .GetAwaiter()
                           .GetResult();

                Assert.IsTrue(HttpStatusCode.OK == response.StatusCode, "StatusCode should be OK");
                task = response.Content.ReadAsAsync <ProcessingTask>().GetAwaiter().GetResult();

                Assert.IsTrue(TaskStatus.Success == task.Status, "Task should be succeeded");

                response = server.CreateRequest("/api/ProcessingResult?taskId=" + taskId)
                           .GetAsync()
                           .GetAwaiter()
                           .GetResult();

                Assert.IsTrue(HttpStatusCode.OK == response.StatusCode, "StatusCode should be OK");
            }
        }