mirror of
https://github.com/cefsharp/CefSharp.git
synced 2026-12-04 08:03:22 +00:00
cafd378859
- Start splitting tests out into own files where relevant - Improve call ordering - Use Assert.RaisesAsync where possible - Classes have been renamed (Tests is now the standard suffix) TODO: Some of the classes still need their method names changed
93 lines
3.3 KiB
C#
93 lines
3.3 KiB
C#
// Copyright © 2020 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 System;
|
|
using System.Collections.Generic;
|
|
using System.Threading.Tasks;
|
|
using Moq;
|
|
using Xunit;
|
|
using Xunit.Abstractions;
|
|
|
|
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 RequestContextExtensionTests
|
|
{
|
|
private const string ProxyPreferenceKey = "proxy";
|
|
|
|
private readonly ITestOutputHelper output;
|
|
|
|
private delegate void SetPreferenceDelegate(string name, object value, out string errorMessage);
|
|
|
|
public RequestContextExtensionTests(ITestOutputHelper output)
|
|
{
|
|
this.output = output;
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData("http", "localhost", 8080, "http://localhost:8080")]
|
|
[InlineData("socks", "localhost", null, "socks://localhost")]
|
|
[InlineData(null, "localhost", null, "http://localhost")]
|
|
public void CanSetProxyWithSchemeHostAndPort(string scheme, string host, int? port, string expected)
|
|
{
|
|
string preferenceName = "";
|
|
object preferenceValue = null;
|
|
var mockRequestContext = new Mock<IRequestContext>();
|
|
|
|
mockRequestContext.Setup(x => x.CanSetPreference(ProxyPreferenceKey)).Returns(true);
|
|
|
|
mockRequestContext.Setup(x => x.SetPreference(ProxyPreferenceKey, It.IsAny<IDictionary<string, object>>(), out It.Ref<string>.IsAny))
|
|
.Callback(new SetPreferenceDelegate((string name, object value, out string errorMessage) =>
|
|
{
|
|
preferenceName = name;
|
|
preferenceValue = value;
|
|
errorMessage = "OK";
|
|
}))
|
|
.Returns(true);
|
|
|
|
string msg;
|
|
|
|
var result = mockRequestContext.Object.SetProxy(scheme, host, port, out msg);
|
|
var dict = (Dictionary<string, object>)preferenceValue;
|
|
|
|
Assert.True(result);
|
|
Assert.Equal("OK", msg);
|
|
Assert.Equal(ProxyPreferenceKey, preferenceName);
|
|
Assert.Equal(2, dict.Count);
|
|
Assert.Equal("fixed_servers", dict["mode"]);
|
|
Assert.Equal(expected, dict["server"]);
|
|
}
|
|
|
|
[Fact]
|
|
public void SetProxyThrowsExceptionOnInvalidScheme()
|
|
{
|
|
var mockRequestContext = new Mock<IRequestContext>();
|
|
|
|
mockRequestContext.Setup(x => x.SetPreference(ProxyPreferenceKey, It.IsAny<IDictionary<string, object>>(), out It.Ref<string>.IsAny)).Returns(true);
|
|
|
|
Assert.Throws<ArgumentException>(() =>
|
|
{
|
|
string msg;
|
|
mockRequestContext.Object.SetProxy("myscheme", "localhost", 0, out msg);
|
|
});
|
|
}
|
|
|
|
[Fact]
|
|
public async Task CanGetCookieManagerForRequestContextAsync()
|
|
{
|
|
var requestContext = RequestContext
|
|
.Configure()
|
|
.Create();
|
|
|
|
var cookieManager = await requestContext.GetCookieManagerAsync();
|
|
|
|
var cookies = await cookieManager.VisitAllCookiesAsync();
|
|
|
|
Assert.NotNull(cookies);
|
|
output.WriteLine("Cookie Count {0}", cookies.Count);
|
|
}
|
|
}
|
|
}
|