Skip to content

Commit

Permalink
Test - Add CanLoadRequestWithPostData test
Browse files Browse the repository at this point in the history
  • Loading branch information
amaitland committed Jan 15, 2021
1 parent a713c21 commit 497e75c
Show file tree
Hide file tree
Showing 6 changed files with 156 additions and 0 deletions.
1 change: 1 addition & 0 deletions CefSharp.Core/CefSharp.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
<Compile Include="RequestContextBuilder.cs" />
<Compile Include="RequestContextSettings.cs" />
<Compile Include="UrlRequest.cs" />
<Compile Include="WebBrowserExtensionsEx.cs" />
<Compile Include="WindowInfo.cs" />
</ItemGroup>
<ItemGroup>
Expand Down
51 changes: 51 additions & 0 deletions CefSharp.Core/WebBrowserExtensionsEx.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Copyright © 2021 The CefSharp Authors. All rights reserved.
//
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.

using CefSharp.Internals;
using System.Threading.Tasks;

namespace CefSharp
{
/// <summary>
/// Extended WebBrowserExtensions
/// </summary>
public static class WebBrowserExtensionsEx
{
/// <summary>
/// Retrieve the current <see cref="NavigationEntry"/>. Contains information like
/// <see cref="NavigationEntry.HttpStatusCode"/> and <see cref="NavigationEntry.SslStatus"/>
/// </summary>
/// <param name="browser">The ChromiumWebBrowser instance this method extends.</param>
/// <returns>
/// <see cref="Task{NavigationEntry}"/> that when executed returns the current <see cref="NavigationEntry"/> or null
/// </returns>
public static Task<NavigationEntry> GetVisibleNavigationEntryAsync(this IWebBrowser browser)
{
var host = browser.GetBrowserHost();

if (host == null)
{
return Task.FromResult<NavigationEntry>(null);
}

if(Cef.CurrentlyOnThread(CefThreadIds.TID_UI))
{
var entry = host.GetVisibleNavigationEntry();

return Task.FromResult<NavigationEntry>(entry);
}

var tcs = new TaskCompletionSource<NavigationEntry>();

Cef.UIThreadTaskFactory.StartNew(delegate
{
var entry = host.GetVisibleNavigationEntry();

tcs.TrySetResultAsync(entry);
});

return tcs.Task;
}
}
}
1 change: 1 addition & 0 deletions CefSharp.Test/CefSharp.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@
<Compile Include="Framework\PathCheckFacts.cs" />
<Compile Include="Framework\RequestContextBuilderFacts.cs" />
<Compile Include="Framework\RequestContextExtensionFacts.cs" />
<Compile Include="Framework\RequestContextFacts.cs" />
<Compile Include="Framework\TestMemberInfo.cs" />
<Compile Include="JavascriptBinding\IntegrationTestFacts.cs" />
<Compile Include="OffScreen\OffScreenBrowserBasicFacts.cs" />
Expand Down
33 changes: 33 additions & 0 deletions CefSharp.Test/Framework/RequestContextFacts.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright © 2021 The CefSharp Authors. All rights reserved.
//
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.

using Xunit;

namespace CefSharp.Test.Framework
{
//NOTE: All Test classes must be part of this collection as it manages the Cef Initialize/Shutdown lifecycle
[Collection(CefSharpFixtureCollection.Key)]
public class RequestContextFacts
{
[Fact]
public void IsSameAs()
{
var ctx1 = new RequestContext();
var ctx2 = ctx1.UnWrap();

Assert.True(ctx1.IsSame(ctx2));
}

[Fact]
public void IsSharingWith()
{
var ctx1 = RequestContext.Configure()
.WithCachePath(@"c:\temp")
.Create();
var ctx2 = new RequestContext(ctx1);

Assert.True(ctx1.IsSharingWith(ctx2));
}
}
}
39 changes: 39 additions & 0 deletions CefSharp.Test/OffScreen/OffScreenBrowserBasicFacts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.

using System.Collections.Generic;
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using CefSharp.Example;
using CefSharp.Example.Handlers;
using CefSharp.Internals;
using CefSharp.OffScreen;
using CefSharp.Web;
using Xunit;
using Xunit.Abstractions;

Expand Down Expand Up @@ -372,6 +374,43 @@ public async Task CanExecuteJavascriptInMainFrameAfterNavigatingToDifferentOrigi
}
}

[Theory]
[InlineData("http://httpbin.org/post")]
public async Task CanLoadRequestWithPostData(string url)
{
const string data = "Testing123";
//To use LoadRequest we must first load a web page
using (var browser = new ChromiumWebBrowser(new HtmlString("Testing")))
{
await browser.LoadPageAsync();

var request = new Request();
request.Url = "http://httpbin.org/post";
request.Method = "POST";
var postData = new PostData();
postData.AddElement(new PostDataElement
{
Bytes = Encoding.UTF8.GetBytes(data)
});

request.PostData = postData;

await browser.LoadRequestAsync(request);

var mainFrame = browser.GetMainFrame();
Assert.Equal(url, mainFrame.Url);

var navEntry = await browser.GetVisibleNavigationEntryAsync();

Assert.Equal((int)HttpStatusCode.OK, navEntry.HttpStatusCode);
Assert.True(navEntry.HasPostData);

var source = await browser.GetTextAsync();

Assert.Contains(data, source);
}
}

[SkipIfRunOnAppVeyorFact]
public async Task CanLoadHttpWebsiteUsingProxy()
{
Expand Down
31 changes: 31 additions & 0 deletions CefSharp.Test/WebBrowserTestExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,37 @@ public static Task LoadPageAsync(this IWebBrowser browser, string address = null
return tcs.Task;
}

public static Task LoadRequestAsync(this IWebBrowser browser, IRequest request)
{
if(request == null)
{
throw new ArgumentNullException("request");
}

//If using .Net 4.6 then use TaskCreationOptions.RunContinuationsAsynchronously
//and switch to tcs.TrySetResult below - no need for the custom extension method
var tcs = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);

EventHandler<LoadingStateChangedEventArgs> handler = null;
handler = (sender, args) =>
{
//Wait for while page to finish loading not just the first frame
if (!args.IsLoading)
{
browser.LoadingStateChanged -= handler;
//This is required when using a standard TaskCompletionSource
//Extension method found in the CefSharp.Internals namespace
tcs.TrySetResult(true);
}
};

browser.LoadingStateChanged += handler;

browser.GetMainFrame().LoadRequest(request);

return tcs.Task;
}

public static Task<bool> WaitForQUnitTestExeuctionToComplete(this IWebBrowser browser)
{
//If using .Net 4.6 then use TaskCreationOptions.RunContinuationsAsynchronously
Expand Down

0 comments on commit 497e75c

Please sign in to comment.