/// <summary> /// Adds a multipart/form-data encoded request body to the <see cref="Browser"/>. /// </summary> /// <param name="browserContext">The <see cref="BrowserContext"/> that the data should be added to.</param> /// <param name="multipartFormData">The multipart/form-data encoded data that should be added.</param> /// <param name="boundaryName">The name of the boundary to be used</param> public static void MultiPartFormData(this BrowserContext browserContext, BrowserContextMultipartFormData multipartFormData, string boundaryName) { var contextValues = (IBrowserContextValues)browserContext; contextValues.Body = multipartFormData.Body; contextValues.Headers["Content-Type"] = new[] { "multipart/form-data; boundary=" + boundaryName }; }
/// <summary> /// Adds a multipart/form-data encoded request body to the <see cref="Browser"/>. /// </summary> /// <param name="browserContext">The <see cref="BrowserContext"/> that the data should be added to.</param> /// <param name="multipartFormData">The multipart/form-data encoded data that should be added.</param> /// <param name="boundaryName">The name of the boundary to be used</param> public static void MultiPartFormData(this BrowserContext browserContext, BrowserContextMultipartFormData multipartFormData, string boundaryName) { var contextValues = (IBrowserContextValues)browserContext; contextValues.Body = multipartFormData.Body; contextValues.Headers["Content-Type"] = new[] { "multipart/form-data; boundary=" + boundaryName }; }
/// <summary> /// Adds a multipart/form-data encoded request body to the <see cref="Browser"/>. /// </summary> /// <param name="browserContext">The <see cref="BrowserContext"/> that the data should be added to.</param> /// <param name="multipartFormData">The multipart/form-data encoded data that should be added.</param> public static void MultiPartFormData(this BrowserContext browserContext, BrowserContextMultipartFormData multipartFormData) { var contextValues = (IBrowserContextValues)browserContext; contextValues.Body = multipartFormData.Body; contextValues.Headers["Content-Type"] = new[] { "multipart/form-data; boundary=NancyMultiPartBoundary123124" }; }
/// <summary> /// Adds a multipart/form-data encoded request body to the <see cref="Browser"/>. /// </summary> /// <param name="browserContext">The <see cref="BrowserContext"/> that the data should be added to.</param> /// <param name="multipartFormData">The multipart/form-data encoded data that should be added.</param> public static void MultiPartFormData(this BrowserContext browserContext, BrowserContextMultipartFormData multipartFormData) { var contextValues = (IBrowserContextValues)browserContext; contextValues.Body = multipartFormData.Body; contextValues.Headers["Content-Type"] = new[] { "multipart/form-data; boundary=NancyMultiPartBoundary123124" }; }
public void Save_OnSaveError_ErrorReturnedInResponse() { int projectId = new Random().Next(1, 100); string fileName = Path.GetRandomFileName() + ".log"; string filePath = Path.Combine(AppContext.BaseDirectory, fileName); string exceptionMessage = Guid.NewGuid().ToString(); // setup _appSettings.LogFileProcessingDirectory.Returns(AppContext.BaseDirectory); var currentUser = new UserIdentity() { Id = Guid.NewGuid(), UserName = "******" }; currentUser.Claims = new string[] { Claims.ProjectEdit }; _createLogFileCommand.When(x => x.Execute(projectId, filePath)) .Do((c) => { throw new Exception(exceptionMessage); }); var browser = new Browser((bootstrapper) => bootstrapper.Module(new LogFileModule(_dbContext, _appSettings, _createLogFileCommand, _deleteLogFileCommand, _dirWrap)) .RequestStartup((container, pipelines, context) => { context.CurrentUser = currentUser; }) ); byte[] buffer = new byte[100]; new Random().NextBytes(buffer); using (MemoryStream stream = new MemoryStream(buffer)) { var multipart = new BrowserContextMultipartFormData(x => { x.AddFile("foo", fileName, "text/plain", stream); }); // execute var url = Actions.LogFile.Save(projectId); var response = browser.Post(url, (with) => { with.HttpRequest(); with.FormsAuth(currentUser.Id, new Nancy.Authentication.Forms.FormsAuthenticationConfiguration()); with.MultiPartFormData(multipart); with.FormValue("projectId", projectId.ToString()); }); // assert Assert.AreEqual(HttpStatusCode.BadRequest, response.StatusCode); string result = response.Body.AsString(); Assert.AreEqual(JsonConvert.SerializeObject(exceptionMessage), result); _createLogFileCommand.Received(1).Execute(projectId, filePath); _dbContext.Received(1).Rollback(); } }
public async Task ExchangeRateModule_UploadNoCSV_ReturnsBadRequest() { var browserContext = new BrowserContextMultipartFormData(c => { }); var url = "/v1/exchangerate/uploadcsv"; var result = await Browser.Post(url, c => { c.User(_user); c.MultiPartFormData(browserContext); }); ValidateStatusCode(result, HttpStatusCode.BadRequest); }
public async Task ExchangeRateModule_ProcessExchangeRatesCSV_Correctly() { var csvFile = @"Currency Name,Code,,29-Feb,Exch Rate,Abstract Type Id,,,,,, BULGARIA,BGN,B/S,0.55903,0.55903,,,,,,, CANADA,CAD,B / S,0.73926,0.73926,,,,,,,"; var url = "/v1/exchangerate/uploadcsv"; var stream = new MemoryStream(Encoding.UTF8.GetBytes(csvFile)); var browserContext = new BrowserContextMultipartFormData(c => { c.AddFile("file", "file.csv", "text/csv", stream); }); var result = await Browser.Post(url, c => { c.User(_user); c.MultiPartFormData(browserContext); }); ValidateStatusCode(result, HttpStatusCode.OK); }
public void Should_return_status_created_when_installing_a_package_by_file_upload() { // Arrange var stream = CreateFakeFileStream("This is the contents of a file"); var multipart = new BrowserContextMultipartFormData(x => x.AddFile("foo", "foo.update", "text/plain", stream)); _mockTempPackager.Setup(x => x.GetPackageToInstall(It.IsAny <Stream>())).Returns("foo.update"); _mockPackageRepos.Setup(x => x.AddPackage(It.IsAny <InstallPackage>())).Returns(new PackageManifest()); // Act var response = _browser.Post("/services/package/install/fileupload", with => { with.HttpRequest(); with.MultiPartFormData(multipart); }); // Assert Assert.Equal(HttpStatusCode.Created, response.StatusCode); Assert.Equal("/services/package/latestversion", response.Headers["Location"]); }
public void Should_be_able_to_save_a_cms_item() { // Given var bootstrapper = new CustomBootstrapper(); var browser = new Browser(bootstrapper); var streamReader = new StreamReader(Path.Combine(Environment.CurrentDirectory, "Content", "Logo.png")); var multipart = new BrowserContextMultipartFormData(x => x.AddFile("Logo.png", "Logo.png", "image/png", streamReader.BaseStream)); // When var result = browser.Post("/cms/", delegate(BrowserContext with) { with.HttpRequest(); with.FormValue("key", "Homepage_Title_Background"); with.MultiPartFormData(multipart); }); // Then Assert.Equal(HttpStatusCode.OK, result.StatusCode); }
public void Should_return_json_containing_installed_package_details() { // Arrange var stream = CreateFakeFileStream("This is the contents of a file"); var multipart = new BrowserContextMultipartFormData(x => x.AddFile("foo", "foo.update", "text/plain", stream)); _mockTempPackager.Setup(x => x.GetPackageToInstall(It.IsAny <Stream>())).Returns("foo.update"); _mockPackageRepos.Setup(x => x.AddPackage(It.IsAny <InstallPackage>())).Returns(new PackageManifest()); // Act var response = _browser.Post("/services/package/install/fileupload", with => { with.HttpRequest(); with.MultiPartFormData(multipart); }); //Assert var manifest = Newtonsoft.Json.JsonConvert.DeserializeObject <PackageManifest>(response.Body.AsString()); Assert.Equal(HttpStatusCode.Created, response.StatusCode); Assert.NotNull(manifest.Entries); }
/// <summary> /// Adds a multipart/form-data encoded request body to the <see cref="Browser"/>, using the default boundary name. /// </summary> /// <param name="browserContext">The <see cref="BrowserContext"/> that the data should be added to.</param> /// <param name="multipartFormData">The multipart/form-data encoded data that should be added.</param> public static void MultiPartFormData(this BrowserContext browserContext, BrowserContextMultipartFormData multipartFormData) { MultiPartFormData(browserContext, multipartFormData, BrowserContextMultipartFormData.DefaultBoundaryName); }
/// <summary> /// Adds a multipart/form-data encoded request body to the <see cref="Browser"/>, using the default boundary name. /// </summary> /// <param name="browserContext">The <see cref="BrowserContext"/> that the data should be added to.</param> /// <param name="multipartFormData">The multipart/form-data encoded data that should be added.</param> public static void MultiPartFormData(this BrowserContext browserContext, BrowserContextMultipartFormData multipartFormData) { MultiPartFormData(browserContext, multipartFormData, BrowserContextMultipartFormData.DefaultBoundaryName); }
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)); }