Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/Foundation/NSUrlSessionHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,8 @@ async Task<NSUrlRequest> CreateRequest (HttpRequestMessage request)
#endif
protected override async Task<HttpResponseMessage> SendAsync (HttpRequestMessage request, CancellationToken cancellationToken)
{
Volatile.Write (ref sentRequest, true);

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are you using a volatile write instead of just assigning to the variable?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I used Volatile following the patterns used in the other handlers to keep everything as close as possible.


var nsrequest = await CreateRequest (request).ConfigureAwait(false);
var dataTask = session.CreateDataTask (nsrequest);

Expand Down
110 changes: 110 additions & 0 deletions tests/monotouch-test/HttpClient/HttpClientTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
#if !__WATCHOS__
using System;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;

#if XAMCORE_2_0
using Foundation;
#else
using MonoTouch.Foundation;
#endif
using NUnit.Framework;


namespace MonoTouchFixtures.HttpClientTests
{
[TestFixture]
public class HttpClientTest
{
const int WaitTimeout = 5000;

interface IHandlerWrapper
{
bool AllowAutoRedirect { get; set; }
HttpMessageHandler Handler { get; }

}

// Add new classes to deal with in this class in order not to change the tests, that way we ensure all
// handlers will pass the exact same tests with no duplication.
class HandlerWrapper : IHandlerWrapper
{
string handlerType;
HttpMessageHandler handler;

public HandlerWrapper (CFNetworkHandler handler)
{
this.handlerType = handler.GetType ().Name;
this.handler = new CFNetworkHandler ();
}

public HandlerWrapper (NSUrlSessionHandler handler)
{
this.handlerType = handler.GetType ().Name;
this.handler = new NSUrlSessionHandler ();
}

public HandlerWrapper (HttpClientHandler handler)
{
this.handlerType = handler.GetType ().Name;
this.handler = handler;
}

public bool AllowAutoRedirect {
get {
if (handlerType == "CFNetworkHandler")
return ((CFNetworkHandler)handler).AllowAutoRedirect;
if (handlerType == "NSUrlSessionHandler")
return ((NSUrlSessionHandler)handler).AllowAutoRedirect;
if (handlerType == "HttpClientHandler")
return ((HttpClientHandler)handler).AllowAutoRedirect;
throw new InvalidOperationException ();
}
set {
if (handlerType == "CFNetworkHandler")
((CFNetworkHandler)handler).AllowAutoRedirect = value;
if (handlerType == "NSUrlSessionHandler")
((NSUrlSessionHandler)handler).AllowAutoRedirect = value;
if (handlerType == "HttpClientHandler")
((HttpClientHandler)handler).AllowAutoRedirect = value;
throw new InvalidOperationException ();
}
}

public HttpMessageHandler Handler { get { return handler; } }

public static IHandlerWrapper GetWrapper (Type handlerType)
{
switch (handlerType.Name) {
case "CFNetworkHandler":
return new HandlerWrapper (new CFNetworkHandler ());
case "NSUrlSessionHandler":
return new HandlerWrapper (new NSUrlSessionHandler ());
case "HttpClientHandler":
return new HandlerWrapper (new HttpClientHandler ());
default:
throw new InvalidOperationException ();
}
}
}

[TestCase (typeof (HttpClientHandler))]
[TestCase (typeof (CFNetworkHandler))]
[TestCase (typeof (NSUrlSessionHandler))]
public void EnsureModifiabilityPostSend (Type handlerType)
{
var wrapper = HandlerWrapper.GetWrapper (handlerType);
using (var client = new HttpClient (wrapper.Handler))
using (var request = new HttpRequestMessage (HttpMethod.Get, "http://xamarin.com")) {
var token = new CancellationTokenSource ();
client.SendAsync (request, token.Token);
Assert.Throws<InvalidOperationException> (() => wrapper.AllowAutoRedirect = !wrapper.AllowAutoRedirect);
// cancel to ensure that we do not have side effects
token.Cancel ();
}
}
}
}
#endif

2 changes: 2 additions & 0 deletions tests/monotouch-test/monotouch-test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -648,6 +648,7 @@
<Compile Include="System.Net.Http\MessageHandlers.cs" />
<Compile Include="AVFoundation\PlayerItemVideoOutputTest.cs" />
<Compile Include="mono\ConfigTest.cs" />
<Compile Include="HttpClient\HttpClientTest.cs" />
<Compile Include="OpenGLES\EAGLContext.cs" />
<Compile Include="CoreText\CTParagraphStyleTests.cs" />
</ItemGroup>
Expand Down Expand Up @@ -714,6 +715,7 @@
<Folder Include="CloudKit\" />
<Folder Include="Intents\" />
<Folder Include="System.Net.Http\" />
<Folder Include="HttpClient\" />
<Folder Include="OpenGLES\" />
</ItemGroup>
<ItemGroup>
Expand Down