Add AllowHttpStatus(HttpStatusCode)

It would be nice to not hard code the status code string
and instead use the HttpStatusCode enum when a pattern is not needed.

e.g. "http://example.com/foo/1"
        .AllowHttpStatus(HttpStatusCode.NotFound)
        .DeleteAsync()
This commit is contained in:
Carolyn Van Slyck 2015-06-25 09:14:45 -05:00
parent ecacdd70c4
commit 875c51487e
2 changed files with 33 additions and 2 deletions

View File

@ -263,6 +263,15 @@ namespace Flurl.Http
return client;
}
/// <summary>
/// Adds an <see cref="HttpStatusCode"/> which (in addtion to 2xx) will NOT result in a FlurlHttpException being thrown.
/// </summary>
/// <param name="statusCode">Examples: HttpStatusCode.NotFound</param>
/// <returns>The modified FlurlClient.</returns>
public static FlurlClient AllowHttpStatus(this FlurlClient client, HttpStatusCode statusCode) {
return AllowHttpStatus(client, ((int)statusCode).ToString());
}
/// <summary>
/// Creates a FlurlClient from the URL and adds a pattern representing an HTTP status code or range of codes which (in addtion to 2xx) will NOT result in a FlurlHttpException being thrown.
/// </summary>
@ -279,7 +288,16 @@ namespace Flurl.Http
/// <returns>The new FlurlClient.</returns>
public static FlurlClient AllowHttpStatus(this string url, string pattern) {
return new FlurlClient(url, true).AllowHttpStatus(pattern);
}
}
/// <summary>
/// Adds an <see cref="HttpStatusCode"/> which (in addtion to 2xx) will NOT result in a FlurlHttpException being thrown.
/// </summary>
/// <param name="statusCode">Examples: HttpStatusCode.NotFound</param>
/// <returns>The new FlurlClient.</returns>
public static FlurlClient AllowHttpStatus(this string url, HttpStatusCode statusCode) {
return new FlurlClient(url, true).AllowHttpStatus(statusCode);
}
/// <summary>
/// Prevents a FlurlHttpException from being thrown on any completed response, regardless of the HTTP status code.

View File

@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using Flurl.Http;
using Flurl.Http.Testing;
@ -113,6 +114,18 @@ namespace Flurl.Test.Http
client.AllowedHttpStatusRanges.Clear();
await client.GetAsync();
}
}
}
[Test]
public async Task can_allow_specific_http_status()
{
using (var test = new HttpTest())
{
test.RespondWith(404, "Nothing to see here");
// allow 404
var client = "http://www.api.com".AllowHttpStatus(HttpStatusCode.NotFound);
await client.DeleteAsync();
}
}
}
}