1
+ // Copyright (c) .NET Foundation and Contributors
2
+ //
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+ //
15
+
16
+ using Moq ;
17
+ using RestSharp . Tests . Integrated . Server ;
18
+
19
+ namespace RestSharp . Tests . Integrated . Interceptor ;
20
+
21
+ [ Collection ( nameof ( TestServerCollection ) ) ]
22
+ public class InterceptorTests {
23
+ readonly RestClient _client ;
24
+
25
+ public InterceptorTests ( TestServerFixture fixture ) => _client = new RestClient ( fixture . Server . Url ) ;
26
+
27
+ [ Fact ]
28
+ public async Task AddInterceptor_ShouldBeUsed ( ) {
29
+ //Arrange
30
+ var body = new TestRequest ( "foo" , 100 ) ;
31
+ var request = new RestRequest ( "post/json" ) . AddJsonBody ( body ) ;
32
+
33
+ var mockInterceptor = new Mock < Interceptors . Interceptor > ( ) ;
34
+ var interceptor = mockInterceptor . Object ;
35
+ var options = _client . Options ;
36
+ options . Interceptors . Add ( interceptor ) ;
37
+ //Act
38
+ var response = await _client . ExecutePostAsync < TestResponse > ( request ) ;
39
+ //Assert
40
+ mockInterceptor . Verify ( m => m . InterceptBeforeSerialization ( It . IsAny < RestRequest > ( ) ) ) ;
41
+ mockInterceptor . Verify ( m => m . InterceptBeforeRequest ( It . IsAny < HttpRequestMessage > ( ) ) ) ;
42
+ mockInterceptor . Verify ( m => m . InterceptAfterRequest ( It . IsAny < HttpResponseMessage > ( ) ) ) ;
43
+ mockInterceptor . Verify ( m => m . InterceptBeforeDeserialize ( It . IsAny < RestResponse > ( ) ) ) ;
44
+ }
45
+ [ Fact ]
46
+ public async Task ThrowExceptionIn_InterceptBeforeSerialization_ShouldBeCatchedInTest ( ) {
47
+ //Arrange
48
+ var body = new TestRequest ( "foo" , 100 ) ;
49
+ var request = new RestRequest ( "post/json" ) . AddJsonBody ( body ) ;
50
+
51
+ var mockInterceptor = new Mock < Interceptors . Interceptor > ( ) ;
52
+ mockInterceptor . Setup ( m => m . InterceptBeforeSerialization ( It . IsAny < RestRequest > ( ) ) ) . Throws < Exception > ( ( ) => throw new Exception ( "DummyException" ) ) ;
53
+ var interceptor = mockInterceptor . Object ;
54
+ var options = _client . Options ;
55
+ options . Interceptors . Add ( interceptor ) ;
56
+ //Act
57
+ var action = ( ) => _client . ExecutePostAsync < TestResponse > ( request ) ;
58
+ //Assert
59
+ await action . Should ( ) . ThrowAsync < Exception > ( ) . WithMessage ( "DummyException" ) ;
60
+ mockInterceptor . Verify ( m => m . InterceptBeforeSerialization ( It . IsAny < RestRequest > ( ) ) ) ;
61
+ mockInterceptor . Verify ( m => m . InterceptBeforeRequest ( It . IsAny < HttpRequestMessage > ( ) ) , Times . Never ) ;
62
+ mockInterceptor . Verify ( m => m . InterceptAfterRequest ( It . IsAny < HttpResponseMessage > ( ) ) , Times . Never ) ;
63
+ mockInterceptor . Verify ( m => m . InterceptBeforeDeserialize ( It . IsAny < RestResponse > ( ) ) , Times . Never ) ;
64
+ }
65
+ [ Fact ]
66
+ public async Task ThrowExceptionIn_InterceptBeforeRequest_ShouldBeCatchableInTest ( ) {
67
+ //Arrange
68
+ var body = new TestRequest ( "foo" , 100 ) ;
69
+ var request = new RestRequest ( "post/json" ) . AddJsonBody ( body ) ;
70
+
71
+ var mockInterceptor = new Mock < Interceptors . Interceptor > ( ) ;
72
+ mockInterceptor . Setup ( m => m . InterceptBeforeRequest ( It . IsAny < HttpRequestMessage > ( ) ) ) . Throws < Exception > ( ( ) => throw new Exception ( "DummyException" ) ) ;
73
+ var interceptor = mockInterceptor . Object ;
74
+ var options = _client . Options ;
75
+ options . Interceptors . Add ( interceptor ) ;
76
+ //Act
77
+ var action = ( ) => _client . ExecutePostAsync < TestResponse > ( request ) ;
78
+ //Assert
79
+ await action . Should ( ) . ThrowAsync < Exception > ( ) . WithMessage ( "DummyException" ) ;
80
+ mockInterceptor . Verify ( m => m . InterceptBeforeSerialization ( It . IsAny < RestRequest > ( ) ) ) ;
81
+ mockInterceptor . Verify ( m => m . InterceptBeforeRequest ( It . IsAny < HttpRequestMessage > ( ) ) ) ;
82
+ mockInterceptor . Verify ( m => m . InterceptAfterRequest ( It . IsAny < HttpResponseMessage > ( ) ) , Times . Never ) ;
83
+ mockInterceptor . Verify ( m => m . InterceptBeforeDeserialize ( It . IsAny < RestResponse > ( ) ) , Times . Never ) ;
84
+ }
85
+ [ Fact ]
86
+ public async Task ThrowExceptionIn_InterceptAfterRequest_ShouldBeCatchableInTest ( ) {
87
+ //Arrange
88
+ var body = new TestRequest ( "foo" , 100 ) ;
89
+ var request = new RestRequest ( "post/json" ) . AddJsonBody ( body ) ;
90
+
91
+ var mockInterceptor = new Mock < Interceptors . Interceptor > ( ) ;
92
+ mockInterceptor . Setup ( m => m . InterceptAfterRequest ( It . IsAny < HttpResponseMessage > ( ) ) ) . Throws < Exception > ( ( ) => throw new Exception ( "DummyException" ) ) ;
93
+ var interceptor = mockInterceptor . Object ;
94
+ var options = _client . Options ;
95
+ options . Interceptors . Add ( interceptor ) ;
96
+ //Act
97
+ var action = ( ) => _client . ExecutePostAsync < TestResponse > ( request ) ;
98
+ //Assert
99
+ await action . Should ( ) . ThrowAsync < Exception > ( ) . WithMessage ( "DummyException" ) ;
100
+ mockInterceptor . Verify ( m => m . InterceptBeforeSerialization ( It . IsAny < RestRequest > ( ) ) ) ;
101
+ mockInterceptor . Verify ( m => m . InterceptBeforeRequest ( It . IsAny < HttpRequestMessage > ( ) ) ) ;
102
+ mockInterceptor . Verify ( m => m . InterceptAfterRequest ( It . IsAny < HttpResponseMessage > ( ) ) ) ;
103
+ mockInterceptor . Verify ( m => m . InterceptBeforeDeserialize ( It . IsAny < RestResponse > ( ) ) , Times . Never ) ;
104
+ }
105
+ [ Fact ]
106
+ public async Task ThrowException_InInterceptBeforeDeserialize_ShouldBeCatchableInTest ( ) {
107
+ //Arrange
108
+ var body = new TestRequest ( "foo" , 100 ) ;
109
+ var request = new RestRequest ( "post/json" ) . AddJsonBody ( body ) ;
110
+
111
+ var mockInterceptor = new Mock < Interceptors . Interceptor > ( ) ;
112
+ mockInterceptor . Setup ( m => m . InterceptBeforeDeserialize ( It . IsAny < RestResponse > ( ) ) ) . Throws < Exception > ( ( ) => throw new Exception ( "DummyException" ) ) ;
113
+ var interceptor = mockInterceptor . Object ;
114
+ var options = _client . Options ;
115
+ options . Interceptors . Add ( interceptor ) ;
116
+ //Act
117
+ var action = ( ) => _client . PostAsync < TestResponse > ( request ) ;
118
+ //Assert
119
+ await action . Should ( ) . ThrowAsync < Exception > ( ) . WithMessage ( "DummyException" ) ;
120
+ mockInterceptor . Verify ( m => m . InterceptBeforeSerialization ( It . IsAny < RestRequest > ( ) ) ) ;
121
+ mockInterceptor . Verify ( m => m . InterceptBeforeRequest ( It . IsAny < HttpRequestMessage > ( ) ) ) ;
122
+ mockInterceptor . Verify ( m => m . InterceptAfterRequest ( It . IsAny < HttpResponseMessage > ( ) ) ) ;
123
+ mockInterceptor . Verify ( m => m . InterceptBeforeDeserialize ( It . IsAny < RestResponse > ( ) ) ) ;
124
+ }
125
+
126
+
127
+ }
0 commit comments