}
}
+ class ThrowOnlyProxy : IWebProxy
+ {
+ public ICredentials Credentials {
+ get {
+ throw new NotImplementedException ();
+ }
+
+ set {
+ throw new NotImplementedException ();
+ }
+ }
+
+ public Uri GetProxy (Uri destination)
+ {
+ throw new NotImplementedException ();
+ }
+
+ public bool IsBypassed (Uri host)
+ {
+ throw new NotImplementedException ();
+ }
+ }
+
const int WaitTimeout = 5000;
- string port, TestHost, LocalServer;
+ string TestHost, LocalServer;
+ int port;
[SetUp]
public void SetupFixture ()
{
if (Environment.OSVersion.Platform == PlatformID.Win32NT) {
- port = "810";
+ port = 810;
} else {
- port = "8810";
+ port = 8810;
}
TestHost = "localhost:" + port;
}
}
+ [Test]
+ [Category ("RequiresBSDSockets")]
+ public void Proxy_Disabled ()
+ {
+ var pp = WebRequest.DefaultWebProxy;
+
+ try {
+ WebRequest.DefaultWebProxy = new ThrowOnlyProxy ();
+
+ var request = new HttpClientHandler {
+ UseProxy = false
+ };
+
+ var client = new HttpClient (request);
+ Assert.IsTrue (client.GetAsync ("http://google.com").Wait (5000), "needs internet access");
+ } finally {
+ WebRequest.DefaultWebProxy = pp;
+ }
+ }
+
[Test]
public void Send ()
{
}
[Test]
+ [Category ("RequiresBSDSockets")]
public void Send_Complete_Default ()
{
bool? failed = null;
}
[Test]
+ [Category ("RequiresBSDSockets")]
public void Send_Complete_Version_1_0 ()
{
bool? failed = null;
}
[Test]
+ [Category ("RequiresBSDSockets")]
public void Send_Complete_ClientHandlerSettings ()
{
bool? failed = null;
}
[Test]
+ [Category ("RequiresBSDSockets")]
public void Send_Complete_CustomHeaders ()
{
bool? failed = null;
}
[Test]
+ [Category ("RequiresBSDSockets")]
public void Send_Complete_CustomHeaders_SpecialSeparators ()
{
bool? failed = null;
}
[Test]
+ [Category ("RequiresBSDSockets")]
+ public void Send_Complete_CustomHeaders_Host ()
+ {
+ bool? failed = null;
+ var listener = CreateListener (l => {
+ var request = l.Request;
+
+ try {
+ Assert.AreEqual ("customhost", request.Headers["Host"], "#1");
+ failed = false;
+ } catch {
+ failed = true;
+ }
+ });
+
+ try {
+ var client = new HttpClient ();
+
+ client.DefaultRequestHeaders.Add("Host", "customhost");
+
+ var request = new HttpRequestMessage (HttpMethod.Get, LocalServer);
+
+ var response = client.SendAsync (request, HttpCompletionOption.ResponseHeadersRead).Result;
+
+ Assert.AreEqual ("", response.Content.ReadAsStringAsync ().Result, "#100");
+ Assert.AreEqual (HttpStatusCode.OK, response.StatusCode, "#101");
+ Assert.AreEqual (false, failed, "#102");
+ } finally {
+ listener.Abort ();
+ listener.Close ();
+ }
+ }
+
+ [Test]
+ [Category ("RequiresBSDSockets")]
public void Send_Transfer_Encoding_Chunked ()
{
bool? failed = null;
}
[Test]
+ [Category ("RequiresBSDSockets")]
public void Send_Transfer_Encoding_Custom ()
{
bool? failed = null;
}
[Test]
+ [Category ("RequiresBSDSockets")]
public void Send_Complete_Content ()
{
var listener = CreateListener (l => {
}
[Test]
+ [Category ("RequiresBSDSockets")]
public void Send_Complete_Content_MaxResponseContentBufferSize ()
{
var listener = CreateListener (l => {
}
[Test]
+ [Category ("RequiresBSDSockets")]
public void Send_Complete_Content_MaxResponseContentBufferSize_Error ()
{
var listener = CreateListener (l => {
}
[Test]
+ [Category ("RequiresBSDSockets")]
public void Send_Complete_NoContent ()
{
foreach (var method in new HttpMethod[] { HttpMethod.Post, HttpMethod.Put, HttpMethod.Delete }) {
}
[Test]
+ [Category ("RequiresBSDSockets")]
public void Send_Complete_Error ()
{
var listener = CreateListener (l => {
}
[Test]
+ [Category ("RequiresBSDSockets")]
public void Send_Content_Get ()
{
var listener = CreateListener (l => {
}
[Test]
+ [Category ("RequiresBSDSockets")]
public void Send_Content_BomEncoding ()
{
var listener = CreateListener (l => {
}
[Test]
+ [Category ("RequiresBSDSockets")]
public void Send_Content_Put ()
{
bool passed = false;
}
[Test]
+ [Category ("RequiresBSDSockets")]
public void Send_Content_Put_CustomStream ()
{
bool passed = false;
[Test]
[Category ("MobileNotWorking")] // Missing encoding
+ [Category ("RequiresBSDSockets")]
public void GetString_Many ()
{
- var client = new HttpClient ();
- var t1 = client.GetStringAsync ("http://www.google.com");
- var t2 = client.GetStringAsync ("http://www.google.com");
- Assert.IsTrue (Task.WaitAll (new [] { t1, t2 }, WaitTimeout));
+ Action<HttpListenerContext> context = (HttpListenerContext l) => {
+ var response = l.Response;
+ response.StatusCode = 200;
+ response.OutputStream.WriteByte (0x68);
+ response.OutputStream.WriteByte (0x65);
+ response.OutputStream.WriteByte (0x6c);
+ response.OutputStream.WriteByte (0x6c);
+ response.OutputStream.WriteByte (0x6f);
+ };
+
+ var listener = CreateListener (context); // creates a default request handler
+ AddListenerContext (listener, context); // add another request handler for the second request
+
+ try {
+ var client = new HttpClient ();
+ var t1 = client.GetStringAsync (LocalServer);
+ var t2 = client.GetStringAsync (LocalServer);
+ Assert.IsTrue (Task.WaitAll (new [] { t1, t2 }, WaitTimeout));
+ Assert.AreEqual ("hello", t1.Result, "#1");
+ Assert.AreEqual ("hello", t2.Result, "#2");
+ } finally {
+ listener.Abort ();
+ listener.Close ();
+ }
}
[Test]
+ [Category ("RequiresBSDSockets")]
public void GetByteArray_ServerError ()
{
var listener = CreateListener (l => {
}
[Test]
+ [Category ("RequiresBSDSockets")]
public void DisallowAutoRedirect ()
{
var listener = CreateListener (l => {
}
[Test]
+ [Category ("RequiresBSDSockets")]
public void RequestUriAfterRedirect ()
{
var listener = CreateListener (l => {
var response = l.Response;
response.StatusCode = (int)HttpStatusCode.Moved;
- response.RedirectLocation = "http://xamarin.com/";
+ response.RedirectLocation = "http://localhost:8811/";
});
+ var listener2 = CreateListener (l => {
+ var response = l.Response;
+
+ response.StatusCode = (int)HttpStatusCode.OK;
+ response.OutputStream.WriteByte (0x68);
+ response.OutputStream.WriteByte (0x65);
+ response.OutputStream.WriteByte (0x6c);
+ response.OutputStream.WriteByte (0x6c);
+ response.OutputStream.WriteByte (0x6f);
+ }, 8811);
+
try {
var chandler = new HttpClientHandler ();
chandler.AllowAutoRedirect = true;
var r = client.GetAsync (LocalServer);
Assert.IsTrue (r.Wait (WaitTimeout), "#1");
var resp = r.Result;
- Assert.AreEqual ("http://xamarin.com/", resp.RequestMessage.RequestUri.AbsoluteUri, "#2");
+ Assert.AreEqual ("http://localhost:8811/", resp.RequestMessage.RequestUri.AbsoluteUri, "#2");
+ Assert.AreEqual ("hello", resp.Content.ReadAsStringAsync ().Result, "#3");
} finally {
listener.Abort ();
listener.Close ();
+ listener2.Abort ();
+ listener2.Close ();
}
}
[Test]
+ [Category ("RequiresBSDSockets")]
/*
* Properties may only be modified before sending the first request.
*/
}
HttpListener CreateListener (Action<HttpListenerContext> contextAssert)
+ {
+ return CreateListener (contextAssert, port);
+ }
+
+ HttpListener CreateListener (Action<HttpListenerContext> contextAssert, int port)
{
var l = new HttpListener ();
l.Prefixes.Add (string.Format ("http://+:{0}/", port));
l.Start ();
+ AddListenerContext(l, contextAssert);
+
+ return l;
+ }
+
+ HttpListener AddListenerContext (HttpListener l, Action<HttpListenerContext> contextAssert)
+ {
l.BeginGetContext (ar => {
var ctx = l.EndGetContext (ar);