-
Notifications
You must be signed in to change notification settings - Fork 496
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Throw exception in CDNClient if http request is not successful (#672)
Provides a WebAPIRequestException subclass for source compatibility. Fixes #650
- Loading branch information
Showing
5 changed files
with
110 additions
and
28 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
20 changes: 20 additions & 0 deletions
20
SteamKit2/SteamKit2/Steam/WebAPI/WebAPIRequestException.cs
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,20 @@ | ||
using System.Net.Http; | ||
|
||
namespace SteamKit2 | ||
{ | ||
/// <summary> | ||
/// Thrown when WebAPI request fails. | ||
/// </summary> | ||
public sealed class WebAPIRequestException : SteamKitWebRequestException | ||
{ | ||
/// <summary> | ||
/// Initializes a new instance of the <see cref="WebAPIRequestException"/> class. | ||
/// </summary> | ||
/// <param name="message">The message that describes the error.</param> | ||
/// <param name="response">HTTP response message including the status code and data.</param> | ||
public WebAPIRequestException(string message, HttpResponseMessage response) | ||
: base(message, response) | ||
{ | ||
} | ||
} | ||
} |
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,34 @@ | ||
using System.Net; | ||
using System.Net.Http; | ||
using System.Net.Http.Headers; | ||
|
||
namespace SteamKit2 | ||
{ | ||
/// <summary> | ||
/// Thrown when a HTTP request fails. | ||
/// </summary> | ||
public class SteamKitWebRequestException : HttpRequestException | ||
{ | ||
/// <summary> | ||
/// Represents the status code of the HTTP response. | ||
/// </summary> | ||
public HttpStatusCode StatusCode { get; private set; } | ||
|
||
/// <summary> | ||
/// Represents the collection of HTTP response headers. | ||
/// </summary> | ||
public HttpResponseHeaders Headers { get; private set; } | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="SteamKitWebRequestException"/> class. | ||
/// </summary> | ||
/// <param name="message">The message that describes the error.</param> | ||
/// <param name="response">HTTP response message including the status code and data.</param> | ||
public SteamKitWebRequestException(string message, HttpResponseMessage response) | ||
: base(message) | ||
{ | ||
this.StatusCode = response.StatusCode; | ||
this.Headers = response.Headers; | ||
} | ||
} | ||
} |
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,37 @@ | ||
using System; | ||
using System.Net; | ||
using System.Net.Http; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using SteamKit2; | ||
using Xunit; | ||
|
||
namespace Tests | ||
{ | ||
public class CDNClientFacts | ||
{ | ||
[Fact] | ||
public async Task ThrowsSteamKitWebExceptionOnUnsuccessfulWebResponse() | ||
{ | ||
var configuration = SteamConfiguration.Create(x => x.WithHttpClientFactory(() => new HttpClient(new TeapotHttpMessageHandler()))); | ||
var steam = new SteamClient(configuration); | ||
var client = new CDNClient(steam); | ||
|
||
try | ||
{ | ||
await client.DownloadManifestAsync(0, 0, "localhost", "12345"); | ||
throw new InvalidOperationException("This should be unreachable."); | ||
} | ||
catch (SteamKitWebRequestException ex) | ||
{ | ||
Assert.Equal((HttpStatusCode)418, ex.StatusCode); | ||
} | ||
} | ||
|
||
sealed class TeapotHttpMessageHandler : HttpMessageHandler | ||
{ | ||
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) | ||
=> Task.FromResult(new HttpResponseMessage((HttpStatusCode)418)); | ||
} | ||
} | ||
} |