Skip to content

Commit 206c5d9

Browse files
committed
Merge pull request #30 from ByteBlast/master
Implemented ShouldRenderFileStream.
2 parents 49b5b84 + bc9ca73 commit 206c5d9

11 files changed

+362
-66
lines changed

BREAKING_CHANGES.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Version 2.0.0
2+
3+
## ShouldRenderFileStream Method
4+
5+
The following overload of the `ShouldRenderFileStream` method has been *replaced*:
6+
7+
public FileStreamResult ShouldRenderFileStream(string contentType = null)
8+
9+
We place emphasis on the word "replace" because it is important to note that this overload has not been removed but replaced - you will not encounter a compile-time error if you upgrade, but you will encounter a logical error when you run your existing test.
10+
11+
### Reason
12+
13+
The aforementioned overload has been replaced in order to enable an overload that takes a stream for comparison in a way that is consistent with the existing convention.
14+
15+
### Fix
16+
17+
Use a [named argument](http://msdn.microsoft.com/en-gb/library/dd264739.aspx).
18+
19+
As where you would have previously done this:
20+
21+
ShouldRenderFileStream("application/json");
22+
23+
You must now do this:
24+
25+
ShouldRenderFileStream(contentType: "application/json");
26+
27+
28+
## ShouldRenderFileMethod
29+
30+
The `ShouldRenderFile` method has been removed.
31+
32+
### Reason
33+
34+
The `ShouldRenderFile` method was ambiguous because it had the possibility to be interperted to test for a `FileResult` when in fact, it tested for a `FileContentResult`.
35+
36+
It is for this reason that we introduced two unequivocal methods namely, `ShouldRenderAnyFile` and `ShouldRenderFileContents`.
37+
38+
### Fix
39+
40+
Use the `ShouldRenderFileContents` method instead:
41+
42+
ShouldRenderAnyFile()
43+
ShouldRenderAnyFile(contentType: "application/json")
44+

NextVersion.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1.1.0
1+
2.0.0

TestStack.FluentMVCTesting.Mvc3/TestStack.FluentMVCTesting.Mvc3.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,8 @@
9494
<Compile Include="Properties\AssemblyInfo.cs" />
9595
</ItemGroup>
9696
<ItemGroup>
97-
<None Include="FluentMVCTesting.Mvc3.nuspec" />
9897
<None Include="packages.config" />
98+
<None Include="TestStack.FluentMVCTesting.Mvc3.nuspec" />
9999
</ItemGroup>
100100
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
101101
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
@@ -105,4 +105,4 @@
105105
<Target Name="AfterBuild">
106106
</Target>
107107
-->
108-
</Project>
108+
</Project>

TestStack.FluentMVCTesting.Mvc3/TestStack.FluentMVCTesting.Mvc3.nuspec

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@
3232
<dependencies>
3333
<dependency id="Microsoft.AspNet.Mvc" version="3.0" />
3434
</dependencies>
35+
<releaseNotes>
36+
For breaking changes from previous versions see
37+
https://github.com/TestStack/TestStack.FluentMVCTesting/blob/master/BREAKING_CHANGES.md
38+
</releaseNotes>
3539
<frameworkAssemblies>
3640
<frameworkAssembly assemblyName="System.Web" targetFramework="net40" />
3741
</frameworkAssemblies>
@@ -40,5 +44,6 @@
4044
<file src="bin\Release\TestStack.FluentMVCTesting.Mvc3.dll" target="lib\NET40" />
4145
<file src="bin\Release\TestStack.FluentMVCTesting.Mvc3.pdb" target="lib\NET40" />
4246
<file src="..\TestStack.FluentMvcTesting\**\*.cs" exclude="..\TestStack.FluentMvcTesting\obj\**\*.*" target="src" />
47+
<file src="..\TestStack.FluentMvcTesting\readme.txt" target="" />
4348
</files>
4449
</package>

TestStack.FluentMVCTesting.Tests/ControllerResultTestTests.cs

Lines changed: 200 additions & 21 deletions
Large diffs are not rendered by default.

TestStack.FluentMVCTesting.Tests/TestControllers/ControllerResultTestController.cs

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,11 @@ class ControllerResultTestController : Controller
1717
public const string JsonValue = "json";
1818
public const string FileName = "NamedFile";
1919
public static byte[] BinaryFileContents = { 1 };
20-
public static string TextualFileContents = "textual content";
20+
public static string TextualFileContent = "textual content";
21+
public static byte[] EmptyFileBuffer = { };
22+
public static readonly Stream EmptyStreamContents = new MemoryStream(EmptyFileBuffer);
23+
public static readonly Stream BinaryStreamContents = new MemoryStream(BinaryFileContents);
24+
2125
#endregion
2226

2327
#region Empty, Null and Random Results
@@ -174,14 +178,30 @@ public ActionResult TextualFile()
174178

175179
public ActionResult TextualFile(Encoding encoding)
176180
{
177-
var encodedContents = encoding.GetBytes(TextualFileContents);
181+
var encodedContents = encoding.GetBytes(TextualFileContent);
178182
return File(encodedContents, FileContentType);
179183
}
180184

181185
public ActionResult EmptyStream()
182186
{
183-
var content = new MemoryStream();
184-
return File(content, FileContentType);
187+
return File(EmptyStreamContents, FileContentType);
188+
}
189+
190+
public ActionResult BinaryStream()
191+
{
192+
return File(BinaryStreamContents, FileContentType);
193+
}
194+
195+
public ActionResult TextualStream()
196+
{
197+
return TextualStream(Encoding.UTF8);
198+
}
199+
200+
public ActionResult TextualStream(Encoding encoding)
201+
{
202+
var encondedContent = encoding.GetBytes(TextualFileContent);
203+
var stream = new MemoryStream(encondedContent);
204+
return File(stream, FileContentType);
185205
}
186206

187207
public ActionResult EmptyFilePath()

TestStack.FluentMVCTesting.sln

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
3-
# Visual Studio 2012
3+
# Visual Studio 2013
4+
VisualStudioVersion = 12.0.30501.0
5+
MinimumVisualStudioVersion = 10.0.40219.1
46
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestStack.FluentMVCTesting", "TestStack.FluentMVCTesting\TestStack.FluentMVCTesting.csproj", "{152CA00F-18D3-4CF5-8CA0-2C5B70CBEA19}"
57
EndProject
68
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestStack.FluentMVCTesting.Tests", "TestStack.FluentMVCTesting.Tests\TestStack.FluentMVCTesting.Tests.csproj", "{4DCC907C-A08A-4139-BB6B-2D34B21F318B}"

TestStack.FluentMvcTesting/ControllerResultTest.cs

Lines changed: 67 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.IO;
23
using System.Linq;
34
using System.Linq.Expressions;
45
using System.Net;
@@ -216,30 +217,42 @@ public ViewResultTest ShouldRenderDefaultPartialView()
216217

217218
#region File Results
218219

219-
public FileResult ShouldRenderAnyFile(string contentType = null)
220+
private static void EnsureContentTypeIsSame(string actual, string expected)
220221
{
221-
ValidateActionReturnType<FileResult>();
222-
223-
var fileResult = (FileResult)_actionResult;
222+
if (expected == null) return;
223+
if (actual != expected)
224+
{
225+
throw new ActionResultAssertionException(string.Format(
226+
"Expected file to be of content type '{0}', but instead was given '{1}'.", expected, actual));
227+
}
228+
}
224229

225-
if (contentType != null && fileResult.ContentType != contentType)
230+
private static byte[] ConvertStreamToArray(Stream stream)
231+
{
232+
using (var memoryStream = new MemoryStream())
226233
{
227-
throw new ActionResultAssertionException(string.Format("Expected file to be of content type '{0}', but instead was given '{1}'.", contentType, fileResult.ContentType));
234+
stream.CopyTo(memoryStream);
235+
stream.Position = 0;
236+
return memoryStream.ToArray();
228237
}
238+
}
239+
240+
public FileResult ShouldRenderAnyFile(string contentType = null)
241+
{
242+
ValidateActionReturnType<FileResult>();
243+
var fileResult = (FileResult)_actionResult;
229244

245+
EnsureContentTypeIsSame(fileResult.ContentType, contentType);
246+
230247
return fileResult;
231248
}
232249

233250
public FileContentResult ShouldRenderFileContents(byte[] contents = null, string contentType = null)
234251
{
235252
ValidateActionReturnType<FileContentResult>();
236-
237253
var fileResult = (FileContentResult) _actionResult;
238254

239-
if (contentType != null && fileResult.ContentType != contentType)
240-
{
241-
throw new ActionResultAssertionException(string.Format("Expected file to be of content type '{0}', but instead was given '{1}'.", contentType, fileResult.ContentType));
242-
}
255+
EnsureContentTypeIsSame(fileResult.ContentType, contentType);
243256

244257
if (contents != null && !fileResult.FileContents.SequenceEqual(contents))
245258
{
@@ -255,52 +268,72 @@ public FileContentResult ShouldRenderFileContents(byte[] contents = null, string
255268
public FileContentResult ShouldRenderFileContents(string contents, string contentType = null, Encoding encoding = null)
256269
{
257270
ValidateActionReturnType<FileContentResult>();
258-
259271
var fileResult = (FileContentResult)_actionResult;
260272

261-
if (contentType != null && fileResult.ContentType != contentType)
262-
{
263-
throw new ActionResultAssertionException(
264-
string.Format("Expected file to be of content type '{0}', but instead was given '{1}'.", contentType,
265-
fileResult.ContentType));
266-
}
273+
EnsureContentTypeIsSame(fileResult.ContentType, contentType);
267274

268275
if (encoding == null)
269276
encoding = Encoding.UTF8;
270277

271278
var reconstitutedText = encoding.GetString(fileResult.FileContents);
272279
if (contents != reconstitutedText)
273280
{
274-
throw new ActionResultAssertionException(string.Format("Expected file contents to be \"{0}\", but instead was \"{1}\".", contents, reconstitutedText));
281+
throw new ActionResultAssertionException(string.Format(
282+
"Expected file contents to be \"{0}\", but instead was \"{1}\".",
283+
contents,
284+
reconstitutedText));
275285
}
276286

277287
return fileResult;
278288
}
279289

280-
[Obsolete("Obsolete: Use ShouldRenderFileContents instead.")]
281-
public FileContentResult ShouldRenderFile(string contentType = null)
290+
public FileStreamResult ShouldRenderFileStream(byte[] content, string contentType = null)
282291
{
283-
ValidateActionReturnType<FileContentResult>();
292+
var reconstitutedStream = new MemoryStream(content);
293+
return ShouldRenderFileStream(reconstitutedStream, contentType);
294+
}
284295

285-
var fileResult = (FileContentResult)_actionResult;
296+
public FileStreamResult ShouldRenderFileStream(Stream stream = null, string contentType = null)
297+
{
298+
ValidateActionReturnType<FileStreamResult>();
299+
var fileResult = (FileStreamResult)_actionResult;
300+
301+
EnsureContentTypeIsSame(fileResult.ContentType, contentType);
286302

287-
if (contentType != null && fileResult.ContentType != contentType)
303+
if (stream != null)
288304
{
289-
throw new ActionResultAssertionException(string.Format("Expected file to be of content type '{0}', but instead was given '{1}'.", contentType, fileResult.ContentType));
305+
byte[] expected = ConvertStreamToArray(stream);
306+
byte[] actual = ConvertStreamToArray(fileResult.FileStream);
307+
308+
if (!expected.SequenceEqual(actual))
309+
{
310+
throw new ActionResultAssertionException(string.Format(
311+
"Expected file contents to be equal to [{0}], but instead was given [{1}].",
312+
string.Join(", ", expected),
313+
string.Join(", ", actual)));
314+
}
290315
}
291316

292317
return fileResult;
293318
}
294319

295-
public FileStreamResult ShouldRenderFileStream(string contentType = null)
320+
public FileStreamResult ShouldRenderFileStream(string contents, string contentType = null, Encoding encoding = null)
296321
{
297322
ValidateActionReturnType<FileStreamResult>();
298-
299323
var fileResult = (FileStreamResult)_actionResult;
300324

301-
if (contentType != null && fileResult.ContentType != contentType)
325+
EnsureContentTypeIsSame(fileResult.ContentType, contentType);
326+
327+
if (encoding == null)
328+
encoding = Encoding.UTF8;
329+
330+
var reconstitutedText = encoding.GetString(ConvertStreamToArray(fileResult.FileStream));
331+
if (contents != reconstitutedText)
302332
{
303-
throw new ActionResultAssertionException(string.Format("Expected file to be of content type '{0}', but instead was given '{1}'.", contentType, fileResult.ContentType));
333+
throw new ActionResultAssertionException(string.Format(
334+
"Expected file contents to be \"{0}\", but instead was \"{1}\".",
335+
contents,
336+
reconstitutedText));
304337
}
305338

306339
return fileResult;
@@ -309,17 +342,16 @@ public FileStreamResult ShouldRenderFileStream(string contentType = null)
309342
public FilePathResult ShouldRenderFilePath(string fileName = null, string contentType = null)
310343
{
311344
ValidateActionReturnType<FilePathResult>();
312-
313345
var fileResult = (FilePathResult)_actionResult;
314346

315-
if (contentType != null && fileResult.ContentType != contentType)
316-
{
317-
throw new ActionResultAssertionException(string.Format("Expected file to be of content type '{0}', but instead was given '{1}'.", contentType, fileResult.ContentType));
318-
}
347+
EnsureContentTypeIsSame(fileResult.ContentType, contentType);
319348

320349
if (fileName != null && fileName != fileResult.FileName)
321350
{
322-
throw new ActionResultAssertionException(string.Format("Expected file name to be '{0}', but instead was given '{1}'.", fileName, fileResult.FileName));
351+
throw new ActionResultAssertionException(string.Format(
352+
"Expected file name to be '{0}', but instead was given '{1}'.",
353+
fileName,
354+
fileResult.FileName));
323355
}
324356

325357
return fileResult;

TestStack.FluentMvcTesting/TestStack.FluentMVCTesting.csproj

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,11 @@
8383
<Compile Include="ViewResultTest.cs" />
8484
</ItemGroup>
8585
<ItemGroup>
86-
<None Include="FluentMVCTesting.nuspec" />
8786
<None Include="packages.config" />
87+
<None Include="TestStack.FluentMVCTesting.nuspec" />
88+
</ItemGroup>
89+
<ItemGroup>
90+
<Content Include="readme.txt" />
8891
</ItemGroup>
8992
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
9093
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
@@ -94,4 +97,4 @@
9497
<Target Name="AfterBuild">
9598
</Target>
9699
-->
97-
</Project>
100+
</Project>

TestStack.FluentMvcTesting/TestStack.FluentMVCTesting.nuspec

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@
3232
<dependencies>
3333
<dependency id="Microsoft.AspNet.Mvc" version="4.0" />
3434
</dependencies>
35+
<releaseNotes>
36+
For breaking changes from previous versions see
37+
https://github.com/TestStack/TestStack.FluentMVCTesting/blob/master/BREAKING_CHANGES.md
38+
</releaseNotes>
3539
<frameworkAssemblies>
3640
<frameworkAssembly assemblyName="System.Web" targetFramework="net40" />
3741
</frameworkAssemblies>
@@ -40,5 +44,6 @@
4044
<file src="bin\Release\TestStack.FluentMVCTesting.dll" target="lib\NET40" />
4145
<file src="bin\Release\TestStack.FluentMVCTesting.pdb" target="lib\NET40" />
4246
<file src="**\*.cs" exclude="obj\**\*.*" target="src" />
47+
<file src="readme.txt" target="" />
4348
</files>
4449
</package>

0 commit comments

Comments
 (0)