Skip to content

Commit

Permalink
Merge pull request #103 from jgiacomini/develop
Browse files Browse the repository at this point in the history
Bump 1.7
  • Loading branch information
jgiacomini authored Nov 1, 2019
2 parents 7eec17f + 5085b5f commit 05f972f
Show file tree
Hide file tree
Showing 12 changed files with 482 additions and 50 deletions.
36 changes: 36 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,42 @@ Define the timeout for one request
```cs
request.WithTimeout(TimeSpan.FromSeconds(100));
```
### Allow non http 2xx responses

#### Globaly

Allow any status codes :
```cs
client.Settings.HttpStatusCodeAllowed.AllowAnyStatus = true;
```

Allow only a range of http status codes :
```cs
client.Settings.HttpStatusCodeAllowed.Add(new HttpStatusRange(400, 420));
```

or

```cs
client.Settings.HttpStatusCodeAllowed.Add(new HttpStatusRange(System.Net.HttpStatusCode.BadRequest, System.Net.HttpStatusCode.BadGateway));
```

#### By request

Allow all status code :
```cs
request.AllowAllHttpStatusCode().ExecuteAsync();
```

Allow only a range of http status codes :
```cs
request.AllowRangeHttpStatusCode(400, 420).ExecuteAsync();
```

Allow only on stats code of http status codes :
```cs
request.AllowSpecificHttpStatusCode(409).ExecuteAsync();
```

### Download file
```cs
Expand Down
42 changes: 42 additions & 0 deletions RELEASE-NOTES.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,46 @@
# Release notes
# 1.7.0
* Add possibility for request with content (like POST, PUT ...) to use methods : WithBasicAuthentication WithOAuthBearer WithTimeout WithETagContainer.
* Allow non 2xx response (see sample below)

## Allow non http 2xx responses

#### Globaly

Allow any status codes :
```cs
client.Settings.HttpStatusCodeAllowed.AllowAnyStatus = true;
```

Allow only a range of http status codes :
```cs
client.Settings.HttpStatusCodeAllowed.Add(new HttpStatusRange(400, 420));
```

or

```cs
client.Settings.HttpStatusCodeAllowed.Add(new HttpStatusRange(System.Net.HttpStatusCode.BadRequest, System.Net.HttpStatusCode.BadGateway));
```

#### By request

Allow all status code :
```cs
request.AllowAllHttpStatusCode().ExecuteAsync();
```

Allow only a range of http status codes :
```cs
request.AllowRangeHttpStatusCode(400, 420).ExecuteAsync();
```

Allow only on stats code of http status codes :
```cs
request.AllowSpecificHttpStatusCode(409).ExecuteAsync();
```


# 1.6.6
* Add icon and license in nuget package

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.IO;
Expand All @@ -14,6 +15,18 @@ public GetTestController()
{
}

[HttpGet("Status500Response")]
public IActionResult Status500Response()
{
return StatusCode(StatusCodes.Status500InternalServerError, new string[] { "value1", "value2" });
}

[HttpGet("Status409Response")]
public IActionResult Status409Response()
{
return StatusCode(StatusCodes.Status409Conflict, new string[] { "value1", "value2" });
}

[HttpGet("NoResponse")]
public Task NoResponse()
{
Expand Down
114 changes: 114 additions & 0 deletions Tests/Tiny.RestClient.Tests/StatusRangeTests.cs
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
}
}
64 changes: 64 additions & 0 deletions Tiny.RestClient/HttpStatusRange/HttpStatusRange.cs
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; }
}
}
35 changes: 35 additions & 0 deletions Tiny.RestClient/HttpStatusRange/HttpStatusRanges.cs
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);
}
}
}
Loading

0 comments on commit 05f972f

Please sign in to comment.