-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #103 from jgiacomini/develop
Bump 1.7
- Loading branch information
Showing
12 changed files
with
482 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace Tiny.RestClient.Tests | ||
{ | ||
[TestClass] | ||
public class StatusRangeTests : BaseTest | ||
{ | ||
#region Client scope | ||
[ExpectedException(typeof(HttpException))] | ||
[TestMethod] | ||
public async Task GetErrorWhenCallApiWhenError500() | ||
{ | ||
var client = GetClient(); | ||
|
||
try | ||
{ | ||
var response = await client. | ||
GetRequest("GetTest/Status500Response"). | ||
ExecuteAsync<IEnumerable<string>>(); | ||
} | ||
catch (HttpException ex) | ||
{ | ||
Assert.AreEqual(System.Net.HttpStatusCode.InternalServerError, ex.StatusCode); | ||
|
||
throw ex; | ||
} | ||
} | ||
|
||
[TestMethod] | ||
public async Task GetAnyStatusResponseAllowed() | ||
{ | ||
var client = GetNewClient(); | ||
client.Settings.HttpStatusCodeAllowed.AllowAnyStatus = true; | ||
|
||
var response = await client. | ||
GetRequest("GetTest/Status500Response"). | ||
ExecuteAsync<IEnumerable<string>>(); | ||
Assert.IsNotNull(response); | ||
} | ||
|
||
[TestMethod] | ||
public async Task GetRangeOfStatusesAllowed() | ||
{ | ||
var client = GetNewClient(); | ||
client.Settings.HttpStatusCodeAllowed.Add( | ||
new HttpStatusRange( | ||
System.Net.HttpStatusCode.BadRequest, // 400 | ||
System.Net.HttpStatusCode.BadGateway)); // 502 | ||
var response = await client. | ||
GetRequest("GetTest/Status409Response"). | ||
ExecuteAsync<IEnumerable<string>>(); | ||
Assert.IsNotNull(response); | ||
} | ||
|
||
[TestMethod] | ||
public async Task GetSpecificStatusResponseAllowed() | ||
{ | ||
var client = GetNewClient(); | ||
client.Settings.HttpStatusCodeAllowed.Add(new HttpStatusRange(System.Net.HttpStatusCode.Conflict)); | ||
var response = await client. | ||
GetRequest("GetTest/Status409Response"). | ||
ExecuteAsync<IEnumerable<string>>(); | ||
Assert.IsNotNull(response); | ||
} | ||
|
||
[ExpectedException(typeof(ArgumentException))] | ||
[TestMethod] | ||
public void AddInvalidStatusRange() | ||
{ | ||
var client = GetNewClient(); | ||
client.Settings.HttpStatusCodeAllowed.Add(new HttpStatusRange(500, 400)); | ||
} | ||
#endregion | ||
|
||
#region Request scope | ||
[TestMethod] | ||
public async Task ForRequest_GetAnyStatusResponseAllowed() | ||
{ | ||
var client = GetClient(); | ||
var response = await client. | ||
GetRequest("GetTest/Status500Response"). | ||
AllowAnyHttpStatusCode(). | ||
ExecuteAsync<IEnumerable<string>>(); | ||
Assert.IsNotNull(response); | ||
} | ||
|
||
[TestMethod] | ||
public async Task ForRequest_GetRangeOfStatusesResponseAllowed() | ||
{ | ||
var client = GetClient(); | ||
var response = await client. | ||
GetRequest("GetTest/Status409Response"). | ||
AllowRangeHttpStatusCode(System.Net.HttpStatusCode.BadRequest, System.Net.HttpStatusCode.BadGateway). | ||
ExecuteAsync<IEnumerable<string>>(); | ||
Assert.IsNotNull(response); | ||
} | ||
|
||
[TestMethod] | ||
public async Task ForRequest_GetSpecificStatusResponseAllowed() | ||
{ | ||
var client = GetClient(); | ||
var response = await client. | ||
GetRequest("GetTest/Status409Response"). | ||
AllowSpecificHttpStatusCode(System.Net.HttpStatusCode.Conflict). | ||
ExecuteAsync<IEnumerable<string>>(); | ||
Assert.IsNotNull(response); | ||
} | ||
#endregion | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
using System; | ||
using System.Net; | ||
|
||
namespace Tiny.RestClient | ||
{ | ||
/// <summary> | ||
/// Represent a range of http code. | ||
/// </summary> | ||
public class HttpStatusRange | ||
{ | ||
/// <summary> | ||
/// Contruct a status range. | ||
/// </summary> | ||
/// <param name="minHttpStatus">min status range.</param> | ||
/// <param name="maxHttpStatus">max status range.</param> | ||
public HttpStatusRange(HttpStatusCode minHttpStatus, HttpStatusCode maxHttpStatus) | ||
: this((int)minHttpStatus, (int)maxHttpStatus) | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// Contruct a status range. | ||
/// </summary> | ||
/// <param name="minHttpStatus">min status range.</param> | ||
/// <param name="maxHttpStatus">max status range.</param> | ||
public HttpStatusRange(int minHttpStatus, int maxHttpStatus) | ||
{ | ||
MinHttpStatus = minHttpStatus; | ||
MaxHttpStatus = maxHttpStatus; | ||
if (maxHttpStatus < minHttpStatus) | ||
{ | ||
throw new ArgumentException($"{nameof(maxHttpStatus)} must be superior or egual to {nameof(minHttpStatus)}"); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Contruct a status range. | ||
/// </summary> | ||
/// <param name="httpStatusAllowed">httpStatus allowed.</param> | ||
public HttpStatusRange(HttpStatusCode httpStatusAllowed) | ||
: this((int)httpStatusAllowed) | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// Contruct a status range. | ||
/// </summary> | ||
/// <param name="httpStatusAllowed">httpStatus allowed.</param> | ||
public HttpStatusRange(int httpStatusAllowed) | ||
: this(httpStatusAllowed, httpStatusAllowed) | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// Min http status. | ||
/// </summary> | ||
public int MinHttpStatus { get; private set; } | ||
|
||
/// <summary> | ||
/// MAx http status. | ||
/// </summary> | ||
public int MaxHttpStatus { get; private set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
namespace Tiny.RestClient | ||
{ | ||
/// <summary> | ||
/// Represent a collection ofstatus range. | ||
/// </summary> | ||
public class HttpStatusRanges : List<HttpStatusRange> | ||
{ | ||
internal HttpStatusRanges() | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// Allow all statuses codes. | ||
/// </summary> | ||
public bool AllowAnyStatus { get; set; } | ||
|
||
/// <summary> | ||
/// Check if httpStatus is allowed. | ||
/// </summary> | ||
/// <param name="statusCode">status code to check.</param> | ||
/// <returns></returns> | ||
public bool CheckIfHttpStatusIsAllowed(int statusCode) | ||
{ | ||
if (AllowAnyStatus) | ||
{ | ||
return true; | ||
} | ||
|
||
return this.Any(r => r.MinHttpStatus <= statusCode && r.MaxHttpStatus >= statusCode); | ||
} | ||
} | ||
} |
Oops, something went wrong.