Skip to content
This repository has been archived by the owner on Jan 24, 2021. It is now read-only.

Commit

Permalink
Merge pull request #1656 from StephenFriend/redirectStatusCodes
Browse files Browse the repository at this point in the history
BrowserResponseExtensions - extended range of valid HttpStatusCodes
  • Loading branch information
thecodejunkie committed Aug 16, 2014
2 parents ebac1d8 + 8ceabb6 commit ab24dac
Showing 1 changed file with 22 additions and 13 deletions.
35 changes: 22 additions & 13 deletions src/Nancy.Testing/BrowserResponseExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
namespace Nancy.Testing
{
using System;
using System.IO;
using System.Xml.Linq;
using System.IO;
using System.Linq;
using System.Xml.Linq;

/// <summary>
/// Defines extensions for the <see cref="BrowserResponse"/> type.
Expand All @@ -17,9 +18,17 @@ public static class BrowserResponseExtensions
/// <param name="stringComparer">The string comparer that should be used by the assertion. The default value is <see cref="StringComparison.InvariantCulture"/>.</param>
public static void ShouldHaveRedirectedTo(this BrowserResponse response, string location, StringComparison stringComparer = StringComparison.InvariantCulture)
{
if (response.StatusCode != HttpStatusCode.SeeOther)
var validRedirectStatuses = new[]
{
throw new AssertException("Status code should be SeeOther");
HttpStatusCode.MovedPermanently,
HttpStatusCode.SeeOther,
HttpStatusCode.TemporaryRedirect
};

if (!validRedirectStatuses.Any(x => x == response.StatusCode))
{
throw new AssertException(
String.Format("Status code should be one of 'MovedPermanently, SeeOther, TemporaryRedirect', but was {0}.", response.StatusCode));
}

if (!response.Headers["Location"].Equals(location, stringComparer))
Expand All @@ -28,14 +37,14 @@ public static void ShouldHaveRedirectedTo(this BrowserResponse response, string
}
}

public static XDocument BodyAsXml(this BrowserResponse response)
{
using (var contentsStream = new MemoryStream())
{
response.Context.Response.Contents.Invoke(contentsStream);
contentsStream.Position = 0;
return XDocument.Load(contentsStream);
}
}
public static XDocument BodyAsXml(this BrowserResponse response)
{
using (var contentsStream = new MemoryStream())
{
response.Context.Response.Contents.Invoke(contentsStream);
contentsStream.Position = 0;
return XDocument.Load(contentsStream);
}
}
}
}

0 comments on commit ab24dac

Please sign in to comment.