2
2
using System ;
3
3
using System . Collections . Generic ;
4
4
using System . Linq ;
5
+ using System . Net . Http ;
5
6
using System . Threading ;
6
7
using System . Threading . Tasks ;
7
8
using Elasticsearch . Net ;
8
9
using FluentAssertions ;
9
10
using Nest ;
10
11
using Tests . Framework ;
12
+ using HttpMethod = Elasticsearch . Net . HttpMethod ;
11
13
12
14
namespace Tests . ClientConcepts . Connection
13
15
{
@@ -19,6 +21,8 @@ public class TestableHttpConnection : HttpConnection
19
21
20
22
public int CallCount { get ; private set ; }
21
23
24
+ public HttpClientHandler LastUsedHttpClientHandler { get ; private set ; }
25
+
22
26
public override ElasticsearchResponse < TReturn > Request < TReturn > ( RequestData requestData )
23
27
{
24
28
CallCount ++ ;
@@ -30,6 +34,12 @@ public override Task<ElasticsearchResponse<TReturn>> RequestAsync<TReturn>(Reque
30
34
CallCount ++ ;
31
35
return base . RequestAsync < TReturn > ( requestData , cancellationToken ) ;
32
36
}
37
+
38
+ protected override HttpClientHandler CreateHttpClientHandler ( RequestData requestData )
39
+ {
40
+ LastUsedHttpClientHandler = base . CreateHttpClientHandler ( requestData ) ;
41
+ return LastUsedHttpClientHandler ;
42
+ }
33
43
}
34
44
35
45
[ U ]
@@ -112,6 +122,36 @@ private static RequestData CreateRequestData(
112
122
} ;
113
123
return requestData ;
114
124
}
125
+
126
+ /// <summary>
127
+ /// Setting HttpClientHandler.Proxy = null don't disable HttpClient automatic proxy detection.
128
+ /// It is disabled by setting Proxy to non-null value or by setting UseProxy = false.
129
+ /// </summary>
130
+ [ U ]
131
+ public async Task HttpClientUseProxyShouldBeFalseWhenDisabledAutoProxyDetection ( )
132
+ {
133
+ var connection = new TestableHttpConnection ( ) ;
134
+ var requestData = CreateRequestData ( disableAutomaticProxyDetection : true ) ;
135
+
136
+ connection . Request < string > ( requestData ) ;
137
+ connection . LastUsedHttpClientHandler . UseProxy . Should ( ) . BeFalse ( ) ;
138
+
139
+ await connection . RequestAsync < string > ( requestData , CancellationToken . None ) . ConfigureAwait ( false ) ;
140
+ connection . LastUsedHttpClientHandler . UseProxy . Should ( ) . BeFalse ( ) ;
141
+ }
142
+
143
+ [ U ]
144
+ public async Task HttpClientUseProxyShouldBeTrueWhenEnabledAutoProxyDetection ( )
145
+ {
146
+ var connection = new TestableHttpConnection ( ) ;
147
+ var requestData = CreateRequestData ( ) ;
148
+
149
+ connection . Request < string > ( requestData ) ;
150
+ connection . LastUsedHttpClientHandler . UseProxy . Should ( ) . BeTrue ( ) ;
151
+
152
+ await connection . RequestAsync < string > ( requestData , CancellationToken . None ) . ConfigureAwait ( false ) ;
153
+ connection . LastUsedHttpClientHandler . UseProxy . Should ( ) . BeTrue ( ) ;
154
+ }
115
155
}
116
156
}
117
157
#endif
0 commit comments