-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathGxHttpClientTest.cs
More file actions
221 lines (204 loc) · 7.65 KB
/
GxHttpClientTest.cs
File metadata and controls
221 lines (204 loc) · 7.65 KB
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using DotNetUnitTest;
using GeneXus.Application;
using GeneXus.Http.Client;
using Xunit;
#if !NETCORE
using System.Web.SessionState;
using System.Web;
#endif
namespace xUnitTesting
{
public class GxHttpClientTest
{
const int MAX_CONNECTIONS= 5;
public GxHttpClientTest() {
Environment.SetEnvironmentVariable("GX_HTTPCLIENT_MAX_PER_ROUTE", MAX_CONNECTIONS.ToString(), EnvironmentVariableTarget.Process);
}
[Fact]
public void AddHeaderWithSpecialCharactersDoesNotThrowException()
{
using (GxHttpClient httpclient = new GxHttpClient())
{
string headerValue = "d3890093-289b-4f87-adad-f2ebea826e8f!8db3bc7ac3d38933c3b0c91a3bcdab60b9bbb3f607a1c9b312b24374e750243f3a31d7e90a4c55@SSORT!d3890093-289b-4f87-adad-f2ebea826e8f!8c7564ac08514ff988ba6c8c6ba3fc0c";
string headerName = "Authorization";
httpclient.AddHeader(headerName, headerValue);
httpclient.Host = "accountstest.genexus.com";
httpclient.Secure = 1;
httpclient.BaseURL = @"oauth/gam/v2.0/dummy/requesttokenanduserinfo";
httpclient.Execute("GET", string.Empty);
Assert.NotEqual(((int)HttpStatusCode.InternalServerError), httpclient.StatusCode);
}
}
[Fact(Skip ="For Local Test")]
public void HttpClientInvalidURLWithCustomPort()
{
using (GxHttpClient httpclient = new GxHttpClient())
{
string headerValue = "d3890093-289b-4f87-adad-f2ebea826e8f!8db3bc7ac3d38933c3b0c91a3bcdab60b9bbb3f607a1c9b312b24374e750243f3a31d7e90a4c55@SSORT!d3890093-289b-4f87-adad-f2ebea826e8f!8c7564ac08514ff988ba6c8c6ba3fc0c";
string headerName = "Authorization";
httpclient.AddHeader(headerName, headerValue);
httpclient.Host = "sistema.planoscs.com.br";
httpclient.Port = 70;
httpclient.BaseURL = @"AnnA/Prestador/Parser.php";
httpclient.Execute("GET", string.Empty);
Assert.NotEqual(((int)HttpStatusCode.InternalServerError), httpclient.StatusCode);
}
}
[WindowsOnlyFact]
public void HttpClientCookieHeader()
{
string headerValue = "CognitoIdentityServiceProvider.3tgmin25m9bkg6vgi7vpavu7a9.M00000936.refreshToken=eyJjdHkiOiJKV1QiLCJlbmMiSkRCAmMpYqndvORnWLTfHw; CognitoIdentityServiceProvider.3tgmin25m9bkg6vgi7vpavu7a9.LastAuthUser=M00000936";
string headerName = "Cookie";
using (GxHttpClient httpclient = new GxHttpClient())
{
httpclient.AddHeader(headerName, headerValue);
httpclient.Host = "localhost";
httpclient.Port = 80;
httpclient.BaseURL = @"NotFound/NotFound.php";
httpclient.HttpClientExecute("GET", string.Empty);
Assert.NotEqual(((int)HttpStatusCode.InternalServerError), httpclient.StatusCode);
}
using (GxHttpClient oldHttpclient = new GxHttpClient())
{
oldHttpclient.AddHeader(headerName, headerValue);
oldHttpclient.Host = "localhost";
oldHttpclient.Port = 80;
oldHttpclient.BaseURL = @"NotFound/NotFound.php";
oldHttpclient.Execute("GET", string.Empty);
Assert.NotEqual(((int)HttpStatusCode.InternalServerError), oldHttpclient.StatusCode);
}
}
#if NETCORE
[Fact(Skip = "For local testing only")]
public void TestHttpClientMaxPoolSize()
{
HttpClientMaxPoolSize().Wait();
}
async Task HttpClientMaxPoolSize() {
GxContext context = new GxContext();
string baseUrl = "http://localhost:8082/HttpClientTestNETSQLServer/testcookies.aspx";
var tasks = new List<Task>();
for (int i = 0; i < MAX_CONNECTIONS * 10; i++)
{
string url = $"{baseUrl}?id=" + i;
tasks.Add(Task.Run(() => ExecuteGet(url)));
}
await Task.WhenAll(tasks);
Assert.Single(GxHttpClient._httpClientInstances);
HttpClient c = GxHttpClient._httpClientInstances.First().Value;
Assert.NotNull(c);
Assert.True(pendingRequestsCount <= MAX_CONNECTIONS, $"Active connections ({pendingRequestsCount}) exceed MaxConnectionsPerServer ({MAX_CONNECTIONS})");
}
static private int pendingRequestsCount = 0;
static private readonly object syncObject = new object();
static private void IncrementPendingRequestsCount()
{
lock (syncObject)
{
pendingRequestsCount++;
}
}
static private void DecrementPendingRequestsCount()
{
lock (syncObject)
{
pendingRequestsCount--;
}
}
private string ExecuteGet(string url)
{
GxContext context = new GxContext();
using (GxHttpClient httpclient = new GxHttpClient(context))
{
IncrementPendingRequestsCount();
httpclient.HttpClientExecute("GET", url);
Assert.Equal((int)HttpStatusCode.OK, httpclient.StatusCode); //When failed, turn on log.config to see server side error.
DecrementPendingRequestsCount();
return httpclient.ToString();
}
}
#endif
[Fact(Skip = "For local testing only")]
public void HttpClientCookiesTest()
{
GxContext context = new GxContext();
string baseUrl = "http://localhost:8082/HttpClientTestNETSQLServer/testcookies.aspx";
using (GxHttpClient httpclient = new GxHttpClient(context))
{
string url = $"{baseUrl}?id=1";
httpclient.HttpClientExecute("GET", url);
Assert.Equal((int)HttpStatusCode.OK, httpclient.StatusCode);
CookieContainer cookies = context.GetCookieContainer(url, true);
Assert.NotNull(cookies);
CookieCollection responseCookies = cookies.GetCookies(new Uri(url));
Assert.NotEmpty(responseCookies);
string result = httpclient.ToString();
Assert.Contains("1", result, StringComparison.OrdinalIgnoreCase);
}
using (GxHttpClient httpclient = new GxHttpClient(context))
{
string url = $"{baseUrl}?id=2";
httpclient.IncludeCookies = true;
httpclient.HttpClientExecute("GET", url);
Assert.Equal((int)HttpStatusCode.OK, httpclient.StatusCode);
string result = httpclient.ToString();
Assert.StartsWith("Cookie found ", result, StringComparison.OrdinalIgnoreCase);
Assert.Contains("2", result, StringComparison.OrdinalIgnoreCase);
}
using (GxHttpClient httpclient = new GxHttpClient(context))
{
string url = $"{baseUrl}?id=3";
httpclient.IncludeCookies = false;
httpclient.HttpClientExecute("GET", url);
Assert.Equal((int)HttpStatusCode.OK, httpclient.StatusCode);
string result = httpclient.ToString();
Assert.StartsWith("Cookie not found", result, StringComparison.OrdinalIgnoreCase);
Assert.Contains("3", result, StringComparison.OrdinalIgnoreCase);
}
using (GxHttpClient httpclient = new GxHttpClient(context))
{
string url = "https://www.google.com/";
httpclient.HttpClientExecute("GET", url);
Assert.Equal((int)HttpStatusCode.OK, httpclient.StatusCode);
CookieContainer cookies = context.GetCookieContainer(url, true);
Assert.NotNull(cookies);
CookieCollection responseCookies = cookies.GetCookies(new Uri(url));
Assert.NotEmpty(responseCookies);
string result = httpclient.ToString();
}
}
[Fact]
public void ToRelativeFile()
{
using (GxHttpClient client = new GxHttpClient())
{
client.Host = "localhost";
client.BaseURL = "/dummy/lem.txt";
client.Execute("GET", string.Empty);
client.ToFile("./lem.txt");
Assert.True(File.Exists(Path.Combine(GxContext.StaticPhysicalPath(), "lem.txt")), $"HttpClient.ToFile failed to create the file at path: {GxContext.StaticPhysicalPath()}");
}
}
#if !NETCORE
[Fact]
public void NoStoreHeader()
{
var httpRequest = new HttpRequest("", "http://localhost/", "");
var httpResponce = new HttpResponse(new StringWriter());
var httpContext = new HttpContext(httpRequest, httpResponce);
HttpContext.Current = httpContext;
GxContext gxcontext = new GxContext();
gxcontext.HttpContext = HttpContext.Current;
byte result = gxcontext.SetHeader("CACHE", "no-store");
Assert.Equal(0, result);
}
#endif
}
}