-
Notifications
You must be signed in to change notification settings - Fork 576
[Foundation] Set 'sentRequest' when sending a request in NSUrlSessionHandler. #1806
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
spouliot
merged 8 commits into
dotnet:master
from
rolfbjarne:nsurlsessionhandler-sendrequest
Jun 5, 2017
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
9988a9d
[Foundation] Set 'sentRequest' when sending a request in NSUrlSession…
rolfbjarne 7e6f046
Add missing tests for the setRequest var.
mandel-macaque 1e09168
Redesign tests to make sure that all handlers run the same code.
mandel-macaque 9bb7a48
Fix failing test.
mandel-macaque 2795517
Fix merge issues with master.
mandel-macaque cff027d
Merge branch 'master' into nsurlsessionhandler-sendrequest
mandel-macaque 40e888a
Add the managed handler to the HttpClient tests.
mandel-macaque 76fa626
Fix minor style issues.
mandel-macaque File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.