Skip to content

Commit

Permalink
Add convenience method for form content
Browse files Browse the repository at this point in the history
Add a convenience method for responding with URL-encoded form content, such as a response from an OAuth token endpoint.
  • Loading branch information
martincostello committed Jun 3, 2018
1 parent 4e52f0e commit ee37e5a
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/HttpClientInterception/HttpClientInterceptorOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ namespace JustEat.HttpClientInterception
/// </summary>
public class HttpClientInterceptorOptions
{
/// <summary>
/// The media type to use for a URL-encoded form.
/// </summary>
internal const string FormMediaType = "application/x-www-form-urlencoded";

/// <summary>
/// The media type to use for JSON.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
// Licensed under the Apache 2.0 license. See the LICENSE file in the project root for full license information.

using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;

namespace JustEat.HttpClientInterception
Expand Down Expand Up @@ -154,6 +156,44 @@ public static HttpRequestInterceptionBuilder WithContent(this HttpRequestInterce
return builder.WithContent(() => Encoding.UTF8.GetBytes(content ?? string.Empty));
}

/// <summary>
/// Sets the parameters to use as the form URL-encoded response content.
/// </summary>
/// <param name="builder">The <see cref="HttpRequestInterceptionBuilder"/> to use.</param>
/// <param name="parameters">The parameters to use for the form URL-encoded content.</param>
/// <returns>
/// The value specified by <paramref name="builder"/>.
/// </returns>
/// <exception cref="ArgumentNullException">
/// <paramref name="builder"/> or <paramref name="parameters"/> is <see langword="null"/>.
/// </exception>
public static HttpRequestInterceptionBuilder WithFormContent(
this HttpRequestInterceptionBuilder builder,
IEnumerable<KeyValuePair<string, string>> parameters)
{
if (builder == null)
{
throw new ArgumentNullException(nameof(builder));
}

if (parameters == null)
{
throw new ArgumentNullException(nameof(parameters));
}

Func<Task<byte[]>> contentFactory = async () =>
{
using (var content = new FormUrlEncodedContent(parameters))
{
return await content.ReadAsByteArrayAsync().ConfigureAwait(false);
}
};

return builder
.WithMediaType(HttpClientInterceptorOptions.FormMediaType)
.WithContent(contentFactory);
}

/// <summary>
/// Sets the object to use as the response content.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -775,6 +775,43 @@ public static void WithContent_Validates_Parameters()
Assert.Throws<ArgumentNullException>("builder", () => (null as HttpRequestInterceptionBuilder).WithContent(null as string));
}

[Fact]
public static void WithFormContent_Validates_Parameters()
{
// Arrange
var builder = new HttpRequestInterceptionBuilder();
IEnumerable<KeyValuePair<string, string>> parameters = null;

// Act and Assert
Assert.Throws<ArgumentNullException>("builder", () => (null as HttpRequestInterceptionBuilder).WithFormContent(parameters));
Assert.Throws<ArgumentNullException>("parameters", () => builder.WithFormContent(parameters));
}

[Fact]
public static async Task WithFormContent_Returns_Correct_Response()
{
// Arrange
var requestUri = "https://api.twitter.com/oauth/request_token";
var parameters = new Dictionary<string, string>()
{
{ "oauth_callback_confirmed", "true" },
{ "oauth_token", "a b c" },
{ "oauth_token_secret", "U5LJUL3eS+fl9bj9xqHKXyHpBc8=" },
};

var options = new HttpClientInterceptorOptions();
var builder = new HttpRequestInterceptionBuilder()
.Requests().ForPost().ForUrl(requestUri)
.Responds().WithFormContent(parameters)
.RegisterWith(options);

// Act
string actual = await HttpAssert.PostAsync(options, requestUri, new { }, mediaType: "application/x-www-form-urlencoded");

// Assert
actual.ShouldBe("oauth_callback_confirmed=true&oauth_token=a+b+c&oauth_token_secret=U5LJUL3eS%2Bfl9bj9xqHKXyHpBc8%3D");
}

[Fact]
public static void WithJsonContent_Validates_Parameters()
{
Expand Down

0 comments on commit ee37e5a

Please sign in to comment.