コード例 #1
0
        public void Should_Return_Validation_Error_When_Invalid_Data_Given()
        {
            // Checked in the Request Validation:
            applicationSettingsMock.Expect(x => x.MaxFileSizeForUpload)
            .Repeat.Any()
            .Return(FileSize.Create(2, FileSize.Unit.Megabyte));

            // Pass the Bootstrapper with Mocks into the Testing Browser:
            var browser = new Browser(bootstrapper);

            // Define the When Action:
            var result = browser.Post("/file/upload", with =>
            {
                with.Header("Accept", "application/json");
                with.HttpRequest();
            });

            // Check the Results:
            Assert.AreEqual(HttpStatusCode.BadRequest, result.StatusCode);

            // Deserialize the Error:
            var error = new JsonSerializer().Deserialize <ServiceErrorModel>(result);

            Assert.AreEqual(ServiceErrorEnum.ValidationError, error.Code);
            Assert.AreEqual("Validation failed for Request Parameters: (Parameter = Title, Errors = ('Title' must not be empty.), Parameter = Tags, Errors = (Length of the array is not between 1 and 5.), Parameter = File, Errors = ('File' must not be empty.))", error.Details);
        }
コード例 #2
0
 public void BytesCorrect_When_Converting_Between_Units()
 {
     Assert.AreEqual(2, FileSize.Create(2, Unit.Byte).Get(Unit.Byte));
     Assert.AreEqual((long)(2 * Math.Pow(2, 10)), FileSize.Create(2, Unit.Kilobyte).Get(Unit.Byte));
     Assert.AreEqual((long)(1000 * Math.Pow(2, 20)), FileSize.Create(1000, Unit.Megabyte).Get(Unit.Byte));
     Assert.AreEqual((long)(2 * Math.Pow(2, 30)), FileSize.Create(2, Unit.Gigabyte).Get(Unit.Byte));
     Assert.AreEqual((long)(3 * Math.Pow(2, 40)), FileSize.Create(3, Unit.Terabyte).Get(Unit.Byte));
 }
コード例 #3
0
        public void Parameters_Mapped_Correctly_When_Parameters_Are_Given()
        {
            string   uploadDirectory = "upload_dir";
            FileSize fileSize        = FileSize.Create(321, FileSize.Unit.Kilobyte);

            ApplicationSettings applicationSettings = new ApplicationSettings(uploadDirectory, fileSize);

            Assert.AreEqual(uploadDirectory, applicationSettings.FileUploadDirectory);
            Assert.AreEqual(321, (int)applicationSettings.MaxFileSizeForUpload.Get(FileSize.Unit.Kilobyte));
        }
コード例 #4
0
        public void Constructor_Throws_When_Required_Parameter_Is_Null()
        {
            string   uploadDirectory = "upload_dir";
            FileSize fileSize        = FileSize.Create(321, FileSize.Unit.Kilobyte);

            Assert.Throws <ArgumentNullException>(() => new ApplicationSettings(uploadDirectory, null));
            Assert.Throws <ArgumentNullException>(() => new ApplicationSettings(null, fileSize));
            Assert.Throws <ArgumentNullException>(() => new ApplicationSettings(null, null));

            Assert.DoesNotThrow(() => new ApplicationSettings(uploadDirectory, fileSize));
        }
コード例 #5
0
 public void ConfigureApplicationContainer(TinyIoCContainer container)
 {
     container.Register <IApplicationSettings>(new ApplicationSettings("uploads", FileSize.Create(2, FileSize.Unit.Megabyte)));
 }
コード例 #6
0
        public void Should_Store_File_When_Request_Is_Valid()
        {
            // Define the File Name and Content:
            var fileName = "persons.txt";

            var fileContent = new StringBuilder()
                              .AppendLine("FirstName;LastName;BirthDate")
                              .AppendLine("Philipp;Wagner;1986/05/12")
                              .AppendLine("Max;Mustermann;2014/01/01");

            // Create the File:
            Assert.AreEqual(true, Create(fileName, fileContent.ToString()));

            // The Result of the Upload (we are not writing to disk actually):
            var fileUploadResult = new FileUploadResult()
            {
                Identifier = Guid.NewGuid().ToString()
            };

            // Checked in the Request Validation:
            applicationSettingsMock.Expect(x => x.MaxFileSizeForUpload)
            .Repeat.Any()
            .Return(FileSize.Create(2, FileSize.Unit.Megabyte));

            // Probably we should also check the Content?
            fileUploadHandlerMock.Expect(x => x.HandleUpload(Arg <string> .Is.Equal(fileName), Arg <Stream> .Is.Anything))
            .Repeat.Once()
            .Return(Task.Delay(100).ContinueWith(x => fileUploadResult));

            // Pass the Bootstrapper with Mocks into the Testing Browser:
            var browser = new Browser(bootstrapper);

            using (var textReader = new FileStream(GetAbsolutePath(fileName), FileMode.Open))
            {
                // Define the Multipart Form Data for an upload:
                var multipartFormData = new BrowserContextMultipartFormData(
                    (configuration =>
                {
                    configuration.AddFile("file", fileName, "text", textReader);

                    configuration.AddFormField("tags", "text", "Hans,Wurst");
                    configuration.AddFormField("description", "text", "Description");
                    configuration.AddFormField("title", "text", "Title");
                }
                    ));

                // Define the When Action:
                var result = browser.Post("/file/upload", with =>
                {
                    with.Header("Accept", "application/json");
                    with.Header("Content-Length", "1234");

                    with.HttpRequest();

                    with.MultiPartFormData(multipartFormData);
                });

                // File Upload was successful:
                Assert.AreEqual(HttpStatusCode.OK, result.StatusCode);

                // We get the Expected Identifier:
                var deserializedResponseContent = new JsonSerializer().Deserialize <FileUploadResult>(result);

                Assert.AreEqual(deserializedResponseContent.Identifier, fileUploadResult.Identifier);
            }


            // Finally delete the Temporary file:
            Assert.AreEqual(true, Delete(fileName));
        }