Skip to content
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

feat: Add support for Relax SSL validation #922

Merged
merged 15 commits into from
Mar 16, 2025
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/Appium.Net/Appium/Service/AppiumClientConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,8 @@ public static AppiumClientConfig DefaultConfig()
///
/// </summary>
public bool DirectConnect { get; set; }

public bool RelaxSslValidation { get; set; }

}
}
22 changes: 11 additions & 11 deletions src/Appium.Net/Appium/Service/AppiumCommandExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
//See the License for the specific language governing permissions and
//limitations under the License.

using OpenQA.Selenium.Remote;
using System;
using System.Threading.Tasks;

Expand All @@ -28,9 +27,9 @@ internal class AppiumCommandExecutor : ICommandExecutor

private TimeSpan CommandTimeout;

private static ICommandExecutor CreateRealExecutor(Uri remoteAddress, TimeSpan commandTimeout)
private static ICommandExecutor CreateRealExecutor(Uri remoteAddress, TimeSpan commandTimeout, AppiumClientConfig clientConfig)
{
return new HttpCommandExecutor(remoteAddress, commandTimeout);
return new AppiumHttpCommandExecutor(remoteAddress, commandTimeout, clientConfig);
}

private AppiumCommandExecutor(ICommandExecutor realExecutor)
Expand All @@ -39,25 +38,25 @@ private AppiumCommandExecutor(ICommandExecutor realExecutor)
}

internal AppiumCommandExecutor(Uri url, TimeSpan timeForTheServerResponding, AppiumClientConfig clientConfig)
: this(CreateRealExecutor(url, timeForTheServerResponding))
: this(CreateRealExecutor(url, timeForTheServerResponding, clientConfig))
{
CommandTimeout = timeForTheServerResponding;
Service = null;
ClientConfig = clientConfig;
}

internal AppiumCommandExecutor(AppiumLocalService service, TimeSpan timeForTheServerResponding, AppiumClientConfig clientConfig)
: this(CreateRealExecutor(service.ServiceUrl, timeForTheServerResponding))
: this(CreateRealExecutor(service.ServiceUrl, timeForTheServerResponding, clientConfig))
{
CommandTimeout = timeForTheServerResponding;
Service = service;
ClientConfig = clientConfig;
}

public Response Execute(Command commandToExecute)
{
return Task.Run(() => ExecuteAsync(commandToExecute)).GetAwaiter().GetResult();
}
{
return Task.Run(() => ExecuteAsync(commandToExecute)).GetAwaiter().GetResult();
}

public async Task<Response> ExecuteAsync(Command commandToExecute)
{
Expand Down Expand Up @@ -131,7 +130,7 @@ private void HandleCommandException(Command command)
if (command.Name == DriverCommand.NewSession)
{
Service?.Dispose();
}
}
}

/// <summary>
Expand All @@ -142,7 +141,8 @@ private void HandleCommandException(Command command)
private ICommandExecutor ModifyNewSessionHttpRequestHeader(ICommandExecutor commandExecutor)
{
if (commandExecutor == null) throw new ArgumentNullException(nameof(commandExecutor));
var modifiedCommandExecutor = commandExecutor as HttpCommandExecutor;

var modifiedCommandExecutor = commandExecutor as AppiumHttpCommandExecutor;

modifiedCommandExecutor.SendingRemoteHttpRequest += (sender, args) =>
args.AddHeader(IdempotencyHeader, Guid.NewGuid().ToString());
Expand Down Expand Up @@ -183,7 +183,7 @@ private ICommandExecutor GetNewExecutorWithDirectConnect(Response response)
var newUri = new DirectConnect(response).GetUri();
if (newUri != null)
{
return new HttpCommandExecutor(newUri, CommandTimeout);
return new AppiumHttpCommandExecutor(newUri, CommandTimeout);
}

return null;
Expand Down
49 changes: 49 additions & 0 deletions src/Appium.Net/Appium/Service/AppiumHttpCommandExecutor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
//Licensed under the Apache License, Version 2.0 (the "License");
//you may not use this file except in compliance with the License.
//See the NOTICE file distributed with this work for additional
//information regarding copyright ownership.
//You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
//Unless required by applicable law or agreed to in writing, software
//distributed under the License is distributed on an "AS IS" BASIS,
//WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//See the License for the specific language governing permissions and
//limitations under the License.

using OpenQA.Selenium.Remote;
using System;
using System.Net.Http;

namespace OpenQA.Selenium.Appium.Service
{
public class AppiumHttpCommandExecutor : HttpCommandExecutor
{
private readonly AppiumClientConfig _clientConfig;

public AppiumHttpCommandExecutor(Uri addressOfRemoteServer, TimeSpan timeout)
: base(addressOfRemoteServer, timeout, enableKeepAlive: true)
{

}

public AppiumHttpCommandExecutor(Uri addressOfRemoteServer, TimeSpan timeout, AppiumClientConfig clientConfig)
: base(addressOfRemoteServer, timeout, enableKeepAlive: true)
{
_clientConfig = clientConfig;
}

protected override HttpClientHandler CreateHttpClientHandler()
{
var handler = base.CreateHttpClientHandler();

if (_clientConfig != null && _clientConfig.RelaxSslValidation)
{
handler.ServerCertificateCustomValidationCallback += (sender, cert, chain, sslPolicyErrors) => true;
}
return handler;
}

}
}
11 changes: 11 additions & 0 deletions test/integration/ServerTests/AppiumClientConfigTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,16 @@ public void SetAndGetDirectConnect()
clientConfig.DirectConnect = true;
Assert.That(clientConfig.DirectConnect);
}

[Test]
public void SetAndGetRelaxSSLValidation()
{
var clientConfig = AppiumClientConfig.DefaultConfig();
Assert.That(clientConfig.RelaxSslValidation, Is.False);

clientConfig.RelaxSslValidation = true;
Assert.That(clientConfig.RelaxSslValidation);
}

}
}