1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
|
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Net;
using System.Net.Http;
using System.Net.Test.Common;
using System.Threading;
using System.Threading.Tasks;
using Xunit;
using Xunit.Abstractions;
// Can't use "WinHttpHandler.Functional.Tests" in namespace as it won't compile.
// WinHttpHandler is a class and not a namespace and can't be part of namespace paths.
namespace System.Net.Http.WinHttpHandlerFunctional.Tests
{
// Note: Disposing the HttpClient object automatically disposes the handler within. So, it is not necessary
// to separately Dispose (or have a 'using' statement) for the handler.
public class WinHttpHandlerTest
{
// TODO: This is a placeholder until GitHub Issue #2383 gets resolved.
private const string SlowServer = "http://httpbin.org/drip?numbytes=1&duration=1&delay=40&code=200";
private readonly ITestOutputHelper _output;
public WinHttpHandlerTest(ITestOutputHelper output)
{
_output = output;
}
[Fact]
public void SendAsync_SimpleGet_Success()
{
var handler = new WinHttpHandler();
using (var client = new HttpClient(handler))
{
// TODO: This is a placeholder until GitHub Issue #2383 gets resolved.
var response = client.GetAsync(Configuration.Http.RemoteEchoServer).Result;
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
var responseContent = response.Content.ReadAsStringAsync().GetAwaiter().GetResult();
_output.WriteLine(responseContent);
}
}
[Theory]
[InlineData(CookieUsePolicy.UseInternalCookieStoreOnly, "cookieName1", "cookieValue1")]
[InlineData(CookieUsePolicy.UseSpecifiedCookieContainer, "cookieName2", "cookieValue2")]
public async Task GetAsync_RedirectResponseHasCookie_CookieSentToFinalUri(
CookieUsePolicy cookieUsePolicy,
string cookieName,
string cookieValue)
{
Uri uri = Configuration.Http.RedirectUriForDestinationUri(false, 302, Configuration.Http.RemoteEchoServer, 1);
var handler = new WinHttpHandler();
handler.WindowsProxyUsePolicy = WindowsProxyUsePolicy.UseWinInetProxy;
handler.CookieUsePolicy = cookieUsePolicy;
if (cookieUsePolicy == CookieUsePolicy.UseSpecifiedCookieContainer)
{
handler.CookieContainer = new CookieContainer();
}
using (HttpClient client = new HttpClient(handler))
{
client.DefaultRequestHeaders.Add(
"X-SetCookie",
string.Format("{0}={1};Path=/", cookieName, cookieValue));
using (HttpResponseMessage httpResponse = await client.GetAsync(uri))
{
string responseText = await httpResponse.Content.ReadAsStringAsync();
_output.WriteLine(responseText);
Assert.True(JsonMessageContainsKeyValue(responseText, cookieName, cookieValue));
}
}
}
[Fact]
[OuterLoop]
public async Task SendAsync_SlowServerAndCancel_ThrowsTaskCanceledException()
{
var handler = new WinHttpHandler();
using (var client = new HttpClient(handler))
{
var cts = new CancellationTokenSource();
Task<HttpResponseMessage> t = client.GetAsync(SlowServer, cts.Token);
await Task.Delay(500);
cts.Cancel();
AggregateException ag = Assert.Throws<AggregateException>(() => t.Wait());
Assert.IsType<TaskCanceledException>(ag.InnerException);
}
}
[Fact]
[OuterLoop]
public void SendAsync_SlowServerRespondsAfterDefaultReceiveTimeout_ThrowsHttpRequestException()
{
var handler = new WinHttpHandler();
using (var client = new HttpClient(handler))
{
Task<HttpResponseMessage> t = client.GetAsync(SlowServer);
AggregateException ag = Assert.Throws<AggregateException>(() => t.Wait());
Assert.IsType<HttpRequestException>(ag.InnerException);
}
}
public static bool JsonMessageContainsKeyValue(string message, string key, string value)
{
// TODO: Merge with System.Net.Http TestHelper class as part of GitHub Issue #4989.
string pattern = string.Format(@"""{0}"": ""{1}""", key, value);
return message.Contains(pattern);
}
}
}
|