Skip to content

Commit

Permalink
MockedRequestExtensions: Allow response headers to be added
Browse files Browse the repository at this point in the history
  • Loading branch information
esskar authored and richardszalay committed Mar 11, 2018
1 parent 2224dc7 commit 9cb4f4a
Show file tree
Hide file tree
Showing 2 changed files with 247 additions and 12 deletions.
161 changes: 149 additions & 12 deletions RichardSzalay.MockHttp.Shared/MockedRequestExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
using RichardSzalay.MockHttp.Matchers;

namespace RichardSzalay.MockHttp
Expand Down Expand Up @@ -279,9 +279,29 @@ public static MockedRequest Respond(this MockedRequest source, HttpStatusCode st
/// <param name="content">The content of the response</param>
public static MockedRequest Respond(this MockedRequest source, HttpStatusCode statusCode, HttpContent content)
{
return source.Respond(req => new HttpResponseMessage(statusCode)
return source.Respond(statusCode, Enumerable.Empty<KeyValuePair<string, string>>(), content);
}

/// <summary>
/// Sets the response of the current <see cref="T:MockedRequest"/>
/// </summary>
/// <param name="source">The source mocked request</param>
/// <param name="statusCode">The <see cref="T:HttpStatusCode"/> of the response</param>
/// <param name="headers">A list of HTTP header name/value pairs to add to the response.</param>
/// <param name="content">The content of the response</param>
public static MockedRequest Respond(this MockedRequest source, HttpStatusCode statusCode, IEnumerable<KeyValuePair<string, string>> headers, HttpContent content)
{
return source.Respond(req =>
{
Content = content
var res = new HttpResponseMessage(statusCode)
{
Content = content
};
foreach (var header in headers)
{
res.Headers.TryAddWithoutValidation(header.Key, header.Value);
}
return res;
});
}

Expand All @@ -295,16 +315,40 @@ public static MockedRequest Respond(this MockedRequest source, HttpContent conte
return source.Respond(HttpStatusCode.OK, content);
}

/// <summary>
/// Sets the response of the current <see cref="T:MockedRequest"/>, with an OK (200) status code
/// </summary>
/// <param name="source">The source mocked request</param>
/// <param name="headers">A list of HTTP header name/value pairs to add to the response.</param>
/// <param name="content">The content of the response</param>
public static MockedRequest Respond(this MockedRequest source, IEnumerable<KeyValuePair<string, string>> headers, HttpContent content)
{
return source.Respond(HttpStatusCode.OK, headers, content);
}

/// <summary>
/// Sets the response of the current <see cref="T:MockedRequest"/>
/// </summary>
/// <param name="source">The source mocked request</param>
/// <param name="statusCode">The <see cref="T:HttpStatusCode"/> of the response</param>
/// <param name="content">The content of the response</param>
/// <param name="mediaType">The media type of the response</param>
/// <param name="content">The content of the response</param>
public static MockedRequest Respond(this MockedRequest source, HttpStatusCode statusCode, string mediaType, string content)
{
return source.Respond(statusCode, _ => new StringContent(content, Encoding.UTF8, mediaType));
return source.Respond(statusCode, Enumerable.Empty<KeyValuePair<string, string>>(), mediaType, content);
}

/// <summary>
/// Sets the response of the current <see cref="T:MockedRequest"/>
/// </summary>
/// <param name="source">The source mocked request</param>
/// <param name="statusCode">The <see cref="T:HttpStatusCode"/> of the response</param>
/// <param name="headers">A list of HTTP header name/value pairs to add to the response.</param>
/// <param name="mediaType">The media type of the response</param>
/// <param name="content">The content of the response</param>
public static MockedRequest Respond(this MockedRequest source, HttpStatusCode statusCode, IEnumerable<KeyValuePair<string, string>> headers, string mediaType, string content)
{
return source.Respond(statusCode, headers, _ => new StringContent(content, Encoding.UTF8, mediaType));
}

/// <summary>
Expand All @@ -318,6 +362,18 @@ public static MockedRequest Respond(this MockedRequest source, string mediaType,
return source.Respond(HttpStatusCode.OK, mediaType, content);
}

/// <summary>
/// Sets the response of the current <see cref="T:MockedRequest"/>, with an OK (200) status code
/// </summary>
/// <param name="source">The source mocked request</param>
/// <param name="headers">A list of HTTP header name/value pairs to add to the response.</param>
/// <param name="content">The content of the response</param>
/// <param name="mediaType">The media type of the response</param>
public static MockedRequest Respond(this MockedRequest source, IEnumerable<KeyValuePair<string, string>> headers, string mediaType, string content)
{
return source.Respond(HttpStatusCode.OK, headers, mediaType, content);
}

/// <summary>
/// Sets the response of the current <see cref="T:MockedRequest"/>
/// </summary>
Expand All @@ -327,7 +383,20 @@ public static MockedRequest Respond(this MockedRequest source, string mediaType,
/// <param name="mediaType">The media type of the response</param>
public static MockedRequest Respond(this MockedRequest source, HttpStatusCode statusCode, string mediaType, Stream content)
{
return source.Respond(statusCode, _ =>
return source.Respond(statusCode, Enumerable.Empty<KeyValuePair<string, string>>(), mediaType, content);
}

/// <summary>
/// Sets the response of the current <see cref="T:MockedRequest"/>
/// </summary>
/// <param name="source">The source mocked request</param>
/// <param name="statusCode">The <see cref="T:HttpStatusCode"/> of the response</param>
/// <param name="headers">A list of HTTP header name/value pairs to add to the response.</param>
/// <param name="content">The content of the response</param>
/// <param name="mediaType">The media type of the response</param>
public static MockedRequest Respond(this MockedRequest source, HttpStatusCode statusCode, IEnumerable<KeyValuePair<string, string>> headers, string mediaType, Stream content)
{
return source.Respond(statusCode, headers, _ =>
{
if (content.CanSeek)
{
Expand All @@ -339,7 +408,7 @@ public static MockedRequest Respond(this MockedRequest source, HttpStatusCode st
ms.Seek(0L, SeekOrigin.Begin);

var streamContent = new StreamContent(ms);
streamContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(mediaType);
streamContent.Headers.ContentType = new MediaTypeHeaderValue(mediaType);

return streamContent;
});
Expand All @@ -356,6 +425,18 @@ public static MockedRequest Respond(this MockedRequest source, string mediaType,
return source.Respond(HttpStatusCode.OK, mediaType, handler);
}

/// <summary>
/// Sets the response of the current <see cref="T:MockedRequest"/>
/// </summary>
/// <param name="source">The source mocked request</param>
/// <param name="headers">A list of HTTP header name/value pairs to add to the response.</param>
/// <param name="mediaType">The media type of the response</param>
/// <param name="handler">A delegate that will return a content stream at runtime</param>
public static MockedRequest Respond(this MockedRequest source, IEnumerable<KeyValuePair<string, string>> headers, string mediaType, Func<HttpRequestMessage, Stream> handler)
{
return source.Respond(HttpStatusCode.OK, headers, mediaType, handler);
}

/// <summary>
/// Sets the response of the current <see cref="T:MockedRequest"/>
/// </summary>
Expand All @@ -365,12 +446,25 @@ public static MockedRequest Respond(this MockedRequest source, string mediaType,
/// <param name="handler">A delegate that will return a content stream at runtime</param>
public static MockedRequest Respond(this MockedRequest source, HttpStatusCode statusCode, string mediaType, Func<HttpRequestMessage, Stream> handler)
{
return source.Respond(statusCode, request =>
return source.Respond(statusCode, Enumerable.Empty<KeyValuePair<string, string>>(), mediaType, handler);
}

/// <summary>
/// Sets the response of the current <see cref="T:MockedRequest"/>
/// </summary>
/// <param name="source">The source mocked request</param>
/// <param name="statusCode">The <see cref="T:HttpStatusCode"/> of the response</param>
/// <param name="headers">A list of HTTP header name/value pairs to add to the response.</param>
/// <param name="mediaType">The media type of the response</param>
/// <param name="handler">A delegate that will return a content stream at runtime</param>
public static MockedRequest Respond(this MockedRequest source, HttpStatusCode statusCode, IEnumerable<KeyValuePair<string, string>> headers, string mediaType, Func<HttpRequestMessage, Stream> handler)
{
return source.Respond(statusCode, headers, request =>
{
var content = handler(request);

var streamContent = new StreamContent(content);
streamContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(mediaType);
streamContent.Headers.ContentType = new MediaTypeHeaderValue(mediaType);

return streamContent;
});
Expand All @@ -387,6 +481,18 @@ public static MockedRequest Respond(this MockedRequest source, string mediaType,
return source.Respond(HttpStatusCode.OK, mediaType, content);
}

/// <summary>
/// Sets the response of the current <see cref="T:MockedRequest"/>, with an OK (200) status code
/// </summary>
/// <param name="source">The source mocked request</param>
/// <param name="headers">A list of HTTP header name/value pairs to add to the response.</param>
/// <param name="content">The content of the response</param>
/// <param name="mediaType">The media type of the response</param>
public static MockedRequest Respond(this MockedRequest source, IEnumerable<KeyValuePair<string, string>> headers, string mediaType, Stream content)
{
return source.Respond(HttpStatusCode.OK, headers, mediaType, content);
}

/// <summary>
/// Sets the response of the current <see cref="T:MockedRequest"/>
/// </summary>
Expand All @@ -397,6 +503,17 @@ public static MockedRequest Respond(this MockedRequest source, Func<HttpRequestM
return source.Respond(HttpStatusCode.OK, handler);
}

/// <summary>
/// Sets the response of the current <see cref="T:MockedRequest"/>
/// </summary>
/// <param name="source">The source mocked request</param>
/// <param name="headers">A list of HTTP header name/value pairs to add to the response.</param>
/// <param name="handler">The delegate that will return a <see cref="T:HttpContent"/> determined at runtime</param>
public static MockedRequest Respond(this MockedRequest source, IEnumerable<KeyValuePair<string, string>> headers, Func<HttpRequestMessage, HttpContent> handler)
{
return source.Respond(HttpStatusCode.OK, headers, handler);
}

/// <summary>
/// Sets the response of the current <see cref="T:MockedRequest"/>
/// </summary>
Expand All @@ -405,9 +522,29 @@ public static MockedRequest Respond(this MockedRequest source, Func<HttpRequestM
/// <param name="handler">The delegate that will return a <see cref="T:HttpContent"/> determined at runtime</param>
public static MockedRequest Respond(this MockedRequest source, HttpStatusCode statusCode, Func<HttpRequestMessage, HttpContent> handler)
{
return source.Respond(req => new HttpResponseMessage(statusCode)
return source.Respond(statusCode, Enumerable.Empty<KeyValuePair<string, string>>(), handler);
}

/// <summary>
/// Sets the response of the current <see cref="T:MockedRequest"/>
/// </summary>
/// <param name="source">The source mocked request</param>
/// <param name="statusCode">The <see cref="T:HttpStatusCode"/> of the response</param>
/// <param name="headers">A list of HTTP header name/value pairs to add to the response.</param>
/// <param name="handler">The delegate that will return a <see cref="T:HttpContent"/> determined at runtime</param>
public static MockedRequest Respond(this MockedRequest source, HttpStatusCode statusCode, IEnumerable<KeyValuePair<string, string>> headers, Func<HttpRequestMessage, HttpContent> handler)
{
return source.Respond(req =>
{
Content = handler(req)
var res = new HttpResponseMessage(statusCode)
{
Content = handler(req),
};
foreach (var header in headers)
{
res.Headers.TryAddWithoutValidation(header.Key, header.Value);
}
return res;
});
}

Expand Down Expand Up @@ -461,7 +598,7 @@ private static HttpRequestMessage CloneRequest(HttpRequestMessage message)

cloned.Content = message.Content;

foreach(var header in message.Headers)
foreach (var header in message.Headers)
{
cloned.Headers.Add(header.Key, header.Value);
}
Expand Down
Loading

0 comments on commit 9cb4f4a

Please sign in to comment.