2016-11-09 17:31:02 +03:00
|
|
|
|
//
|
|
|
|
|
// MessageHandlers.cs
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using System.Net;
|
|
|
|
|
using System.Net.Http;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.IO;
|
|
|
|
|
|
|
|
|
|
using NUnit.Framework;
|
2019-01-10 19:45:20 +03:00
|
|
|
|
using System.Net.Http.Headers;
|
|
|
|
|
using System.Text;
|
2017-01-12 21:13:56 +03:00
|
|
|
|
#if MONOMAC
|
|
|
|
|
using Foundation;
|
|
|
|
|
#endif
|
2018-07-04 12:44:14 +03:00
|
|
|
|
using ObjCRuntime;
|
2016-11-09 17:31:02 +03:00
|
|
|
|
|
|
|
|
|
namespace MonoTests.System.Net.Http
|
|
|
|
|
{
|
|
|
|
|
[TestFixture]
|
|
|
|
|
public class MessageHandlerTest
|
|
|
|
|
{
|
2017-07-13 15:24:36 +03:00
|
|
|
|
void PrintHandlerToTest ()
|
|
|
|
|
{
|
|
|
|
|
#if !__WATCHOS__
|
|
|
|
|
Console.WriteLine (new HttpClientHandler ());
|
|
|
|
|
Console.WriteLine (new CFNetworkHandler ());
|
|
|
|
|
#endif
|
|
|
|
|
Console.WriteLine (new NSUrlSessionHandler ());
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-09 17:31:02 +03:00
|
|
|
|
HttpMessageHandler GetHandler (Type handler_type)
|
|
|
|
|
{
|
|
|
|
|
return (HttpMessageHandler) Activator.CreateInstance (handler_type);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
#if !__WATCHOS__
|
|
|
|
|
[TestCase (typeof (HttpClientHandler))]
|
|
|
|
|
[TestCase (typeof (CFNetworkHandler))]
|
|
|
|
|
#endif
|
|
|
|
|
[TestCase (typeof (NSUrlSessionHandler))]
|
|
|
|
|
public void DnsFailure (Type handlerType)
|
|
|
|
|
{
|
2018-07-04 12:44:14 +03:00
|
|
|
|
TestRuntime.AssertSystemVersion (PlatformName.MacOSX, 10, 9, throwIfOtherPlatform: false);
|
|
|
|
|
TestRuntime.AssertSystemVersion (PlatformName.iOS, 7, 0, throwIfOtherPlatform: false);
|
2018-07-03 18:09:49 +03:00
|
|
|
|
|
2017-07-13 15:24:36 +03:00
|
|
|
|
PrintHandlerToTest ();
|
|
|
|
|
|
2016-11-09 17:31:02 +03:00
|
|
|
|
bool done = false;
|
|
|
|
|
Exception ex = null;
|
|
|
|
|
|
|
|
|
|
TestRuntime.RunAsync (DateTime.Now.AddSeconds (30), async () =>
|
|
|
|
|
{
|
|
|
|
|
try {
|
|
|
|
|
HttpClient client = new HttpClient (GetHandler (handlerType));
|
|
|
|
|
var s = await client.GetStringAsync ("http://doesnotexist.xamarin.com");
|
2018-11-06 11:57:35 +03:00
|
|
|
|
Console.WriteLine (s);
|
2016-11-09 17:31:02 +03:00
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
ex = e;
|
|
|
|
|
} finally {
|
|
|
|
|
done = true;
|
|
|
|
|
}
|
|
|
|
|
}, () => done);
|
|
|
|
|
|
2018-12-03 19:43:58 +03:00
|
|
|
|
Assert.IsTrue (done, "Did not time out");
|
2016-11-09 17:31:02 +03:00
|
|
|
|
Assert.IsNotNull (ex, "Exception");
|
2017-07-13 15:24:36 +03:00
|
|
|
|
// The handlers throw different types of exceptions, so we can't assert much more than that something went wrong.
|
2016-11-09 17:31:02 +03:00
|
|
|
|
}
|
2017-07-13 15:24:36 +03:00
|
|
|
|
|
2018-12-11 00:59:11 +03:00
|
|
|
|
#if !__WATCHOS__
|
|
|
|
|
// ensure that we do get the same number of cookies as the managed handler
|
|
|
|
|
[TestCase]
|
|
|
|
|
public void TestNSUrlSessionHandlerCookies ()
|
|
|
|
|
{
|
|
|
|
|
bool areEqual = false;
|
|
|
|
|
var manageCount = 0;
|
|
|
|
|
var nativeCount = 0;
|
|
|
|
|
Exception ex = null;
|
|
|
|
|
|
|
|
|
|
TestRuntime.RunAsync (DateTime.Now.AddSeconds (30), async () =>
|
|
|
|
|
{
|
|
|
|
|
try {
|
|
|
|
|
var managedClient = new HttpClient (new HttpClientHandler ());
|
|
|
|
|
var managedResponse = await managedClient.GetAsync ("https://google.com");
|
|
|
|
|
if (managedResponse.Headers.TryGetValues ("Set-Cookie", out var managedCookies)) {
|
|
|
|
|
var nativeClient = new HttpClient (new NSUrlSessionHandler ());
|
|
|
|
|
var nativeResponse = await nativeClient.GetAsync ("https://google.com");
|
|
|
|
|
if (managedResponse.Headers.TryGetValues ("Set-Cookie", out var nativeCookies)) {
|
|
|
|
|
manageCount = managedCookies.Count ();
|
|
|
|
|
nativeCount = nativeCookies.Count ();
|
|
|
|
|
areEqual = manageCount == nativeCount;
|
|
|
|
|
} else {
|
|
|
|
|
manageCount = -1;
|
|
|
|
|
nativeCount = -1;
|
|
|
|
|
areEqual = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
ex = e;
|
|
|
|
|
}
|
|
|
|
|
}, () => areEqual);
|
|
|
|
|
|
|
|
|
|
Assert.IsTrue (areEqual, $"Cookies are different - Managed {manageCount} vs Native {nativeCount}");
|
|
|
|
|
Assert.IsNull (ex, "Exception");
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2019-01-10 19:45:20 +03:00
|
|
|
|
// ensure that if we have a redirect, we do not have the auth headers in the following requests
|
|
|
|
|
#if !__WATCHOS__
|
|
|
|
|
[TestCase (typeof (HttpClientHandler))]
|
|
|
|
|
[TestCase (typeof (CFNetworkHandler))]
|
|
|
|
|
#endif
|
|
|
|
|
[TestCase (typeof (NSUrlSessionHandler))]
|
|
|
|
|
public void RedirectionWithAuthorizationHeaders (Type handlerType)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
TestRuntime.AssertSystemVersion (PlatformName.MacOSX, 10, 9, throwIfOtherPlatform: false);
|
|
|
|
|
TestRuntime.AssertSystemVersion (PlatformName.iOS, 7, 0, throwIfOtherPlatform: false);
|
|
|
|
|
|
|
|
|
|
bool containsAuthorizarion = false;
|
|
|
|
|
bool containsHeaders = false;
|
|
|
|
|
string json = "";
|
|
|
|
|
bool done = false;
|
|
|
|
|
Exception ex = null;
|
|
|
|
|
|
|
|
|
|
TestRuntime.RunAsync (DateTime.Now.AddSeconds (30), async () =>
|
|
|
|
|
{
|
|
|
|
|
try {
|
|
|
|
|
HttpClient client = new HttpClient (GetHandler (handlerType));
|
|
|
|
|
client.BaseAddress = new Uri ("https://httpbin.org");
|
|
|
|
|
var byteArray = new UTF8Encoding ().GetBytes ("username:password");
|
|
|
|
|
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue ("Basic", Convert.ToBase64String(byteArray));
|
|
|
|
|
var result = await client.GetAsync ("https://httpbin.org/redirect/3");
|
|
|
|
|
// get the data returned from httpbin which contains the details of the requested performed.
|
|
|
|
|
json = await result.Content.ReadAsStringAsync ();
|
|
|
|
|
containsAuthorizarion = json.Contains ("Authorization");
|
|
|
|
|
containsHeaders = json.Contains ("headers"); // ensure we do have the headers in the response
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
ex = e;
|
|
|
|
|
} finally {
|
|
|
|
|
done = true;
|
|
|
|
|
}
|
|
|
|
|
}, () => done);
|
|
|
|
|
|
|
|
|
|
Assert.IsTrue (done, "Request timedout.");
|
|
|
|
|
Assert.IsTrue (containsHeaders, "Request did not reach final destination.");
|
|
|
|
|
Assert.IsFalse (containsAuthorizarion, $"Authorization header did reach the final destination. {json}");
|
|
|
|
|
Assert.IsNull (ex, $"Exception {ex} for {json}");
|
|
|
|
|
}
|
2018-12-11 00:59:11 +03:00
|
|
|
|
}
|
|
|
|
|
}
|