-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Option to add Interceptors on Client Level (#2118)
* feature: Added Implementation for Interceptors
- Loading branch information
1 parent
36ebe2f
commit d0b4b18
Showing
11 changed files
with
292 additions
and
22 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
packages | ||
packages/ | ||
nuget.config | ||
|
||
|
||
#ignore thumbnails created by windows | ||
Thumbs.db | ||
|
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,7 @@ | ||
{ | ||
"sdk": { | ||
"version": "7.0.0", | ||
"rollForward": "latestMajor", | ||
"allowPrerelease": false | ||
} | ||
} |
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,57 @@ | ||
// Copyright (c) .NET Foundation and Contributors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
|
||
namespace RestSharp.Interceptors; | ||
|
||
/// <summary> | ||
/// Base Interceptor | ||
/// </summary> | ||
public abstract class Interceptor { | ||
/// <summary> | ||
/// Intercepts the request before serialization | ||
/// </summary> | ||
/// <param name="request">RestRequest before serialization</param> | ||
/// <returns>Value Tags</returns> | ||
public virtual ValueTask InterceptBeforeSerialization(RestRequest request) { | ||
return new(); | ||
} | ||
|
||
/// <summary> | ||
/// Intercepts the request before being sent | ||
/// </summary> | ||
/// <param name="req">HttpRequestMessage before being sent</param> | ||
/// <returns>Value Tags</returns> | ||
public virtual ValueTask InterceptBeforeRequest(HttpRequestMessage req) { | ||
return new(); | ||
} | ||
|
||
/// <summary> | ||
/// Intercepts the request before being sent | ||
/// </summary> | ||
/// <param name="responseMessage">HttpResponseMessage as received from Server</param> | ||
/// <returns>Value Tags</returns> | ||
public virtual ValueTask InterceptAfterRequest(HttpResponseMessage responseMessage) { | ||
return new(); | ||
} | ||
|
||
/// <summary> | ||
/// Intercepts the request before deserialization | ||
/// </summary> | ||
/// <param name="response">HttpResponseMessage as received from Server</param> | ||
/// <returns>Value Tags</returns> | ||
public virtual ValueTask InterceptBeforeDeserialize(RestResponse response) { | ||
return new(); | ||
} | ||
} |
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
127 changes: 127 additions & 0 deletions
127
test/RestSharp.Tests.Integrated/Interceptor/InterceptorTests.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,127 @@ | ||
// Copyright (c) .NET Foundation and Contributors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
|
||
using Moq; | ||
using RestSharp.Tests.Integrated.Server; | ||
|
||
namespace RestSharp.Tests.Integrated.Interceptor; | ||
|
||
[Collection(nameof(TestServerCollection))] | ||
public class InterceptorTests { | ||
readonly RestClient _client; | ||
|
||
public InterceptorTests(TestServerFixture fixture) => _client = new RestClient(fixture.Server.Url); | ||
|
||
[Fact] | ||
public async Task AddInterceptor_ShouldBeUsed() { | ||
//Arrange | ||
var body = new TestRequest("foo", 100); | ||
var request = new RestRequest("post/json").AddJsonBody(body); | ||
|
||
var mockInterceptor = new Mock<Interceptors.Interceptor>(); | ||
var interceptor = mockInterceptor.Object; | ||
var options = _client.Options; | ||
options.Interceptors.Add(interceptor); | ||
//Act | ||
var response = await _client.ExecutePostAsync<TestResponse>(request); | ||
//Assert | ||
mockInterceptor.Verify(m => m.InterceptBeforeSerialization(It.IsAny<RestRequest>())); | ||
mockInterceptor.Verify(m => m.InterceptBeforeRequest(It.IsAny<HttpRequestMessage>())); | ||
mockInterceptor.Verify(m => m.InterceptAfterRequest(It.IsAny<HttpResponseMessage>())); | ||
mockInterceptor.Verify(m => m.InterceptBeforeDeserialize(It.IsAny<RestResponse>())); | ||
} | ||
[Fact] | ||
public async Task ThrowExceptionIn_InterceptBeforeSerialization_ShouldBeCatchedInTest() { | ||
//Arrange | ||
var body = new TestRequest("foo", 100); | ||
var request = new RestRequest("post/json").AddJsonBody(body); | ||
|
||
var mockInterceptor = new Mock<Interceptors.Interceptor>(); | ||
mockInterceptor.Setup(m => m.InterceptBeforeSerialization(It.IsAny<RestRequest>())).Throws<Exception>(() => throw new Exception("DummyException")); | ||
var interceptor = mockInterceptor.Object; | ||
var options = _client.Options; | ||
options.Interceptors.Add(interceptor); | ||
//Act | ||
var action = () => _client.ExecutePostAsync<TestResponse>(request); | ||
//Assert | ||
await action.Should().ThrowAsync<Exception>().WithMessage("DummyException"); | ||
mockInterceptor.Verify(m => m.InterceptBeforeSerialization(It.IsAny<RestRequest>())); | ||
mockInterceptor.Verify(m => m.InterceptBeforeRequest(It.IsAny<HttpRequestMessage>()),Times.Never); | ||
mockInterceptor.Verify(m => m.InterceptAfterRequest(It.IsAny<HttpResponseMessage>()),Times.Never); | ||
mockInterceptor.Verify(m => m.InterceptBeforeDeserialize(It.IsAny<RestResponse>()),Times.Never); | ||
} | ||
[Fact] | ||
public async Task ThrowExceptionIn_InterceptBeforeRequest_ShouldBeCatchableInTest() { | ||
//Arrange | ||
var body = new TestRequest("foo", 100); | ||
var request = new RestRequest("post/json").AddJsonBody(body); | ||
|
||
var mockInterceptor = new Mock<Interceptors.Interceptor>(); | ||
mockInterceptor.Setup(m => m.InterceptBeforeRequest(It.IsAny<HttpRequestMessage>())).Throws<Exception>(() => throw new Exception("DummyException")); | ||
var interceptor = mockInterceptor.Object; | ||
var options = _client.Options; | ||
options.Interceptors.Add(interceptor); | ||
//Act | ||
var action = () => _client.ExecutePostAsync<TestResponse>(request); | ||
//Assert | ||
await action.Should().ThrowAsync<Exception>().WithMessage("DummyException"); | ||
mockInterceptor.Verify(m => m.InterceptBeforeSerialization(It.IsAny<RestRequest>())); | ||
mockInterceptor.Verify(m => m.InterceptBeforeRequest(It.IsAny<HttpRequestMessage>())); | ||
mockInterceptor.Verify(m => m.InterceptAfterRequest(It.IsAny<HttpResponseMessage>()),Times.Never); | ||
mockInterceptor.Verify(m => m.InterceptBeforeDeserialize(It.IsAny<RestResponse>()),Times.Never); | ||
} | ||
[Fact] | ||
public async Task ThrowExceptionIn_InterceptAfterRequest_ShouldBeCatchableInTest() { | ||
//Arrange | ||
var body = new TestRequest("foo", 100); | ||
var request = new RestRequest("post/json").AddJsonBody(body); | ||
|
||
var mockInterceptor = new Mock<Interceptors.Interceptor>(); | ||
mockInterceptor.Setup(m => m.InterceptAfterRequest(It.IsAny<HttpResponseMessage>())).Throws<Exception>(() => throw new Exception("DummyException")); | ||
var interceptor = mockInterceptor.Object; | ||
var options = _client.Options; | ||
options.Interceptors.Add(interceptor); | ||
//Act | ||
var action = () => _client.ExecutePostAsync<TestResponse>(request); | ||
//Assert | ||
await action.Should().ThrowAsync<Exception>().WithMessage("DummyException"); | ||
mockInterceptor.Verify(m => m.InterceptBeforeSerialization(It.IsAny<RestRequest>())); | ||
mockInterceptor.Verify(m => m.InterceptBeforeRequest(It.IsAny<HttpRequestMessage>())); | ||
mockInterceptor.Verify(m => m.InterceptAfterRequest(It.IsAny<HttpResponseMessage>())); | ||
mockInterceptor.Verify(m => m.InterceptBeforeDeserialize(It.IsAny<RestResponse>()),Times.Never); | ||
} | ||
[Fact] | ||
public async Task ThrowException_InInterceptBeforeDeserialize_ShouldBeCatchableInTest() { | ||
//Arrange | ||
var body = new TestRequest("foo", 100); | ||
var request = new RestRequest("post/json").AddJsonBody(body); | ||
|
||
var mockInterceptor = new Mock<Interceptors.Interceptor>(); | ||
mockInterceptor.Setup(m => m.InterceptBeforeDeserialize(It.IsAny<RestResponse>())).Throws<Exception>(() => throw new Exception("DummyException")); | ||
var interceptor = mockInterceptor.Object; | ||
var options = _client.Options; | ||
options.Interceptors.Add(interceptor); | ||
//Act | ||
var action = () => _client.PostAsync<TestResponse>(request); | ||
//Assert | ||
await action.Should().ThrowAsync<Exception>().WithMessage("DummyException"); | ||
mockInterceptor.Verify(m => m.InterceptBeforeSerialization(It.IsAny<RestRequest>())); | ||
mockInterceptor.Verify(m => m.InterceptBeforeRequest(It.IsAny<HttpRequestMessage>())); | ||
mockInterceptor.Verify(m => m.InterceptAfterRequest(It.IsAny<HttpResponseMessage>())); | ||
mockInterceptor.Verify(m => m.InterceptBeforeDeserialize(It.IsAny<RestResponse>())); | ||
} | ||
|
||
|
||
} |
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