-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathShouldRenderFile.cs
More file actions
150 lines (121 loc) · 5.36 KB
/
ShouldRenderFile.cs
File metadata and controls
150 lines (121 loc) · 5.36 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
using System.IO;
using System.Linq;
using System.Text;
using System.Web.Mvc;
namespace TestStack.FluentMVCTesting
{
public partial class ControllerResultTest<T>
{
private static void EnsureContentTypeIsSame(string actual, string expected)
{
if (expected == null) return;
if (actual != expected)
{
throw new ActionResultAssertionException(string.Format(
"Expected file to be of content type '{0}', but instead was given '{1}'.", expected, actual));
}
}
private static byte[] ConvertStreamToArray(Stream stream)
{
using (var memoryStream = new MemoryStream())
{
stream.CopyTo(memoryStream);
stream.Position = 0;
return memoryStream.ToArray();
}
}
public FileResult ShouldRenderAnyFile(string contentType = null)
{
ValidateActionReturnType<FileResult>();
var fileResult = (FileResult)ActionResult;
EnsureContentTypeIsSame(fileResult.ContentType, contentType);
return fileResult;
}
public FileContentResult ShouldRenderFileContents(byte[] contents = null, string contentType = null)
{
ValidateActionReturnType<FileContentResult>();
var fileResult = (FileContentResult)ActionResult;
EnsureContentTypeIsSame(fileResult.ContentType, contentType);
if (contents != null && !fileResult.FileContents.SequenceEqual(contents))
{
throw new ActionResultAssertionException(string.Format(
"Expected file contents to be equal to [{0}], but instead was given [{1}].",
string.Join(", ", contents),
string.Join(", ", fileResult.FileContents)));
}
return fileResult;
}
public FileContentResult ShouldRenderFileContents(string contents, string contentType = null, Encoding encoding = null)
{
ValidateActionReturnType<FileContentResult>();
var fileResult = (FileContentResult)ActionResult;
EnsureContentTypeIsSame(fileResult.ContentType, contentType);
if (encoding == null)
encoding = Encoding.UTF8;
var reconstitutedText = encoding.GetString(fileResult.FileContents);
if (contents != reconstitutedText)
{
throw new ActionResultAssertionException(string.Format(
"Expected file contents to be '{0}', but instead was '{1}'.",
contents,
reconstitutedText));
}
return fileResult;
}
public FileStreamResult ShouldRenderFileStream(byte[] content, string contentType = null)
{
var reconstitutedStream = new MemoryStream(content);
return ShouldRenderFileStream(reconstitutedStream, contentType);
}
public FileStreamResult ShouldRenderFileStream(Stream stream = null, string contentType = null)
{
ValidateActionReturnType<FileStreamResult>();
var fileResult = (FileStreamResult)ActionResult;
EnsureContentTypeIsSame(fileResult.ContentType, contentType);
if (stream != null)
{
byte[] expected = ConvertStreamToArray(stream);
byte[] actual = ConvertStreamToArray(fileResult.FileStream);
if (!expected.SequenceEqual(actual))
{
throw new ActionResultAssertionException(string.Format(
"Expected file contents to be equal to [{0}], but instead was given [{1}].",
string.Join(", ", expected),
string.Join(", ", actual)));
}
}
return fileResult;
}
public FileStreamResult ShouldRenderFileStream(string contents, string contentType = null, Encoding encoding = null)
{
ValidateActionReturnType<FileStreamResult>();
var fileResult = (FileStreamResult)ActionResult;
EnsureContentTypeIsSame(fileResult.ContentType, contentType);
if (encoding == null)
encoding = Encoding.UTF8;
var reconstitutedText = encoding.GetString(ConvertStreamToArray(fileResult.FileStream));
if (contents != reconstitutedText)
{
throw new ActionResultAssertionException(string.Format(
"Expected file contents to be '{0}', but instead was '{1}'.",
contents,
reconstitutedText));
}
return fileResult;
}
public FilePathResult ShouldRenderFilePath(string fileName = null, string contentType = null)
{
ValidateActionReturnType<FilePathResult>();
var fileResult = (FilePathResult)ActionResult;
EnsureContentTypeIsSame(fileResult.ContentType, contentType);
if (fileName != null && fileName != fileResult.FileName)
{
throw new ActionResultAssertionException(string.Format(
"Expected file name to be '{0}', but instead was given '{1}'.",
fileName,
fileResult.FileName));
}
return fileResult;
}
}
}