Exemplo n.º 1
0
        public async Task PostFile_MultipartMultipleFiles_BadRequestFailure()
        {
            // Arrange
            var guid = Guid.NewGuid();
            var moq  = new Mock <IFilesRepository>();

            moq.Setup(t => t.PostFileHead(It.IsAny <FsFile>())).ReturnsAsync(guid);
            var moqFileStreamRepo = new Mock <IFileStreamRepository>();
            var moqConfigRepo     = new Mock <IConfigRepository>();
            var moqLog            = new Mock <sl.IServiceLogRepository>();

            string fileName4Upload    = "\"UploadTest.txt\"";
            string fileContent4Upload = "This is the content of the uploaded test file";

            var multiPartContent1 = new MultipartFormDataContent("----MyGreatBoundary");
            ByteArrayContent byteArrayContent1 = new ByteArrayContent(Encoding.UTF8.GetBytes(fileContent4Upload));
            ByteArrayContent byteArrayContent2 = new ByteArrayContent(Encoding.UTF8.GetBytes(fileContent4Upload));

            byteArrayContent1.Headers.Add("Content-Type", "multipart/form-data");
            multiPartContent1.Add(byteArrayContent1, "this is the name of the content", fileName4Upload);
            multiPartContent1.Add(byteArrayContent2, "this is the name of the content", fileName4Upload);

            var controller = new FilesController(moq.Object, moqFileStreamRepo.Object, moqConfigRepo.Object, moqLog.Object)
            {
                Request = new HttpRequestMessage
                {
                    RequestUri = new Uri("http://localhost/files"),
                    Content    = multiPartContent1
                },
                Configuration = new HttpConfiguration()
            };

            var context = await SetupMultipartPost(multiPartContent1);


            controller.Configuration.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "files/{id}",
                defaults: new { id = RouteParameter.Optional });

            // Act
            // 1. Upload file
            var response = await controller.PostFileHttpContext(context.Object, null);

            // Assert
            Assert.IsTrue(response.Status == HttpStatusCode.BadRequest);
        }
Exemplo n.º 2
0
        public async Task PostFile_MultipartRepoThrowsException()
        {
            // Arrange
            var moq = new Mock <IFilesRepository>();

            moq.Setup(t => t.PostFileHead(It.IsAny <FsFile>())).Throws(new Exception());
            var moqFileStreamRepo = new Mock <IFileStreamRepository>();
            var moqConfigRepo     = new Mock <IConfigRepository>();
            var moqLog            = new Mock <sl.IServiceLogRepository>();

            string fileName4Upload    = "\"UploadTest.txt\"";
            string fileContent4Upload = "This is the content of the uploaded test file";

            MultipartFormDataContent multiPartContent = new MultipartFormDataContent("----MyGreatBoundary");
            ByteArrayContent         byteArrayContent = new ByteArrayContent(Encoding.UTF8.GetBytes(fileContent4Upload));

            byteArrayContent.Headers.Add("Content-Type", "multipart/form-data");
            multiPartContent.Add(byteArrayContent, "this is the name of the content", fileName4Upload);

            var controller = new FilesController(moq.Object, moqFileStreamRepo.Object, moqConfigRepo.Object, moqLog.Object)
            {
                Request = new HttpRequestMessage
                {
                    RequestUri = new Uri("http://localhost/files"),
                    Content    = multiPartContent
                },
                Configuration = new HttpConfiguration()
            };

            var context = await SetupMultipartPost(multiPartContent);


            controller.Configuration.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "files/{id}",
                defaults: new { id = RouteParameter.Optional });

            // Act
            // 1. Upload file
            await controller.PostFileHttpContext(context.Object, null);
        }