Skip to content

Commit 0ebdc45

Browse files
committed
Test - Add CanLoadRequestWithPostData test
1 parent ad76ddf commit 0ebdc45

File tree

6 files changed

+156
-0
lines changed

6 files changed

+156
-0
lines changed

CefSharp.Core/CefSharp.Core.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
<Compile Include="RequestContextBuilder.cs" />
6868
<Compile Include="RequestContextSettings.cs" />
6969
<Compile Include="UrlRequest.cs" />
70+
<Compile Include="WebBrowserExtensionsEx.cs" />
7071
<Compile Include="WindowInfo.cs" />
7172
</ItemGroup>
7273
<ItemGroup>
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Copyright © 2021 The CefSharp Authors. All rights reserved.
2+
//
3+
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
4+
5+
using CefSharp.Internals;
6+
using System.Threading.Tasks;
7+
8+
namespace CefSharp
9+
{
10+
/// <summary>
11+
/// Extended WebBrowserExtensions
12+
/// </summary>
13+
public static class WebBrowserExtensionsEx
14+
{
15+
/// <summary>
16+
/// Retrieve the current <see cref="NavigationEntry"/>. Contains information like
17+
/// <see cref="NavigationEntry.HttpStatusCode"/> and <see cref="NavigationEntry.SslStatus"/>
18+
/// </summary>
19+
/// <param name="browser">The ChromiumWebBrowser instance this method extends.</param>
20+
/// <returns>
21+
/// <see cref="Task{NavigationEntry}"/> that when executed returns the current <see cref="NavigationEntry"/> or null
22+
/// </returns>
23+
public static Task<NavigationEntry> GetVisibleNavigationEntryAsync(this IWebBrowser browser)
24+
{
25+
var host = browser.GetBrowserHost();
26+
27+
if (host == null)
28+
{
29+
return Task.FromResult<NavigationEntry>(null);
30+
}
31+
32+
if(Cef.CurrentlyOnThread(CefThreadIds.TID_UI))
33+
{
34+
var entry = host.GetVisibleNavigationEntry();
35+
36+
return Task.FromResult<NavigationEntry>(entry);
37+
}
38+
39+
var tcs = new TaskCompletionSource<NavigationEntry>();
40+
41+
Cef.UIThreadTaskFactory.StartNew(delegate
42+
{
43+
var entry = host.GetVisibleNavigationEntry();
44+
45+
tcs.TrySetResultAsync(entry);
46+
});
47+
48+
return tcs.Task;
49+
}
50+
}
51+
}

CefSharp.Test/CefSharp.Test.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@
160160
<Compile Include="Framework\PathCheckFacts.cs" />
161161
<Compile Include="Framework\RequestContextBuilderFacts.cs" />
162162
<Compile Include="Framework\RequestContextExtensionFacts.cs" />
163+
<Compile Include="Framework\RequestContextFacts.cs" />
163164
<Compile Include="Framework\TestMemberInfo.cs" />
164165
<Compile Include="JavascriptBinding\IntegrationTestFacts.cs" />
165166
<Compile Include="OffScreen\OffScreenBrowserBasicFacts.cs" />
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Copyright © 2021 The CefSharp Authors. All rights reserved.
2+
//
3+
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
4+
5+
using Xunit;
6+
7+
namespace CefSharp.Test.Framework
8+
{
9+
//NOTE: All Test classes must be part of this collection as it manages the Cef Initialize/Shutdown lifecycle
10+
[Collection(CefSharpFixtureCollection.Key)]
11+
public class RequestContextFacts
12+
{
13+
[Fact]
14+
public void IsSameAs()
15+
{
16+
var ctx1 = new RequestContext();
17+
var ctx2 = ctx1.UnWrap();
18+
19+
Assert.True(ctx1.IsSame(ctx2));
20+
}
21+
22+
[Fact]
23+
public void IsSharingWith()
24+
{
25+
var ctx1 = RequestContext.Configure()
26+
.WithCachePath(@"c:\temp")
27+
.Create();
28+
var ctx2 = new RequestContext(ctx1);
29+
30+
Assert.True(ctx1.IsSharingWith(ctx2));
31+
}
32+
}
33+
}

CefSharp.Test/OffScreen/OffScreenBrowserBasicFacts.cs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@
33
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
44

55
using System.Collections.Generic;
6+
using System.Net;
67
using System.Text;
78
using System.Threading;
89
using System.Threading.Tasks;
910
using CefSharp.Example;
1011
using CefSharp.Example.Handlers;
1112
using CefSharp.Internals;
1213
using CefSharp.OffScreen;
14+
using CefSharp.Web;
1315
using Xunit;
1416
using Xunit.Abstractions;
1517

@@ -372,6 +374,43 @@ public async Task CanExecuteJavascriptInMainFrameAfterNavigatingToDifferentOrigi
372374
}
373375
}
374376

377+
[Theory]
378+
[InlineData("http://httpbin.org/post")]
379+
public async Task CanLoadRequestWithPostData(string url)
380+
{
381+
const string data = "Testing123";
382+
//To use LoadRequest we must first load a web page
383+
using (var browser = new ChromiumWebBrowser(new HtmlString("Testing")))
384+
{
385+
await browser.LoadPageAsync();
386+
387+
var request = new Request();
388+
request.Url = "http://httpbin.org/post";
389+
request.Method = "POST";
390+
var postData = new PostData();
391+
postData.AddElement(new PostDataElement
392+
{
393+
Bytes = Encoding.UTF8.GetBytes(data)
394+
});
395+
396+
request.PostData = postData;
397+
398+
await browser.LoadRequestAsync(request);
399+
400+
var mainFrame = browser.GetMainFrame();
401+
Assert.Equal(url, mainFrame.Url);
402+
403+
var navEntry = await browser.GetVisibleNavigationEntryAsync();
404+
405+
Assert.Equal((int)HttpStatusCode.OK, navEntry.HttpStatusCode);
406+
Assert.True(navEntry.HasPostData);
407+
408+
var source = await browser.GetTextAsync();
409+
410+
Assert.Contains(data, source);
411+
}
412+
}
413+
375414
[SkipIfRunOnAppVeyorFact]
376415
public async Task CanLoadHttpWebsiteUsingProxy()
377416
{

CefSharp.Test/WebBrowserTestExtensions.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,37 @@ public static Task LoadPageAsync(this IWebBrowser browser, string address = null
3737
return tcs.Task;
3838
}
3939

40+
public static Task LoadRequestAsync(this IWebBrowser browser, IRequest request)
41+
{
42+
if(request == null)
43+
{
44+
throw new ArgumentNullException("request");
45+
}
46+
47+
//If using .Net 4.6 then use TaskCreationOptions.RunContinuationsAsynchronously
48+
//and switch to tcs.TrySetResult below - no need for the custom extension method
49+
var tcs = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);
50+
51+
EventHandler<LoadingStateChangedEventArgs> handler = null;
52+
handler = (sender, args) =>
53+
{
54+
//Wait for while page to finish loading not just the first frame
55+
if (!args.IsLoading)
56+
{
57+
browser.LoadingStateChanged -= handler;
58+
//This is required when using a standard TaskCompletionSource
59+
//Extension method found in the CefSharp.Internals namespace
60+
tcs.TrySetResult(true);
61+
}
62+
};
63+
64+
browser.LoadingStateChanged += handler;
65+
66+
browser.GetMainFrame().LoadRequest(request);
67+
68+
return tcs.Task;
69+
}
70+
4071
public static Task<bool> WaitForQUnitTestExeuctionToComplete(this IWebBrowser browser)
4172
{
4273
//If using .Net 4.6 then use TaskCreationOptions.RunContinuationsAsynchronously

0 commit comments

Comments
 (0)