-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathToFileStreamHttpResultStreamE.cs
More file actions
95 lines (82 loc) · 3.52 KB
/
ToFileStreamHttpResultStreamE.cs
File metadata and controls
95 lines (82 loc) · 3.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
using System.Net.Mime;
using CSharpFunctionalExtensions.HttpResults.Tests.Shared;
using FluentAssertions;
using Microsoft.AspNetCore.Http.HttpResults;
using Microsoft.Net.Http.Headers;
namespace CSharpFunctionalExtensions.HttpResults.Tests.ResultExtensions;
public class ToFileStreamHttpResultStreamE
{
[Fact]
public void ResultStreamE_Success_can_be_mapped_to_FileStreamHttpResult()
{
var value = "foo"u8.ToArray();
using var sourceStream = new MemoryStream();
sourceStream.Write(value);
var contentType = MediaTypeNames.Text.Plain;
var fileDownloadName = "foo.txt";
var lastModified = DateTimeOffset.Now;
var entityTag = new EntityTagHeaderValue("\"fooETag\"");
var enableRangeProcessing = true;
var result =
Result
.Success<Stream, DocumentMissingError>(sourceStream)
.ToFileStreamHttpResult(contentType, fileDownloadName, lastModified, entityTag, enableRangeProcessing)
.Result as FileStreamHttpResult;
using var resultStream = new MemoryStream();
result!.FileStream.Position = 0;
result!.FileStream.CopyTo(resultStream);
resultStream.ToArray().Should().BeEquivalentTo(value);
result!.LastModified.Should().Be(lastModified);
result!.FileDownloadName.Should().Be(fileDownloadName);
result!.FileLength.Should().Be(value.Length);
result!.ContentType.Should().Be(contentType);
result!.EnableRangeProcessing.Should().Be(enableRangeProcessing);
result!.EntityTag.Should().Be(entityTag);
}
[Fact]
public async Task ResultStreamE_Success_can_be_mapped_to_FileStreamHttpResult_Async()
{
var value = "foo"u8.ToArray();
await using var sourceStream = new MemoryStream();
sourceStream.Write(value);
var contentType = MediaTypeNames.Text.Plain;
var fileDownloadName = "foo.txt";
var lastModified = DateTimeOffset.Now;
var entityTag = new EntityTagHeaderValue("\"fooETag\"");
var enableRangeProcessing = true;
var result =
(
await Task.FromResult(Result.Success<Stream, DocumentMissingError>(sourceStream))
.ToFileStreamHttpResult(contentType, fileDownloadName, lastModified, entityTag, enableRangeProcessing)
).Result as FileStreamHttpResult;
await using var resultStream = new MemoryStream();
result!.FileStream.Position = 0;
await result!.FileStream.CopyToAsync(resultStream);
resultStream.ToArray().Should().BeEquivalentTo(value);
result!.LastModified.Should().Be(lastModified);
result!.FileDownloadName.Should().Be(fileDownloadName);
result!.FileLength.Should().Be(value.Length);
result!.ContentType.Should().Be(contentType);
result!.EnableRangeProcessing.Should().Be(enableRangeProcessing);
result!.EntityTag.Should().Be(entityTag);
}
[Fact]
public void ResultStreamE_Failure_can_be_mapped_to_FileStreamHttpResult()
{
var error = new DocumentMissingError { DocumentId = Guid.NewGuid().ToString() };
var result =
Result.Failure<Stream, DocumentMissingError>(error).ToFileStreamHttpResult().Result as NotFound<string>;
result!.StatusCode.Should().Be(404);
result!.Value.Should().Be(error.DocumentId);
}
[Fact]
public async Task ResultStreamE_Failure_can_be_mapped_to_FileStreamHttpResult_Async()
{
var error = new DocumentMissingError { DocumentId = Guid.NewGuid().ToString() };
var result =
(await Task.FromResult(Result.Failure<Stream, DocumentMissingError>(error)).ToFileStreamHttpResult()).Result
as NotFound<string>;
result!.StatusCode.Should().Be(404);
result!.Value.Should().Be(error.DocumentId);
}
}