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

Commit

Permalink
Merge pull request #462 from georgehemmings/master
Browse files Browse the repository at this point in the history
Allow inspection of request body when mocking HTTP requests
  • Loading branch information
mythz committed Nov 21, 2015
2 parents 7984fca + 5845b0e commit 1ec026c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 16 deletions.
20 changes: 10 additions & 10 deletions src/ServiceStack.Text/HttpUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@ public static string SendStringToUrl(this string url, string method = null,

if (ResultsFilter != null)
{
return ResultsFilter.GetString(webReq);
return ResultsFilter.GetString(webReq, requestBody);
}

if (requestBody != null)
Expand Down Expand Up @@ -497,7 +497,7 @@ public static byte[] SendBytesToUrl(this string url, string method = null,

if (ResultsFilter != null)
{
return ResultsFilter.GetBytes(webReq);
return ResultsFilter.GetBytes(webReq, requestBody);
}

if (requestBody != null)
Expand Down Expand Up @@ -800,8 +800,8 @@ public static string PutXmlToUrl(this string url, object data,

public interface IHttpResultsFilter : IDisposable
{
string GetString(HttpWebRequest webReq);
byte[] GetBytes(HttpWebRequest webReq);
string GetString(HttpWebRequest webReq, string reqBody);
byte[] GetBytes(HttpWebRequest webReq, byte[] reqBody);
void UploadStream(HttpWebRequest webRequest, Stream fileStream, string fileName);
}

Expand All @@ -812,8 +812,8 @@ public class HttpResultsFilter : IHttpResultsFilter
public string StringResult { get; set; }
public byte[] BytesResult { get; set; }

public Func<HttpWebRequest, string> StringResultFn { get; set; }
public Func<HttpWebRequest, byte[]> BytesResultFn { get; set; }
public Func<HttpWebRequest, string, string> StringResultFn { get; set; }
public Func<HttpWebRequest, byte[], byte[]> BytesResultFn { get; set; }
public Action<HttpWebRequest, Stream, string> UploadFileFn { get; set; }

public HttpResultsFilter(string stringResult=null, byte[] bytesResult=null)
Expand All @@ -830,17 +830,17 @@ public void Dispose()
HttpUtils.ResultsFilter = previousFilter;
}

public string GetString(HttpWebRequest webReq)
public string GetString(HttpWebRequest webReq, string reqBody)
{
return StringResultFn != null
? StringResultFn(webReq)
? StringResultFn(webReq, reqBody)
: StringResult;
}

public byte[] GetBytes(HttpWebRequest webReq)
public byte[] GetBytes(HttpWebRequest webReq, byte[] reqBody)
{
return BytesResultFn != null
? BytesResultFn(webReq)
? BytesResultFn(webReq, reqBody)
: BytesResult;
}

Expand Down
26 changes: 20 additions & 6 deletions tests/ServiceStack.Text.Tests/HttpUtilsMockTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,16 +82,23 @@ public void Can_Mock_StringFn_Api_responses()
{
using (new HttpResultsFilter
{
StringResultFn = webReq => webReq.RequestUri.ToString().Contains("google")
? "mocked-google"
: "mocked-yahoo"
StringResultFn = (webReq, reqBody) =>
{
if (reqBody != null && reqBody.Contains("{\"a\":1}")) return "mocked-by-body";
return webReq.RequestUri.ToString().Contains("google")
? "mocked-google"
: "mocked-yahoo";
}
})
{
Assert.That(ExampleGoogleUrl.GetJsonFromUrl(), Is.EqualTo("mocked-google"));
Assert.That(ExampleYahooUrl.GetJsonFromUrl(), Is.EqualTo("mocked-yahoo"));

Assert.That(ExampleGoogleUrl.PostJsonToUrl(json: "{\"postdata\":1}"), Is.EqualTo("mocked-google"));
Assert.That(ExampleYahooUrl.PostJsonToUrl(json: "{\"postdata\":1}"), Is.EqualTo("mocked-yahoo"));

Assert.That(ExampleYahooUrl.PostJsonToUrl(json: "{\"a\":1}"), Is.EqualTo("mocked-by-body"));
}
}

Expand All @@ -100,16 +107,23 @@ public void Can_Mock_BytesFn_Api_responses()
{
using (new HttpResultsFilter
{
BytesResultFn = webReq => webReq.RequestUri.ToString().Contains("google")
? "mocked-google".ToUtf8Bytes()
: "mocked-yahoo".ToUtf8Bytes()
BytesResultFn = (webReq, reqBody) =>
{
if (reqBody != null && reqBody.FromUtf8Bytes().Contains("{\"a\":1}")) return "mocked-by-body".ToUtf8Bytes();
return webReq.RequestUri.ToString().Contains("google")
? "mocked-google".ToUtf8Bytes()
: "mocked-yahoo".ToUtf8Bytes();
}
})
{
Assert.That(ExampleGoogleUrl.GetBytesFromUrl(), Is.EqualTo("mocked-google".ToUtf8Bytes()));
Assert.That(ExampleYahooUrl.GetBytesFromUrl(), Is.EqualTo("mocked-yahoo".ToUtf8Bytes()));

Assert.That(ExampleGoogleUrl.PostBytesToUrl(requestBody: "postdata=1".ToUtf8Bytes()), Is.EqualTo("mocked-google".ToUtf8Bytes()));
Assert.That(ExampleYahooUrl.PostBytesToUrl(requestBody: "postdata=1".ToUtf8Bytes()), Is.EqualTo("mocked-yahoo".ToUtf8Bytes()));

Assert.That(ExampleYahooUrl.PostBytesToUrl(requestBody: "{\"a\":1}".ToUtf8Bytes()), Is.EqualTo("mocked-by-body".ToUtf8Bytes()));
}
}

Expand Down

0 comments on commit 1ec026c

Please sign in to comment.