Merge branch 'master' into WebView/WebViewControlProcessOptions_put_UserAgent

This commit is contained in:
Richard Murillo 2018-07-17 16:48:38 -07:00 коммит произвёл GitHub
Родитель 1521666098 212304df2c
Коммит dcc95f77fc
8 изменённых файлов: 309 добавлений и 2 удалений

Просмотреть файл

@ -4,6 +4,7 @@
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.Toolkit.Win32.UI.Controls.Interop.WinRT;
@ -351,6 +352,23 @@ namespace Microsoft.Toolkit.Win32.UI.Controls
/// navigation has completed.
void Navigate(string source);
/// <summary>
/// Navigates the web view with the URI with a HTTP request and HTTP headers.
/// </summary>
/// <param name="requestUri">The Uniform Resource Identifier (URI) to send the request.</param>
/// <param name="httpMethod">The HTTP method of the request.</param>
/// <param name="content">Optional content to send with the request.</param>
/// <param name="headers">Optional headers to send with the request.</param>
/// <remarks>
/// This method only supports <see cref="HttpMethod.Get"/> and <see cref="HttpMethod.Post"/> for the <paramref name="httpMethod"/> parameter.
/// </remarks>
/// <seealso cref="Windows.Web.UI.Interop.WebViewControl.NavigateWithHttpRequestMessage"/>
void Navigate(
Uri requestUri,
HttpMethod httpMethod,
string content = null,
IEnumerable<KeyValuePair<string, string>> headers = null);
/// <summary>
/// Loads the specified HTML content relative to the location of the current executable.
/// </summary>

Просмотреть файл

@ -10,6 +10,7 @@ using System.Security;
using System.Threading.Tasks;
using Windows.Foundation.Metadata;
using Windows.Web;
using Windows.Web.Http;
using Windows.Web.UI;
using Windows.Web.UI.Interop;
@ -572,6 +573,66 @@ namespace Microsoft.Toolkit.Win32.UI.Controls.Interop.WinRT
_webViewControl?.NavigateToLocalStreamUri(uri, AsWindowsRuntimeUriToStreamResolver(streamResolver));
}
internal void Navigate(
Uri requestUri,
System.Net.Http.HttpMethod method,
string content = null,
IEnumerable<KeyValuePair<string, string>> headers = null)
{
if (requestUri == null)
{
throw new ArgumentNullException(nameof(requestUri));
}
if (method == null)
{
throw new ArgumentNullException(nameof(method));
}
// Convert a System.Net.Http.HttpMethod to Windows.Web.Http.HttpMethod
HttpMethod ToHttpMethod(System.Net.Http.HttpMethod httpMethod)
{
if (System.Net.Http.HttpMethod.Get.Equals(httpMethod))
{
return HttpMethod.Get;
}
if (System.Net.Http.HttpMethod.Post.Equals(httpMethod))
{
return HttpMethod.Post;
}
// For compatabilty with WebView.NavigateWithHttpRequestMessage, this only supports POST and GET
throw new ArgumentOutOfRangeException(nameof(httpMethod));
}
var requestMessage = new HttpRequestMessage
{
RequestUri = requestUri,
Method = ToHttpMethod(method)
};
if (content != null)
{
requestMessage.Content = new HttpStringContent(content);
}
if (headers != null)
{
foreach (var header in headers)
{
requestMessage.Headers.Add(header);
}
}
NavigateWithHttpRequestMessage(requestMessage);
}
internal void NavigateWithHttpRequestMessage(HttpRequestMessage requestMessage)
{
_webViewControl?.NavigateWithHttpRequestMessage(requestMessage);
}
/// <exception cref="ArgumentNullException"><paramref name="text"/> is <see langword="null"/></exception>
internal void NavigateToString(string text)
{

Просмотреть файл

@ -41,6 +41,7 @@
<Reference Include="System.Windows.Forms" />
<Reference Include="System.Xaml" />
<Reference Include="WindowsBase" />
<PackageReference Include="System.Net.Http" Version="4.0.0" />
<PackageReference Include="System.Runtime.WindowsRuntime" PrivateAssets="All">
<Version>4.0.0</Version>
</PackageReference>

Просмотреть файл

@ -68,8 +68,18 @@ using System.Security;
[assembly: Dependency("Windows.Storage.winmd", LoadHint.Sometimes)]
// InternalsVisableTo
[assembly: InternalsVisibleTo("Microsoft.Toolkit.Win32.UI.Controls.Test.WinForms.WebView")]
[assembly: InternalsVisibleTo("Microsoft.Toolkit.Win32.UI.Controls.Test.WebView.Shared")]
[assembly: InternalsVisibleTo("Microsoft.Toolkit.Win32.UI.Controls.Test.WinForms.WebView, PublicKey=00240000048000009400000006020000002" +
"4000052534131000400000100010041753af735ae6140c9508567666c51c6" +
"ab929806adb0d210694b30ab142a060237bc741f9682e7d8d4310364b4bba" +
"4ee89cc9d3d5ce7e5583587e8ea44dca09977996582875e71fb54fa7b1707" +
"98d853d5d8010b07219633bdb761d01ac924da44576d6180cdceae5379739" +
"82bb461c541541d58417a3794e34f45e6f2d129e2")]
[assembly: InternalsVisibleTo("Microsoft.Toolkit.Win32.UI.Controls.Test.WebView.Shared, PublicKey=00240000048000009400000006020000002" +
"4000052534131000400000100010041753af735ae6140c9508567666c51c6" +
"ab929806adb0d210694b30ab142a060237bc741f9682e7d8d4310364b4bba" +
"4ee89cc9d3d5ce7e5583587e8ea44dca09977996582875e71fb54fa7b1707" +
"98d853d5d8010b07219633bdb761d01ac924da44576d6180cdceae5379739" +
"82bb461c541541d58417a3794e34f45e6f2d129e2")]
[assembly: SecurityRules(SecurityRuleSet.Level2)]
#if ALLOW_PARTIALLY_TRUSTED_CALLERS

Просмотреть файл

@ -9,6 +9,7 @@ using System.ComponentModel;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Net.Http;
using System.Runtime.InteropServices;
using System.Security;
using System.Threading;
@ -605,6 +606,25 @@ namespace Microsoft.Toolkit.Win32.UI.Controls.WPF
Source = source;
}
/// <inheritdoc />
public void Navigate(
Uri requestUri,
HttpMethod httpMethod,
string content = null,
IEnumerable<KeyValuePair<string, string>> headers = null)
{
VerifyAccess();
do
{
Dispatcher.CurrentDispatcher.DoEvents();
}
while (!_initializationComplete.WaitOne(InitializationBlockingTime));
Verify.IsNotNull(_webViewControl);
_webViewControl.Navigate(requestUri, httpMethod, content, headers);
}
/// <inheritdoc />
[Obsolete("Use NavigateToLocalStreamUri(Uri, IUriToStreamResolver) instead")]
public void NavigateToLocal(string relativePath)

Просмотреть файл

@ -3,7 +3,9 @@
// See the LICENSE file in the project root for more information.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Net.Http;
using Microsoft.Toolkit.Win32.UI.Controls.Interop.WinRT;
namespace Microsoft.Toolkit.Win32.UI.Controls.WinForms
@ -117,5 +119,13 @@ namespace Microsoft.Toolkit.Win32.UI.Controls.WinForms
/// <inheritdoc />
public void NavigateToLocalStreamUri(Uri relativePath, IUriToStreamResolver streamResolver) => _webViewControl?.NavigateToLocalStreamUri(relativePath, streamResolver);
/// <inheritdoc />
public void Navigate(
Uri requestUri,
HttpMethod httpMethod,
string content = null,
IEnumerable<KeyValuePair<string, string>> headers = null) =>
_webViewControl.Navigate(requestUri, httpMethod, content, headers);
}
}

Просмотреть файл

@ -19,6 +19,9 @@ namespace Microsoft.Toolkit.Win32.UI.Controls.Test.WebView.Shared
// Local navigation: when using a null value for Source the uri is about:blank
public static readonly Uri AboutBlank = new Uri("about:blank");
// A simple HTTP Request & Response Service
public static readonly Uri HttpBin = new Uri("http://httpbin.org", UriKind.Absolute);
}
}
}

Просмотреть файл

@ -3,7 +3,10 @@
// See the LICENSE file in the project root for more information.
using System;
using System.Collections.Generic;
using System.IO;
using System.Net.Http;
using System.Text;
using Microsoft.Toolkit.Win32.UI.Controls.Test.WebView.Shared;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Should;
@ -83,4 +86,185 @@ namespace Microsoft.Toolkit.Win32.UI.Controls.Test.WinForms.WebView.FunctionalTe
});
}
}
[TestClass]
public class Navigate2Tests : HostFormWebViewContextSpecification
{
private bool _navigationCompleted;
protected override void Given()
{
base.Given();
WebView.NavigationCompleted += (o, e) =>
{
_navigationCompleted = e.IsSuccess;
Form.Close();
};
}
protected override void When()
{
PerformActionAndWaitForFormClose(() =>
{
WebView.Navigate(TestConstants.Uris.HttpBin, HttpMethod.Get);
});
}
[TestMethod]
public void Explict_HTTP_GET_succeeds()
{
_navigationCompleted.ShouldBeTrue();
}
}
[TestClass]
public class NavigateGetWithHeaders : HostFormWebViewContextSpecification
{
private bool _navigationCompleted;
protected override void Given()
{
base.Given();
WebView.NavigationCompleted += (o, e) =>
{
_navigationCompleted = e.IsSuccess;
Form.Close();
};
}
protected override void When()
{
PerformActionAndWaitForFormClose(() =>
{
WebView.Navigate(
TestConstants.Uris.HttpBin,
HttpMethod.Get,
null,
new[] { new KeyValuePair<string, string>("pragma", "no-cache") });
});
}
[TestMethod]
public void Explict_HTTP_GET_with_HEADERS_succeeds()
{
_navigationCompleted.ShouldBeTrue();
}
}
[TestClass]
public class NavigateGetWithBasicAuth : HostFormWebViewContextSpecification
{
private bool _navigationCompleted;
protected override void Given()
{
base.Given();
WebView.NavigationCompleted += (o, e) =>
{
_navigationCompleted = e.IsSuccess;
Form.Close();
};
}
protected override void When()
{
PerformActionAndWaitForFormClose(() =>
{
const string user = "usr";
const string password = "pwd";
const string header = "Authorization";
var authInfo = Convert.ToBase64String(Encoding.Default.GetBytes($"{user}:{password}"));
WebView.Navigate(
new Uri(TestConstants.Uris.HttpBin, new Uri($"/basic-auth/{user}/{password}", UriKind.Relative)),
HttpMethod.Get,
null,
new[] { new KeyValuePair<string, string>(header, $"Basic {authInfo}") });
});
}
[TestMethod]
public void Explict_HTTP_GET_with_AUTH_BASIC_succeeds()
{
_navigationCompleted.ShouldBeTrue();
}
}
[TestClass]
public class NavigateOption : HostFormWebViewContextSpecification
{
private bool _navigationCompleted;
protected override void Given()
{
base.Given();
WebView.NavigationCompleted += (o, e) =>
{
_navigationCompleted = e.IsSuccess;
Form.Close();
};
}
protected override void When()
{
PerformActionAndWaitForFormClose(() =>
{
WebView.Navigate(
TestConstants.Uris.ExampleCom,
HttpMethod.Options
);
});
}
[TestMethod]
[ExpectedException(typeof(ArgumentOutOfRangeException))]
[Ignore("Pops UI that stalls test")]
public void Explict_HTTP_OPTION_fails()
{
_navigationCompleted.ShouldBeFalse();
}
}
[TestClass]
public class NavigatePostWithContent : HostFormWebViewContextSpecification
{
private bool _navigationCompleted;
protected override void Given()
{
base.Given();
WebView.NavigationCompleted += (o, e) =>
{
_navigationCompleted = e.IsSuccess;
Form.Close();
};
}
protected override void When()
{
PerformActionAndWaitForFormClose(() =>
{
string Foo()
{
var c = new FormUrlEncodedContent(new[] { new KeyValuePair<string, string>("Foo", "Bar"), });
return c.ReadAsStringAsync().Result;
}
WebView.Navigate(
new Uri(TestConstants.Uris.HttpBin, "/post"),
HttpMethod.Post,
Foo()
);
});
}
[TestMethod]
public void Explict_HTTP_POST_with_data_succeeds()
{
_navigationCompleted.ShouldBeTrue();
}
}
}