private async Task <string> UploadPayloadsWith(MultipartPayloadReader reader) { string payloadId = Guid.NewGuid().ToString(); await reader.StartReading( async payload => payloadId = await _payloadPersistor.SavePayload(payload)); return(payloadId); }
private static async Task <MultipartPayloadReader> CreateStubReaderThatReturns(string expectedContent, Stream actualStream) { using (Stream contentStream = expectedContent.AsStream()) { var multipartContent = new MultipartFormDataContent { { new StreamContent(contentStream), "name", "filename" } }; await multipartContent.CopyToAsync(actualStream); actualStream.Position = 0; return(MultipartPayloadReader.TryCreate( actualStream, multipartContent.Headers.ContentType.ToString()).Item2); } }
public async Task <IActionResult> Upload() { (bool success, MultipartPayloadReader reader)result = MultipartPayloadReader.TryCreate(Request.Body, Request.ContentType); if (!result.success) { return(BadRequest($"Expected a multipart request, but got {Request.ContentType}")); } string payloadId = await UploadPayloadsWith(result.reader); string downloadUrl = FormatWithDownloadUrl(payloadId); return(new OkObjectResult(new UploadResult(payloadId, downloadUrl))); }
public async Task ReadsExpectedContent() { // Arrange const string expectedContent = "message data!"; using (var actualStream = new MemoryStream()) { MultipartPayloadReader reader = await CreateStubReaderThatReturns(expectedContent, actualStream); var waitHandle = new ManualResetEvent(initialState: false); // Act await reader.StartReading(payload => { // Assert Assert.Equal(expectedContent, payload.DeserializeContent()); waitHandle.Set(); return(Task.CompletedTask); }); Assert.True(waitHandle.WaitOne(timeout: TimeSpan.FromSeconds(1))); } }
public void CannotCreateReader_IfContentTypeIsntMultiPart() { Assert.False(MultipartPayloadReader.TryCreate(Stream.Null, "application/json").Item1); }