mirror of
https://github.com/PepperDash/Essentials.git
synced 2026-02-13 19:54:59 +00:00
test: more mocks
This commit is contained in:
@@ -42,6 +42,66 @@ namespace Crestron.SimplSharp.CrestronDataStore
|
||||
value = "";
|
||||
return CDS_ERROR.CDS_SUCCESS;
|
||||
}
|
||||
|
||||
/// <summary>Initialize the Crestron data store</summary>
|
||||
/// <returns>0 on success, negative on error</returns>
|
||||
public static int InitCrestronDataStore()
|
||||
{
|
||||
// Mock implementation
|
||||
return 0;
|
||||
}
|
||||
|
||||
/// <summary>Get a boolean value from local storage</summary>
|
||||
/// <param name="key">The key to retrieve</param>
|
||||
/// <param name="value">The retrieved value</param>
|
||||
/// <returns>0 on success, negative on error</returns>
|
||||
public static int GetLocalBoolValue(string key, out bool value)
|
||||
{
|
||||
// Mock implementation - always return false for now
|
||||
value = false;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/// <summary>Set a boolean value in local storage</summary>
|
||||
/// <param name="key">The key to set</param>
|
||||
/// <param name="value">The value to set</param>
|
||||
/// <returns>0 on success, negative on error</returns>
|
||||
public static int SetLocalBoolValue(string key, bool value)
|
||||
{
|
||||
// Mock implementation
|
||||
return 0;
|
||||
}
|
||||
|
||||
/// <summary>Get an integer value from local storage</summary>
|
||||
/// <param name="key">The key to retrieve</param>
|
||||
/// <param name="value">The retrieved value</param>
|
||||
/// <returns>0 on success, negative on error</returns>
|
||||
public static int GetLocalIntValue(string key, out int value)
|
||||
{
|
||||
// Mock implementation - always return 0 for now
|
||||
value = 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/// <summary>Set an integer value in local storage</summary>
|
||||
/// <param name="key">The key to set</param>
|
||||
/// <param name="value">The value to set</param>
|
||||
/// <returns>0 on success, negative on error</returns>
|
||||
public static int SetLocalIntValue(string key, int value)
|
||||
{
|
||||
// Mock implementation
|
||||
return 0;
|
||||
}
|
||||
|
||||
/// <summary>Set an unsigned integer value in local storage</summary>
|
||||
/// <param name="key">The key to set</param>
|
||||
/// <param name="value">The value to set</param>
|
||||
/// <returns>0 on success, negative on error</returns>
|
||||
public static int SetLocalUintValue(string key, uint value)
|
||||
{
|
||||
// Mock implementation
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
public enum CDS_ERROR
|
||||
@@ -49,4 +109,11 @@ namespace Crestron.SimplSharp.CrestronDataStore
|
||||
CDS_SUCCESS = 0,
|
||||
CDS_ERROR = -1
|
||||
}
|
||||
|
||||
/// <summary>Mock CrestronDataStore for local data storage</summary>
|
||||
public static class CrestronDataStore
|
||||
{
|
||||
/// <summary>Error constant for CDS operations</summary>
|
||||
public static readonly int CDS_ERROR = -1;
|
||||
}
|
||||
}
|
||||
|
||||
27
src/CrestronMock/ConsoleTypes.cs
Normal file
27
src/CrestronMock/ConsoleTypes.cs
Normal file
@@ -0,0 +1,27 @@
|
||||
using System;
|
||||
|
||||
namespace Crestron.SimplSharp
|
||||
{
|
||||
/// <summary>Mock console command response utility</summary>
|
||||
public static class ConsoleCommandResponseUtility
|
||||
{
|
||||
/// <summary>Send console command response with response code</summary>
|
||||
/// <param name="response">The response text</param>
|
||||
/// <param name="responseCode">The response code</param>
|
||||
public static void ConsoleCommandResponse(string response, int responseCode = 0)
|
||||
{
|
||||
// Mock implementation - just log to console or ignore
|
||||
Console.WriteLine($"Console Response ({responseCode}): {response}");
|
||||
}
|
||||
|
||||
/// <summary>Send console command response with additional parameter</summary>
|
||||
/// <param name="response">The response text</param>
|
||||
/// <param name="param1">First parameter</param>
|
||||
/// <param name="param2">Second parameter</param>
|
||||
public static void ConsoleCommandResponse(string response, object param1, object param2)
|
||||
{
|
||||
// Mock implementation
|
||||
Console.WriteLine($"Console Response: {response} - {param1}, {param2}");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -17,20 +17,49 @@ namespace Crestron.SimplSharp
|
||||
// Mock implementation
|
||||
}
|
||||
|
||||
/// <summary>Ethernet event handler delegate</summary>
|
||||
public delegate void EthernetEventHandler(EthernetEventArgs args);
|
||||
|
||||
/// <summary>Mock CrestronEnvironment for system event handling</summary>
|
||||
public static class CrestronEnvironment
|
||||
{
|
||||
/// <summary>Event fired when program status changes</summary>
|
||||
public static event Action<eProgramStatusEventType>? ProgramStatusEventHandler;
|
||||
public static event ProgramStatusEventHandler? ProgramStatusEventHandler;
|
||||
|
||||
/// <summary>Event fired when ethernet status changes</summary>
|
||||
public static event Action<EthernetEventArgs>? EthernetEventHandler;
|
||||
public static event EthernetEventHandler? EthernetEventHandler;
|
||||
|
||||
/// <summary>Gets the device platform</summary>
|
||||
public static string DevicePlatform => "Mock";
|
||||
public static eDevicePlatform DevicePlatform => eDevicePlatform.Appliance;
|
||||
|
||||
/// <summary>Gets the runtime environment</summary>
|
||||
public static string RuntimeEnvironment => "Test";
|
||||
public static eRuntimeEnvironment RuntimeEnvironment => eRuntimeEnvironment.SimplSharpPro;
|
||||
|
||||
/// <summary>Gets system information</summary>
|
||||
public static string SystemInfo => "Mock System v1.0";
|
||||
|
||||
/// <summary>Gets OS version</summary>
|
||||
public static string OSVersion => "Mock OS 1.0.0";
|
||||
|
||||
/// <summary>Gets new line character sequence</summary>
|
||||
public static string NewLine => Environment.NewLine;
|
||||
|
||||
/// <summary>Gets program compatibility level</summary>
|
||||
public static eProgramCompatibility ProgramCompatibility => eProgramCompatibility.Series3And4;
|
||||
|
||||
/// <summary>Sleep for specified milliseconds</summary>
|
||||
/// <param name="milliseconds">Sleep duration</param>
|
||||
public static void Sleep(int milliseconds)
|
||||
{
|
||||
System.Threading.Thread.Sleep(milliseconds);
|
||||
}
|
||||
|
||||
/// <summary>Gets the time zone</summary>
|
||||
/// <returns>Time zone string</returns>
|
||||
public static string GetTimeZone()
|
||||
{
|
||||
return TimeZoneInfo.Local.Id;
|
||||
}
|
||||
|
||||
/// <summary>Triggers a program status event (for testing)</summary>
|
||||
/// <param name="eventType">Event type</param>
|
||||
|
||||
@@ -56,6 +56,13 @@ namespace Crestron.SimplSharp
|
||||
// Mock implementation
|
||||
}
|
||||
|
||||
/// <summary>Initializes a new instance of the CrestronQueue class with specified capacity</summary>
|
||||
/// <param name="capacity">The initial capacity of the queue</param>
|
||||
public CrestronQueue(int capacity)
|
||||
{
|
||||
// Mock implementation - capacity is ignored in this mock
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Public Methods
|
||||
|
||||
@@ -6,13 +6,13 @@ namespace Crestron.SimplSharp
|
||||
public enum eProgramStatusEventType
|
||||
{
|
||||
/// <summary>Program stopping</summary>
|
||||
eProgramStopping = 0,
|
||||
Stopping = 0,
|
||||
/// <summary>Program started</summary>
|
||||
eProgramStarted = 1,
|
||||
Starting = 1,
|
||||
/// <summary>Program running</summary>
|
||||
Running = 2,
|
||||
/// <summary>Program paused</summary>
|
||||
eProgramPaused = 2,
|
||||
/// <summary>Program resumed</summary>
|
||||
eProgramResumed = 3
|
||||
Paused = 3
|
||||
}
|
||||
|
||||
/// <summary>Mock EthernetEventArgs class</summary>
|
||||
@@ -122,6 +122,10 @@ namespace Crestron.SimplSharp.CrestronIO
|
||||
return new System.IO.FileStream(FullName, System.IO.FileMode.Open, System.IO.FileAccess.Read);
|
||||
}
|
||||
}
|
||||
|
||||
// Event handler delegates
|
||||
/// <summary>Ethernet event handler delegate</summary>
|
||||
public delegate void EthernetEventHandler(EthernetEventArgs args);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -1,6 +1,13 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using System.Threa case ETHERNET_PARAMETER_TO_GET.ETHERNET_HOSTNAME:
|
||||
return "mock-hostname";
|
||||
case ETHERNET_PARAMETER_TO_GET.ETHERNET_MAC_ADDRESS:
|
||||
return "00:11:22:33:44:55";
|
||||
case ETHERNET_PARAMETER_TO_GET.GET_DOMAIN_NAME:
|
||||
return "mock-domain.local";
|
||||
default:
|
||||
return string.Empty; asks;
|
||||
|
||||
namespace Crestron.SimplSharp
|
||||
{
|
||||
@@ -19,6 +26,26 @@ namespace Crestron.SimplSharp
|
||||
|
||||
public static class CrestronEthernetHelper
|
||||
{
|
||||
/// <summary>Ethernet parameter enumeration</summary>
|
||||
public enum ETHERNET_PARAMETER_TO_GET
|
||||
{
|
||||
ETHERNET_HOSTNAME = 0,
|
||||
ETHERNET_DOMAIN_NAME = 1,
|
||||
ETHERNET_IP_ADDRESS = 2,
|
||||
ETHERNET_SUBNET_MASK = 3,
|
||||
ETHERNET_GATEWAY = 4,
|
||||
ETHERNET_DNS_SERVER = 5,
|
||||
ETHERNET_MAC_ADDRESS = 6,
|
||||
ETHERNET_DHCP_STATUS = 7,
|
||||
GET_CURRENT_DHCP_STATE = 8,
|
||||
GET_CURRENT_IP_ADDRESS = 9,
|
||||
GET_CURRENT_IP_MASK = 10,
|
||||
GET_CURRENT_ROUTER = 11,
|
||||
GET_HOSTNAME = 12,
|
||||
GET_LINK_STATUS = 13,
|
||||
GET_DOMAIN_NAME = 14
|
||||
}
|
||||
|
||||
public static List<string> GetEthernetAdaptersInfo()
|
||||
{
|
||||
return new List<string> { "MockAdapter" };
|
||||
@@ -28,47 +55,58 @@ namespace Crestron.SimplSharp
|
||||
{
|
||||
return "MockValue";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
namespace Crestron.SimplSharp.Net.Https
|
||||
{
|
||||
public class UrlParser
|
||||
{
|
||||
public string Url { get; set; }
|
||||
|
||||
public UrlParser(string url)
|
||||
/// <summary>Get ethernet parameter as string</summary>
|
||||
/// <param name="parameter">The parameter to get</param>
|
||||
/// <param name="adapterType">The adapter type</param>
|
||||
/// <returns>The parameter value as string</returns>
|
||||
public static string GetEthernetParameter(ETHERNET_PARAMETER_TO_GET parameter, EthernetAdapterType adapterType)
|
||||
{
|
||||
Url = url;
|
||||
// Mock implementation
|
||||
switch (parameter)
|
||||
{
|
||||
case ETHERNET_PARAMETER_TO_GET.ETHERNET_IP_ADDRESS:
|
||||
return "192.168.1.100";
|
||||
case ETHERNET_PARAMETER_TO_GET.ETHERNET_SUBNET_MASK:
|
||||
return "255.255.255.0";
|
||||
case ETHERNET_PARAMETER_TO_GET.ETHERNET_GATEWAY:
|
||||
return "192.168.1.1";
|
||||
case ETHERNET_PARAMETER_TO_GET.ETHERNET_HOSTNAME:
|
||||
return "MockHost";
|
||||
case ETHERNET_PARAMETER_TO_GET.ETHERNET_MAC_ADDRESS:
|
||||
return "00:11:22:33:44:55";
|
||||
default:
|
||||
return string.Empty;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Get adapter ID for specified adapter type</summary>
|
||||
/// <param name="adapterType">The adapter type</param>
|
||||
/// <returns>The adapter ID</returns>
|
||||
public static int GetAdapterdIdForSpecifiedAdapterType(EthernetAdapterType adapterType)
|
||||
{
|
||||
// Mock implementation
|
||||
return (int)adapterType;
|
||||
}
|
||||
|
||||
/// <summary>Check if control subnet is in automatic mode</summary>
|
||||
/// <param name="adapterId">The adapter ID</param>
|
||||
/// <returns>True if in automatic mode</returns>
|
||||
public static bool IsControlSubnetInAutomaticMode(int adapterId)
|
||||
{
|
||||
// Mock implementation
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
public class HttpsHeader
|
||||
/// <summary>Mock EthernetAdapterType enumeration</summary>
|
||||
public enum EthernetAdapterType
|
||||
{
|
||||
public string Name { get; set; }
|
||||
public string Value { get; set; }
|
||||
|
||||
public HttpsHeader(string name, string value)
|
||||
{
|
||||
Name = name;
|
||||
Value = value;
|
||||
}
|
||||
}
|
||||
|
||||
public class HttpException : Exception
|
||||
{
|
||||
public HttpException(string message) : base(message) { }
|
||||
public HttpException(string message, Exception innerException) : base(message, innerException) { }
|
||||
}
|
||||
}
|
||||
|
||||
namespace System.Collections.Generic
|
||||
{
|
||||
public static class DictionaryExtensions
|
||||
{
|
||||
public static void AddHeader(this Dictionary<string, string> dictionary, Crestron.SimplSharp.Net.Https.HttpsHeader header)
|
||||
{
|
||||
dictionary[header.Name] = header.Value;
|
||||
}
|
||||
/// <summary>Ethernet LAN adapter</summary>
|
||||
EthernetLANAdapter = 0,
|
||||
/// <summary>Control subnet adapter</summary>
|
||||
ControlSubnet = 1,
|
||||
/// <summary>Auto-detect adapter</summary>
|
||||
EthernetAdapterAuto = 2
|
||||
}
|
||||
}
|
||||
|
||||
41
src/CrestronMock/NetworkingExtensions.cs
Normal file
41
src/CrestronMock/NetworkingExtensions.cs
Normal file
@@ -0,0 +1,41 @@
|
||||
using System;
|
||||
using System.Net;
|
||||
|
||||
namespace Crestron.SimplSharp.CrestronSockets
|
||||
{
|
||||
// Additional types needed for networking compatibility
|
||||
|
||||
/// <summary>IP address extensions and utilities</summary>
|
||||
public static class IPAddress
|
||||
{
|
||||
/// <summary>Parse IP address string</summary>
|
||||
public static System.Net.IPAddress Parse(string ipString)
|
||||
{
|
||||
return System.Net.IPAddress.Parse(ipString);
|
||||
}
|
||||
|
||||
/// <summary>Any IP address</summary>
|
||||
public static System.Net.IPAddress Any => System.Net.IPAddress.Any;
|
||||
}
|
||||
}
|
||||
|
||||
namespace Crestron.SimplSharp
|
||||
{
|
||||
/// <summary>Extensions for CrestronQueue</summary>
|
||||
public static class CrestronQueueExtensions
|
||||
{
|
||||
/// <summary>Try to enqueue item</summary>
|
||||
public static bool TryToEnqueue<T>(this CrestronQueue<T> queue, T item)
|
||||
{
|
||||
try
|
||||
{
|
||||
queue.Enqueue(item);
|
||||
return true;
|
||||
}
|
||||
catch
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,3 +1,6 @@
|
||||
using System;
|
||||
using System.Net.Sockets;
|
||||
|
||||
namespace Crestron.SimplSharp.CrestronSockets
|
||||
{
|
||||
/// <summary>Mock EthernetAdapterType enumeration</summary>
|
||||
@@ -45,6 +48,27 @@ namespace Crestron.SimplSharp.CrestronSockets
|
||||
/// <summary>Address already in use</summary>
|
||||
SOCKET_ADDRESS_IN_USE = 14,
|
||||
/// <summary>Invalid parameter</summary>
|
||||
SOCKET_INVALID_PARAMETER = 15
|
||||
SOCKET_INVALID_PARAMETER = 15,
|
||||
/// <summary>Connection in progress</summary>
|
||||
SOCKET_CONNECTION_IN_PROGRESS = 16
|
||||
}
|
||||
|
||||
/// <summary>Mock socket exception</summary>
|
||||
public class SocketException : Exception
|
||||
{
|
||||
/// <summary>Error code</summary>
|
||||
public int ErrorCode { get; }
|
||||
|
||||
/// <summary>Constructor with error code</summary>
|
||||
public SocketException(int errorCode, string message) : base(message)
|
||||
{
|
||||
ErrorCode = errorCode;
|
||||
}
|
||||
|
||||
/// <summary>Constructor with message only</summary>
|
||||
public SocketException(string message) : base(message)
|
||||
{
|
||||
ErrorCode = -1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,10 @@
|
||||
using System;
|
||||
using System.Threading;
|
||||
|
||||
namespace Crestron.SimplSharp
|
||||
{
|
||||
public delegate void ProgramStatusEventHandler(eProgramStatusEventType eventType);
|
||||
|
||||
public class InitialParametersClass
|
||||
{
|
||||
public static string ApplicationDirectory { get; set; } = "/User/";
|
||||
@@ -11,6 +14,13 @@ namespace Crestron.SimplSharp
|
||||
public static uint ProgramNumber { get; set; } = 1;
|
||||
public static eDevicePlatform DevicePlatform { get; set; } = eDevicePlatform.Appliance;
|
||||
public static eCrestronSeries ControllerSeries { get; set; } = eCrestronSeries.FourSeries;
|
||||
|
||||
// Additional properties needed by PepperDash.Core
|
||||
public static string RoomId { get; set; } = "Room001";
|
||||
public static string RoomName { get; set; } = "Conference Room";
|
||||
public static uint ApplicationNumber { get; set; } = 1;
|
||||
public static string ControllerPromptName { get; set; } = "TestController";
|
||||
public static string ProgramDirectory { get; set; } = "/User/";
|
||||
}
|
||||
|
||||
public enum eDevicePlatform
|
||||
@@ -24,7 +34,11 @@ namespace Crestron.SimplSharp
|
||||
{
|
||||
TwoSeries = 2,
|
||||
ThreeSeries = 3,
|
||||
FourSeries = 4
|
||||
FourSeries = 4,
|
||||
// Alias names used in some contexts
|
||||
Series2 = 2,
|
||||
Series3 = 3,
|
||||
Series4 = 4
|
||||
}
|
||||
|
||||
public enum eRuntimeEnvironment
|
||||
@@ -32,4 +46,16 @@ namespace Crestron.SimplSharp
|
||||
SimplSharpPro = 0,
|
||||
SimplSharp = 1
|
||||
}
|
||||
|
||||
public enum eProgramCompatibility
|
||||
{
|
||||
Series3And4 = 0,
|
||||
Series3Only = 1,
|
||||
Series4Only = 2
|
||||
}
|
||||
|
||||
public static class Timeout
|
||||
{
|
||||
public const int Infinite = -1;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,7 +22,9 @@ namespace Crestron.SimplSharp.CrestronSockets
|
||||
/// <summary>Socket error</summary>
|
||||
SOCKET_STATUS_SOCKET_ERROR = 8,
|
||||
/// <summary>Secure connection failed</summary>
|
||||
SOCKET_STATUS_SSL_FAILED = 9
|
||||
SOCKET_STATUS_SSL_FAILED = 9,
|
||||
/// <summary>No connection available</summary>
|
||||
SOCKET_STATUS_NO_CONNECT = 10
|
||||
}
|
||||
|
||||
/// <summary>Mock ServerState enumeration</summary>
|
||||
@@ -31,7 +33,9 @@ namespace Crestron.SimplSharp.CrestronSockets
|
||||
/// <summary>Server is not listening</summary>
|
||||
SERVER_NOT_LISTENING = 0,
|
||||
/// <summary>Server is listening</summary>
|
||||
SERVER_LISTENING = 1
|
||||
SERVER_LISTENING = 1,
|
||||
/// <summary>Server is connected</summary>
|
||||
SERVER_CONNECTED = 2
|
||||
}
|
||||
|
||||
/// <summary>Mock event handler for TCP client status changes</summary>
|
||||
@@ -43,6 +47,16 @@ namespace Crestron.SimplSharp.CrestronSockets
|
||||
/// <param name="client">TCP client instance</param>
|
||||
public delegate void TCPClientConnectCallback(TCPClient client);
|
||||
|
||||
/// <summary>Delegate for TCP client send callback</summary>
|
||||
/// <param name="client">TCP client instance</param>
|
||||
/// <param name="numberOfBytesSent">Number of bytes sent</param>
|
||||
public delegate void TCPClientSendCallback(TCPClient client, int numberOfBytesSent);
|
||||
|
||||
/// <summary>Delegate for TCP client receive callback</summary>
|
||||
/// <param name="client">TCP client instance</param>
|
||||
/// <param name="numberOfBytesReceived">Number of bytes received</param>
|
||||
public delegate void TCPClientReceiveCallback(TCPClient client, int numberOfBytesReceived);
|
||||
|
||||
/// <summary>Mock event handler for receiving TCP client data</summary>
|
||||
/// <param name="client">The TCP client</param>
|
||||
/// <param name="numberOfBytesReceived">Number of bytes received</param>
|
||||
@@ -101,7 +115,10 @@ namespace Crestron.SimplSharp.CrestronSockets
|
||||
}
|
||||
|
||||
/// <summary>Gets the address the client is connected to</summary>
|
||||
public string AddressClientConnectedTo { get; private set; } = string.Empty;
|
||||
public string AddressClientConnectedTo { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>Gets the local port number of the client</summary>
|
||||
public uint LocalPortNumberOfClient { get; private set; } = 0;
|
||||
|
||||
/// <summary>Gets the incoming data buffer</summary>
|
||||
public byte[] IncomingDataBuffer { get; private set; } = new byte[0];
|
||||
@@ -251,6 +268,18 @@ namespace Crestron.SimplSharp.CrestronSockets
|
||||
return SendData(dataToSend, lengthToSend);
|
||||
}
|
||||
|
||||
/// <summary>Sends data to the connected server asynchronously with callback</summary>
|
||||
/// <param name="dataToSend">Data to send as byte array</param>
|
||||
/// <param name="lengthToSend">Number of bytes to send</param>
|
||||
/// <param name="callback">Callback to invoke when send completes</param>
|
||||
/// <returns>Number of bytes sent, or -1 on error</returns>
|
||||
public int SendDataAsync(byte[] dataToSend, int lengthToSend, TCPClientSendCallback callback)
|
||||
{
|
||||
var result = SendData(dataToSend, lengthToSend);
|
||||
callback?.Invoke(this, result);
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>Receives data from the server asynchronously</summary>
|
||||
/// <returns>Number of bytes received, or -1 on error</returns>
|
||||
public int ReceiveDataAsync()
|
||||
@@ -262,6 +291,16 @@ namespace Crestron.SimplSharp.CrestronSockets
|
||||
return 0;
|
||||
}
|
||||
|
||||
/// <summary>Receives data from the server asynchronously with callback</summary>
|
||||
/// <param name="callback">Callback to invoke when data is received</param>
|
||||
/// <returns>Number of bytes received, or -1 on error</returns>
|
||||
public int ReceiveDataAsync(TCPClientReceiveCallback callback)
|
||||
{
|
||||
var result = ReceiveDataAsync();
|
||||
callback?.Invoke(this, result);
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>Simulates receiving data (for testing purposes)</summary>
|
||||
/// <param name="data">Data to simulate receiving</param>
|
||||
public void SimulateDataReceived(string data)
|
||||
|
||||
@@ -15,6 +15,8 @@ namespace Crestron.SimplSharp.CrestronSockets
|
||||
private readonly List<TCPClientConnection> _clients = new List<TCPClientConnection>();
|
||||
private bool _listening;
|
||||
private readonly object _lockObject = new object();
|
||||
private int _bufferSize = 4096;
|
||||
private CancellationTokenSource? _cancellationTokenSource;
|
||||
|
||||
/// <summary>Event fired when waiting for connections</summary>
|
||||
public event TCPServerWaitingForConnectionsEventHandler? WaitingForConnections;
|
||||
@@ -28,13 +30,20 @@ namespace Crestron.SimplSharp.CrestronSockets
|
||||
/// <summary>Event fired when data is received from a client</summary>
|
||||
public event TCPServerReceiveDataEventHandler? ReceivedData;
|
||||
|
||||
/// <summary>Event fired when socket status changes</summary>
|
||||
public event TCPServerWaitingForConnectionsEventHandler? SocketStatusChange;
|
||||
|
||||
/// <summary>Gets the server state</summary>
|
||||
public SocketServerState State { get; private set; } = SocketServerState.SERVER_NOT_LISTENING;
|
||||
public ServerState State { get; private set; } = ServerState.SERVER_NOT_LISTENING;
|
||||
|
||||
/// <summary>Gets the port number</summary>
|
||||
public int PortNumber { get; private set; }
|
||||
/// <summary>Gets or sets the port number</summary>
|
||||
public int PortNumber { get; set; }
|
||||
|
||||
/// <summary>Gets the maximum number of clients</summary>
|
||||
/// <summary>Gets or sets the socket send or receive timeout in milliseconds</summary>
|
||||
public int SocketSendOrReceiveTimeOutInMs { get; set; } = 30000;
|
||||
|
||||
/// <summary>Gets the server socket status based on current state</summary>
|
||||
public SocketStatus ServerSocketStatus => State == ServerState.SERVER_LISTENING ? SocketStatus.SOCKET_STATUS_CONNECTED : SocketStatus.SOCKET_STATUS_NOT_CONNECTED; /// <summary>Gets the maximum number of clients</summary>
|
||||
public int MaxNumberOfClientSupported { get; private set; }
|
||||
|
||||
/// <summary>Gets the number of connected clients</summary>
|
||||
@@ -49,26 +58,38 @@ namespace Crestron.SimplSharp.CrestronSockets
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Initializes a new instance of TCPServer</summary>
|
||||
/// <summary>Creates a TCP server with IP address binding</summary>
|
||||
/// <param name="ipAddress">IP address to bind to</param>
|
||||
/// <param name="portNumber">Port number to listen on</param>
|
||||
/// <param name="bufferSize">Buffer size for data reception</param>
|
||||
/// <param name="bufferSize">Buffer size for incoming data</param>
|
||||
/// <param name="ethernetAdapterToBindTo">Ethernet adapter to bind to</param>
|
||||
/// <param name="maxNumberOfClientSupported">Maximum number of clients</param>
|
||||
public TCPServer(string ipAddress, int portNumber, int bufferSize, EthernetAdapterType ethernetAdapterToBindTo, int maxNumberOfClientSupported)
|
||||
{
|
||||
PortNumber = portNumber;
|
||||
MaxNumberOfClientSupported = maxNumberOfClientSupported;
|
||||
_bufferSize = bufferSize;
|
||||
}
|
||||
|
||||
/// <summary>Initializes a new instance of TCPServer</summary>
|
||||
/// <summary>Creates a TCP server</summary>
|
||||
/// <param name="portNumber">Port number to listen on</param>
|
||||
/// <param name="bufferSize">Buffer size for data reception</param>
|
||||
/// <param name="bufferSize">Buffer size for incoming data</param>
|
||||
/// <param name="maxNumberOfClientSupported">Maximum number of clients</param>
|
||||
public TCPServer(int portNumber, int bufferSize, int maxNumberOfClientSupported)
|
||||
{
|
||||
PortNumber = portNumber;
|
||||
MaxNumberOfClientSupported = maxNumberOfClientSupported;
|
||||
_bufferSize = bufferSize;
|
||||
}
|
||||
|
||||
/// <summary>Creates a TCP server with just port and max clients</summary>
|
||||
/// <param name="portNumber">Port number to listen on</param>
|
||||
/// <param name="maxNumberOfClientSupported">Maximum number of clients</param>
|
||||
public TCPServer(int portNumber, int maxNumberOfClientSupported)
|
||||
{
|
||||
PortNumber = portNumber;
|
||||
MaxNumberOfClientSupported = maxNumberOfClientSupported;
|
||||
_bufferSize = 4096; // Default buffer size
|
||||
}
|
||||
|
||||
/// <summary>Starts listening for client connections</summary>
|
||||
@@ -83,21 +104,31 @@ namespace Crestron.SimplSharp.CrestronSockets
|
||||
_listener = new TcpListener(IPAddress.Any, PortNumber);
|
||||
_listener.Start();
|
||||
_listening = true;
|
||||
State = SocketServerState.SERVER_LISTENING;
|
||||
State = ServerState.SERVER_LISTENING;
|
||||
_cancellationTokenSource = new CancellationTokenSource();
|
||||
|
||||
WaitingForConnections?.Invoke(this, new TCPServerWaitingForConnectionsEventArgs(0));
|
||||
|
||||
_ = Task.Run(AcceptClientsAsync);
|
||||
// Start accepting clients in background
|
||||
_ = Task.Run(() => AcceptClientsAsync());
|
||||
|
||||
return SocketErrorCodes.SOCKET_OK;
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
State = SocketServerState.SERVER_NOT_LISTENING;
|
||||
State = ServerState.SERVER_NOT_LISTENING;
|
||||
return SocketErrorCodes.SOCKET_CONNECTION_FAILED;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Starts listening for connections asynchronously with callback</summary>
|
||||
/// <param name="ipAddress">IP address to listen on</param>
|
||||
/// <param name="callback">Callback for connection events</param>
|
||||
/// <returns>SocketErrorCodes indicating success or failure</returns>
|
||||
public SocketErrorCodes WaitForConnectionAsync(string ipAddress, TCPServerWaitingForConnectionsEventHandler callback)
|
||||
{
|
||||
SocketStatusChange += callback;
|
||||
return WaitForConnectionAsync();
|
||||
}
|
||||
|
||||
/// <summary>Stops listening for connections</summary>
|
||||
/// <returns>SocketErrorCodes indicating success or failure</returns>
|
||||
public SocketErrorCodes Stop()
|
||||
@@ -109,7 +140,7 @@ namespace Crestron.SimplSharp.CrestronSockets
|
||||
{
|
||||
_listening = false;
|
||||
_listener?.Stop();
|
||||
State = SocketServerState.SERVER_NOT_LISTENING;
|
||||
State = ServerState.SERVER_NOT_LISTENING;
|
||||
|
||||
lock (_lockObject)
|
||||
{
|
||||
@@ -354,6 +385,20 @@ namespace Crestron.SimplSharp.CrestronSockets
|
||||
: base(portNumber, bufferSize, maxNumberOfClientSupported)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>Initializes a new instance of SecureTCPServer</summary>
|
||||
/// <param name="portNumber">Port number to listen on</param>
|
||||
/// <param name="maxNumberOfClientSupported">Maximum number of clients</param>
|
||||
public SecureTCPServer(int portNumber, int maxNumberOfClientSupported)
|
||||
: base(portNumber, 4096, maxNumberOfClientSupported) // Default buffer size
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>Gets or sets the handshake timeout in seconds</summary>
|
||||
public int HandshakeTimeout { get; set; } = 30;
|
||||
|
||||
/// <summary>Event raised when socket status changes with client details</summary>
|
||||
public event SecureTCPServerSocketStatusEventHandler? SocketStatusChangeWithClientDetails;
|
||||
}
|
||||
|
||||
/// <summary>Internal class representing a client connection</summary>
|
||||
@@ -458,7 +503,9 @@ namespace Crestron.SimplSharp.CrestronSockets
|
||||
/// <summary>Server is not listening</summary>
|
||||
SERVER_NOT_LISTENING = 0,
|
||||
/// <summary>Server is listening for connections</summary>
|
||||
SERVER_LISTENING = 1
|
||||
SERVER_LISTENING = 1,
|
||||
/// <summary>Server is connected</summary>
|
||||
SERVER_CONNECTED = 2
|
||||
}
|
||||
|
||||
// Event handler delegates
|
||||
@@ -466,6 +513,9 @@ namespace Crestron.SimplSharp.CrestronSockets
|
||||
public delegate void TCPServerClientConnectEventHandler(TCPServer server, TCPServerClientConnectEventArgs args);
|
||||
public delegate void TCPServerClientDisconnectEventHandler(TCPServer server, TCPServerClientDisconnectEventArgs args);
|
||||
public delegate void TCPServerReceiveDataEventHandler(TCPServer server, TCPServerReceiveDataEventArgs args);
|
||||
public delegate void SecureTCPServerSocketStatusChangeEventHandler(SecureTCPServer server, TCPServerWaitingForConnectionsEventArgs args);
|
||||
/// <summary>Delegate for secure TCP server socket status changes with client details</summary>
|
||||
public delegate void SecureTCPServerSocketStatusEventHandler(SecureTCPServer server, uint clientIndex, SocketStatus serverSocketStatus);
|
||||
|
||||
// Event argument classes
|
||||
public class TCPServerWaitingForConnectionsEventArgs : EventArgs
|
||||
|
||||
@@ -20,12 +20,34 @@ namespace Crestron.SimplSharp.CrestronSockets
|
||||
/// <summary>Gets the server state</summary>
|
||||
public SocketServerState State { get; private set; } = SocketServerState.SERVER_NOT_LISTENING;
|
||||
|
||||
/// <summary>Gets the port number</summary>
|
||||
public int PortNumber { get; private set; }
|
||||
/// <summary>Gets the server status (alias for State)</summary>
|
||||
public SocketServerState ServerStatus => State;
|
||||
|
||||
/// <summary>Gets the client status as SocketStatus</summary>
|
||||
public SocketStatus ClientStatus => State == SocketServerState.SERVER_LISTENING ? SocketStatus.SOCKET_STATUS_CONNECTED : SocketStatus.SOCKET_STATUS_NOT_CONNECTED;
|
||||
|
||||
/// <summary>Gets or sets the port number</summary>
|
||||
public int PortNumber { get; set; }
|
||||
|
||||
/// <summary>Gets the buffer size</summary>
|
||||
public int BufferSize { get; private set; }
|
||||
|
||||
/// <summary>Gets the IP address of the last message received from</summary>
|
||||
public string IPAddressLastMessageReceivedFrom { get; private set; } = string.Empty;
|
||||
|
||||
/// <summary>Gets the IP port of the last message received from</summary>
|
||||
public int IPPortLastMessageReceivedFrom { get; private set; }
|
||||
|
||||
/// <summary>Gets the incoming data buffer</summary>
|
||||
public byte[] IncomingDataBuffer { get; private set; } = new byte[0];
|
||||
|
||||
/// <summary>Initializes a new instance of UDPServer</summary>
|
||||
public UDPServer()
|
||||
{
|
||||
PortNumber = 0;
|
||||
BufferSize = 1024;
|
||||
}
|
||||
|
||||
/// <summary>Initializes a new instance of UDPServer</summary>
|
||||
/// <param name="ipAddress">IP address to bind to</param>
|
||||
/// <param name="portNumber">Port number to listen on</param>
|
||||
@@ -71,6 +93,16 @@ namespace Crestron.SimplSharp.CrestronSockets
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Starts listening for UDP packets on specified hostname and port</summary>
|
||||
/// <param name="hostname">Hostname to bind to</param>
|
||||
/// <param name="port">Port number to listen on</param>
|
||||
/// <returns>SocketErrorCodes indicating success or failure</returns>
|
||||
public SocketErrorCodes EnableUDPServer(string hostname, int port)
|
||||
{
|
||||
PortNumber = port;
|
||||
return EnableUDPServer();
|
||||
}
|
||||
|
||||
/// <summary>Stops listening for UDP packets</summary>
|
||||
/// <returns>SocketErrorCodes indicating success or failure</returns>
|
||||
public SocketErrorCodes DisableUDPServer()
|
||||
@@ -116,6 +148,34 @@ namespace Crestron.SimplSharp.CrestronSockets
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Sends data to the last received endpoint</summary>
|
||||
/// <param name="data">Data to send</param>
|
||||
/// <param name="dataLength">Length of data</param>
|
||||
/// <returns>SocketErrorCodes indicating success or failure</returns>
|
||||
public SocketErrorCodes SendData(byte[] data, int dataLength)
|
||||
{
|
||||
return SendData(data, dataLength, IPAddressLastMessageReceivedFrom, IPPortLastMessageReceivedFrom);
|
||||
}
|
||||
|
||||
/// <summary>Receives data asynchronously</summary>
|
||||
/// <param name="callback">Callback to invoke when data is received</param>
|
||||
/// <returns>SocketErrorCodes indicating success or failure</returns>
|
||||
public SocketErrorCodes ReceiveDataAsync(UDPServerReceiveDataEventHandler callback)
|
||||
{
|
||||
ReceivedData += callback;
|
||||
return SocketErrorCodes.SOCKET_OK;
|
||||
}
|
||||
|
||||
/// <summary>Receives data asynchronously with simple callback</summary>
|
||||
/// <param name="callback">Simple callback to invoke when data is received</param>
|
||||
/// <returns>SocketErrorCodes indicating success or failure</returns>
|
||||
public SocketErrorCodes ReceiveDataAsync(UDPServerReceiveDataSimpleEventHandler callback)
|
||||
{
|
||||
// Convert simple callback to full event handler and subscribe
|
||||
ReceivedData += (server, args) => callback(server, args.DataLength);
|
||||
return SocketErrorCodes.SOCKET_OK;
|
||||
}
|
||||
|
||||
/// <summary>Sends data to a specific endpoint</summary>
|
||||
/// <param name="data">Data to send</param>
|
||||
/// <param name="dataLength">Length of data</param>
|
||||
@@ -175,47 +235,9 @@ namespace Crestron.SimplSharp.CrestronSockets
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Mock SecureTCPClient class for secure TCP client operations</summary>
|
||||
public class SecureTCPClient : TCPClient
|
||||
{
|
||||
/// <summary>Initializes a new instance of SecureTCPClient</summary>
|
||||
/// <param name="ipAddress">Server IP address</param>
|
||||
/// <param name="portNumber">Server port number</param>
|
||||
/// <param name="bufferSize">Buffer size for data reception</param>
|
||||
/// <param name="ethernetAdapterToBindTo">Ethernet adapter to bind to</param>
|
||||
public SecureTCPClient(string ipAddress, int portNumber, int bufferSize, EthernetAdapterType ethernetAdapterToBindTo)
|
||||
: base(ipAddress, portNumber, bufferSize, ethernetAdapterToBindTo)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>Initializes a new instance of SecureTCPClient</summary>
|
||||
/// <param name="ipAddress">Server IP address</param>
|
||||
/// <param name="portNumber">Server port number</param>
|
||||
/// <param name="bufferSize">Buffer size for data reception</param>
|
||||
public SecureTCPClient(string ipAddress, int portNumber, int bufferSize)
|
||||
: base(ipAddress, portNumber, bufferSize)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>Sets the SSL/TLS settings (mock implementation)</summary>
|
||||
/// <param name="context">SSL context</param>
|
||||
public void SetSSLContext(object context)
|
||||
{
|
||||
// Mock implementation - does nothing in test environment
|
||||
}
|
||||
|
||||
/// <summary>Validates server certificate (mock implementation)</summary>
|
||||
/// <param name="certificate">Server certificate</param>
|
||||
/// <returns>Always returns true in mock implementation</returns>
|
||||
public bool ValidateServerCertificate(object certificate)
|
||||
{
|
||||
// Mock implementation - always accept certificate
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
// Event handler delegates for UDP
|
||||
public delegate void UDPServerReceiveDataEventHandler(UDPServer server, UDPServerReceiveDataEventArgs args);
|
||||
public delegate void UDPServerReceiveDataSimpleEventHandler(UDPServer server, int numBytes);
|
||||
|
||||
// Event argument classes for UDP
|
||||
public class UDPServerReceiveDataEventArgs : EventArgs
|
||||
|
||||
45
src/CrestronMock/UrlParserTypes.cs
Normal file
45
src/CrestronMock/UrlParserTypes.cs
Normal file
@@ -0,0 +1,45 @@
|
||||
using System;
|
||||
|
||||
namespace Crestron.SimplSharp.Net.Http
|
||||
{
|
||||
/// <summary>Mock UrlParser for HTTP</summary>
|
||||
public static class UrlParser
|
||||
{
|
||||
/// <summary>Parse a URL string</summary>
|
||||
/// <param name="url">URL to parse</param>
|
||||
/// <returns>Parsed URL components</returns>
|
||||
public static UrlParserResult Parse(string url)
|
||||
{
|
||||
return new UrlParserResult { Url = url };
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>URL parser result</summary>
|
||||
public class UrlParserResult
|
||||
{
|
||||
/// <summary>Original URL</summary>
|
||||
public string Url { get; set; } = string.Empty;
|
||||
}
|
||||
}
|
||||
|
||||
namespace Crestron.SimplSharp.Net.Https
|
||||
{
|
||||
/// <summary>Mock UrlParser for HTTPS - different from HTTP version</summary>
|
||||
public static class UrlParser
|
||||
{
|
||||
/// <summary>Parse a URL string</summary>
|
||||
/// <param name="url">URL to parse</param>
|
||||
/// <returns>Parsed URL components</returns>
|
||||
public static UrlParserResult Parse(string url)
|
||||
{
|
||||
return new UrlParserResult { Url = url };
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>HTTPS URL parser result</summary>
|
||||
public class UrlParserResult
|
||||
{
|
||||
/// <summary>Original URL</summary>
|
||||
public string Url { get; set; } = string.Empty;
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,5 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Crestron.SimplSharp.WebScripting;
|
||||
|
||||
namespace Crestron.SimplSharp.Net.Http
|
||||
{
|
||||
@@ -24,33 +23,48 @@ namespace Crestron.SimplSharp.Net.Http
|
||||
}
|
||||
|
||||
/// <summary>Mock HTTP client</summary>
|
||||
public static class HttpClient
|
||||
public class HttpClient
|
||||
{
|
||||
/// <summary>Gets or sets the keep-alive setting</summary>
|
||||
public bool KeepAlive { get; set; } = false;
|
||||
|
||||
/// <summary>Gets or sets the port number</summary>
|
||||
public int Port { get; set; } = 80;
|
||||
|
||||
/// <summary>Dispatch HTTP request</summary>
|
||||
/// <param name="request">HTTP request</param>
|
||||
/// <param name="callback">Callback for response</param>
|
||||
public static void Dispatch(HttpClientRequest request, Action<HttpClientResponse> callback)
|
||||
public void Dispatch(HttpClientRequest request, Action<HttpClientResponse> callback)
|
||||
{
|
||||
// Mock implementation - invoke callback with empty response
|
||||
var response = new HttpClientResponse();
|
||||
callback?.Invoke(response);
|
||||
}
|
||||
|
||||
/// <summary>Dispatches HTTP request synchronously</summary>
|
||||
/// <param name="request">HTTP request</param>
|
||||
/// <returns>HTTP response</returns>
|
||||
public HttpClientResponse Dispatch(HttpClientRequest request)
|
||||
{
|
||||
// Mock implementation - return empty response
|
||||
return new HttpClientResponse();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Mock HTTP client request</summary>
|
||||
public class HttpClientRequest
|
||||
{
|
||||
/// <summary>Gets or sets the URL</summary>
|
||||
public string Url { get; set; } = string.Empty;
|
||||
/// <summary>Gets or sets the URL parser</summary>
|
||||
public Crestron.SimplSharp.Net.Http.UrlParserResult Url { get; set; } = new Crestron.SimplSharp.Net.Http.UrlParserResult();
|
||||
|
||||
/// <summary>Gets or sets the HTTP method</summary>
|
||||
public string RequestType { get; set; } = "GET";
|
||||
public RequestType RequestType { get; set; } = RequestType.Get;
|
||||
|
||||
/// <summary>Gets or sets the content data</summary>
|
||||
public string ContentString { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>Gets the headers collection</summary>
|
||||
public Dictionary<string, string> Header { get; } = new Dictionary<string, string>();
|
||||
public HttpHeaderCollection Header { get; } = new HttpHeaderCollection();
|
||||
}
|
||||
|
||||
/// <summary>Mock HTTP client response</summary>
|
||||
@@ -66,8 +80,39 @@ namespace Crestron.SimplSharp.Net.Http
|
||||
public byte[] ContentBytes { get; set; } = Array.Empty<byte>();
|
||||
|
||||
/// <summary>Gets the headers collection</summary>
|
||||
public Dictionary<string, string> Header { get; } = new Dictionary<string, string>();
|
||||
public HttpHeaderCollection Header { get; } = new HttpHeaderCollection();
|
||||
}
|
||||
|
||||
/// <summary>Mock HTTP header collection</summary>
|
||||
public class HttpHeaderCollection
|
||||
{
|
||||
private readonly Dictionary<string, string> _headers = new Dictionary<string, string>();
|
||||
|
||||
/// <summary>Gets or sets the content type</summary>
|
||||
public string ContentType
|
||||
{
|
||||
get => _headers.TryGetValue("Content-Type", out var value) ? value : string.Empty;
|
||||
set => _headers["Content-Type"] = value;
|
||||
}
|
||||
|
||||
/// <summary>Sets a header value</summary>
|
||||
/// <param name="name">Header name</param>
|
||||
/// <param name="value">Header value</param>
|
||||
public void SetHeaderValue(string name, string value)
|
||||
{
|
||||
_headers[name] = value;
|
||||
}
|
||||
|
||||
/// <summary>Gets a header value</summary>
|
||||
/// <param name="name">Header name</param>
|
||||
/// <returns>Header value or empty string if not found</returns>
|
||||
public string GetHeaderValue(string name)
|
||||
{
|
||||
return _headers.TryGetValue(name, out var value) ? value : string.Empty;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
namespace Crestron.SimplSharp.Net.Https
|
||||
@@ -92,33 +137,51 @@ namespace Crestron.SimplSharp.Net.Https
|
||||
}
|
||||
|
||||
/// <summary>Mock HTTPS client</summary>
|
||||
public static class HttpsClient
|
||||
public class HttpsClient
|
||||
{
|
||||
/// <summary>Gets or sets the keep-alive setting</summary>
|
||||
public bool KeepAlive { get; set; } = false;
|
||||
|
||||
/// <summary>Gets or sets the host verification setting</summary>
|
||||
public bool HostVerification { get; set; } = false;
|
||||
|
||||
/// <summary>Gets or sets the peer verification setting</summary>
|
||||
public bool PeerVerification { get; set; } = false;
|
||||
|
||||
/// <summary>Dispatch HTTPS request</summary>
|
||||
/// <param name="request">HTTPS request</param>
|
||||
/// <param name="callback">Callback for response</param>
|
||||
public static void Dispatch(HttpsClientRequest request, Action<HttpsClientResponse> callback)
|
||||
public void Dispatch(HttpsClientRequest request, Action<HttpsClientResponse> callback)
|
||||
{
|
||||
// Mock implementation - invoke callback with empty response
|
||||
var response = new HttpsClientResponse();
|
||||
callback?.Invoke(response);
|
||||
}
|
||||
|
||||
/// <summary>Dispatches HTTPS request synchronously</summary>
|
||||
/// <param name="request">HTTPS request</param>
|
||||
/// <returns>HTTPS response</returns>
|
||||
public HttpsClientResponse Dispatch(HttpsClientRequest request)
|
||||
{
|
||||
// Mock implementation - return empty response
|
||||
return new HttpsClientResponse();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Mock HTTPS client request</summary>
|
||||
public class HttpsClientRequest
|
||||
{
|
||||
/// <summary>Gets or sets the URL</summary>
|
||||
public string Url { get; set; } = string.Empty;
|
||||
/// <summary>Gets or sets the URL parser</summary>
|
||||
public Crestron.SimplSharp.Net.Https.UrlParserResult Url { get; set; } = new Crestron.SimplSharp.Net.Https.UrlParserResult();
|
||||
|
||||
/// <summary>Gets or sets the HTTP method</summary>
|
||||
public string RequestType { get; set; } = "GET";
|
||||
public RequestType RequestType { get; set; } = RequestType.Get;
|
||||
|
||||
/// <summary>Gets or sets the content data</summary>
|
||||
public string ContentString { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>Gets the headers collection</summary>
|
||||
public Dictionary<string, string> Header { get; } = new Dictionary<string, string>();
|
||||
public HttpsHeaderCollection Header { get; } = new HttpsHeaderCollection();
|
||||
}
|
||||
|
||||
/// <summary>Mock HTTPS client response</summary>
|
||||
@@ -134,90 +197,42 @@ namespace Crestron.SimplSharp.Net.Https
|
||||
public byte[] ContentBytes { get; set; } = Array.Empty<byte>();
|
||||
|
||||
/// <summary>Gets the headers collection</summary>
|
||||
public Dictionary<string, string> Header { get; } = new Dictionary<string, string>();
|
||||
}
|
||||
}
|
||||
|
||||
namespace Crestron.SimplSharp.CrestronDataStore
|
||||
{
|
||||
/// <summary>Mock Crestron data store</summary>
|
||||
public static class CrestronDataStore
|
||||
{
|
||||
/// <summary>Mock data store interface</summary>
|
||||
public interface IDataStore
|
||||
{
|
||||
/// <summary>Sets a value</summary>
|
||||
/// <param name="key">Key</param>
|
||||
/// <param name="value">Value</param>
|
||||
void SetValue(string key, string value);
|
||||
|
||||
/// <summary>Gets a value</summary>
|
||||
/// <param name="key">Key</param>
|
||||
/// <returns>Value or null if not found</returns>
|
||||
string? GetValue(string key);
|
||||
}
|
||||
|
||||
/// <summary>Gets the global data store</summary>
|
||||
/// <returns>Mock data store instance</returns>
|
||||
public static IDataStore GetGlobalDataStore()
|
||||
{
|
||||
return new MockDataStore();
|
||||
}
|
||||
|
||||
private class MockDataStore : IDataStore
|
||||
{
|
||||
private readonly Dictionary<string, string> _data = new Dictionary<string, string>();
|
||||
|
||||
public void SetValue(string key, string value)
|
||||
{
|
||||
_data[key] = value;
|
||||
}
|
||||
|
||||
public string? GetValue(string key)
|
||||
{
|
||||
return _data.TryGetValue(key, out var value) ? value : null;
|
||||
}
|
||||
}
|
||||
public HttpsHeaderCollection Header { get; } = new HttpsHeaderCollection();
|
||||
}
|
||||
|
||||
/// <summary>Mock HTTPS client request for data store namespace</summary>
|
||||
public class HttpsClientRequest
|
||||
/// <summary>Mock HTTPS header collection</summary>
|
||||
public class HttpsHeaderCollection
|
||||
{
|
||||
/// <summary>Gets or sets the request URL</summary>
|
||||
public string Url { get; set; } = string.Empty;
|
||||
private readonly Dictionary<string, string> _headers = new Dictionary<string, string>();
|
||||
|
||||
/// <summary>Gets or sets the HTTP method</summary>
|
||||
public string Method { get; set; } = "GET";
|
||||
|
||||
/// <summary>Gets or sets the request headers</summary>
|
||||
public HttpsHeaderCollection Headers { get; set; }
|
||||
|
||||
/// <summary>Gets or sets the request content</summary>
|
||||
public string Content { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>Initializes a new instance of HttpsClientRequest</summary>
|
||||
public HttpsClientRequest()
|
||||
/// <summary>Gets or sets the content type</summary>
|
||||
public string ContentType
|
||||
{
|
||||
Headers = new HttpsHeaderCollection();
|
||||
get => _headers.TryGetValue("Content-Type", out var value) ? value : string.Empty;
|
||||
set => _headers["Content-Type"] = value;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Mock HTTPS client response for data store namespace</summary>
|
||||
public class HttpsClientResponse
|
||||
{
|
||||
/// <summary>Gets or sets the response status code</summary>
|
||||
public int StatusCode { get; set; } = 200;
|
||||
|
||||
/// <summary>Gets or sets the response content</summary>
|
||||
public string Content { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>Gets or sets the response headers</summary>
|
||||
public HttpsHeaderCollection Headers { get; set; }
|
||||
|
||||
/// <summary>Initializes a new instance of HttpsClientResponse</summary>
|
||||
public HttpsClientResponse()
|
||||
/// <summary>Sets a header value</summary>
|
||||
/// <param name="name">Header name</param>
|
||||
/// <param name="value">Header value</param>
|
||||
public void SetHeaderValue(string name, string value)
|
||||
{
|
||||
Headers = new HttpsHeaderCollection();
|
||||
_headers[name] = value;
|
||||
}
|
||||
|
||||
/// <summary>Adds a header</summary>
|
||||
/// <param name="header">Header to add</param>
|
||||
public void AddHeader(HttpsHeader header)
|
||||
{
|
||||
_headers[header.Name] = header.Value;
|
||||
}
|
||||
|
||||
/// <summary>Gets a header value</summary>
|
||||
/// <param name="name">Header name</param>
|
||||
/// <returns>Header value or empty string if not found</returns>
|
||||
public string GetHeaderValue(string name)
|
||||
{
|
||||
return _headers.TryGetValue(name, out var value) ? value : string.Empty;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -235,78 +250,28 @@ namespace Crestron.SimplSharp.CrestronDataStore
|
||||
/// <param name="value">Header value</param>
|
||||
public HttpsHeader(string name, string value)
|
||||
{
|
||||
Name = name;
|
||||
Value = value;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Mock HTTPS header collection</summary>
|
||||
public class HttpsHeaderCollection
|
||||
{
|
||||
private readonly List<HttpsHeader> _headers = new List<HttpsHeader>();
|
||||
|
||||
/// <summary>Adds a header to the collection</summary>
|
||||
/// <param name="header">Header to add</param>
|
||||
public void AddHeader(HttpsHeader header)
|
||||
{
|
||||
_headers.Add(header);
|
||||
}
|
||||
|
||||
/// <summary>Gets all headers</summary>
|
||||
/// <returns>Array of headers</returns>
|
||||
public HttpsHeader[] GetHeaders()
|
||||
{
|
||||
return _headers.ToArray();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Mock HTTPS client for data store namespace</summary>
|
||||
public class HttpsClient
|
||||
{
|
||||
/// <summary>Dispatch HTTPS request</summary>
|
||||
/// <param name="request">HTTPS request</param>
|
||||
/// <param name="callback">Callback for response</param>
|
||||
public void Dispatch(HttpsClientRequest request, Action<HttpsClientResponse> callback)
|
||||
{
|
||||
// Mock implementation - invoke callback with empty response
|
||||
var response = new HttpsClientResponse();
|
||||
callback?.Invoke(response);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Mock URL parser</summary>
|
||||
public class UrlParser
|
||||
{
|
||||
/// <summary>Gets the parsed URL</summary>
|
||||
public string Url { get; private set; }
|
||||
|
||||
/// <summary>Initializes a new instance of UrlParser</summary>
|
||||
/// <param name="url">URL to parse</param>
|
||||
public UrlParser(string url)
|
||||
{
|
||||
Url = url;
|
||||
}
|
||||
|
||||
/// <summary>Implicit conversion to string</summary>
|
||||
/// <param name="parser">URL parser</param>
|
||||
public static implicit operator string(UrlParser parser)
|
||||
{
|
||||
return parser.Url;
|
||||
Name = name ?? string.Empty;
|
||||
Value = value ?? string.Empty;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Mock HTTP exception</summary>
|
||||
public class HttpException : Exception
|
||||
{
|
||||
/// <summary>Gets the HTTP response</summary>
|
||||
public HttpsClientResponse Response { get; }
|
||||
|
||||
/// <summary>Initializes a new instance of HttpException</summary>
|
||||
public HttpException() : base()
|
||||
{
|
||||
Response = new HttpsClientResponse();
|
||||
}
|
||||
|
||||
/// <summary>Initializes a new instance of HttpException</summary>
|
||||
/// <param name="message">Exception message</param>
|
||||
public HttpException(string message) : base(message)
|
||||
{
|
||||
Response = new HttpsClientResponse();
|
||||
}
|
||||
|
||||
/// <summary>Initializes a new instance of HttpException</summary>
|
||||
@@ -314,6 +279,17 @@ namespace Crestron.SimplSharp.CrestronDataStore
|
||||
/// <param name="innerException">Inner exception</param>
|
||||
public HttpException(string message, Exception innerException) : base(message, innerException)
|
||||
{
|
||||
Response = new HttpsClientResponse();
|
||||
}
|
||||
|
||||
/// <summary>Initializes a new instance of HttpException</summary>
|
||||
/// <param name="message">Exception message</param>
|
||||
/// <param name="response">HTTP response</param>
|
||||
public HttpException(string message, HttpsClientResponse response) : base(message)
|
||||
{
|
||||
Response = response ?? new HttpsClientResponse();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -1,364 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Crestron.SimplSharp.WebScripting;
|
||||
|
||||
namespace Crestron.SimplSharp.Net.Http
|
||||
{
|
||||
/// <summary>HTTP request types</summary>
|
||||
public enum RequestType
|
||||
{
|
||||
/// <summary>GET request</summary>
|
||||
Get = 0,
|
||||
/// <summary>POST request</summary>
|
||||
Post = 1,
|
||||
/// <summary>PUT request</summary>
|
||||
Put = 2,
|
||||
/// <summary>DELETE request</summary>
|
||||
Delete = 3,
|
||||
/// <summary>HEAD request</summary>
|
||||
Head = 4,
|
||||
/// <summary>OPTIONS request</summary>
|
||||
Options = 5,
|
||||
/// <summary>PATCH request</summary>
|
||||
Patch = 6
|
||||
}
|
||||
|
||||
/// <summary>Mock HTTP client</summary>
|
||||
public static class HttpClient
|
||||
{
|
||||
/// <summary>Dispatch HTTP request</summary>
|
||||
/// <param name="request">HTTP request</param>
|
||||
/// <param name="callback">Callback for response</param>
|
||||
public static void Dispatch(HttpClientRequest request, Action<HttpClientResponse> callback)
|
||||
{
|
||||
// Mock implementation - invoke callback with empty response
|
||||
var response = new HttpClientResponse();
|
||||
callback?.Invoke(response);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Mock HTTP client request</summary>
|
||||
public class HttpClientRequest
|
||||
{
|
||||
/// <summary>Gets or sets the URL</summary>
|
||||
public string Url { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>Gets or sets the HTTP method</summary>
|
||||
public string RequestType { get; set; } = "GET";
|
||||
|
||||
/// <summary>Gets or sets the content data</summary>
|
||||
public string ContentString { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>Gets the headers collection</summary>
|
||||
public Dictionary<string, string> Header { get; } = new Dictionary<string, string>();
|
||||
}
|
||||
|
||||
/// <summary>Mock HTTP client response</summary>
|
||||
public class HttpClientResponse
|
||||
{
|
||||
/// <summary>Gets the response code</summary>
|
||||
public int Code { get; set; } = 200;
|
||||
|
||||
/// <summary>Gets the response content</summary>
|
||||
public string ContentString { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>Gets the response data as bytes</summary>
|
||||
public byte[] ContentBytes { get; set; } = Array.Empty<byte>();
|
||||
|
||||
/// <summary>Gets the headers collection</summary>
|
||||
public Dictionary<string, string> Header { get; } = new Dictionary<string, string>();
|
||||
}
|
||||
}
|
||||
|
||||
namespace Crestron.SimplSharp.Net.Https
|
||||
{
|
||||
/// <summary>HTTPS request types</summary>
|
||||
public enum RequestType
|
||||
{
|
||||
/// <summary>GET request</summary>
|
||||
Get = 0,
|
||||
/// <summary>POST request</summary>
|
||||
Post = 1,
|
||||
/// <summary>PUT request</summary>
|
||||
Put = 2,
|
||||
/// <summary>DELETE request</summary>
|
||||
Delete = 3,
|
||||
/// <summary>HEAD request</summary>
|
||||
Head = 4,
|
||||
/// <summary>OPTIONS request</summary>
|
||||
Options = 5,
|
||||
/// <summary>PATCH request</summary>
|
||||
Patch = 6
|
||||
}
|
||||
|
||||
/// <summary>Mock HTTPS client</summary>
|
||||
public static class HttpsClient
|
||||
{
|
||||
/// <summary>Dispatch HTTPS request</summary>
|
||||
/// <param name="request">HTTPS request</param>
|
||||
/// <param name="callback">Callback for response</param>
|
||||
public static void Dispatch(HttpsClientRequest request, Action<HttpsClientResponse> callback)
|
||||
{
|
||||
// Mock implementation - invoke callback with empty response
|
||||
var response = new HttpsClientResponse();
|
||||
callback?.Invoke(response);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Mock HTTPS client request</summary>
|
||||
public class HttpsClientRequest
|
||||
{
|
||||
/// <summary>Gets or sets the URL</summary>
|
||||
public string Url { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>Gets or sets the HTTP method</summary>
|
||||
public string RequestType { get; set; } = "GET";
|
||||
|
||||
/// <summary>Gets or sets the content data</summary>
|
||||
public string ContentString { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>Gets the headers collection</summary>
|
||||
public Dictionary<string, string> Header { get; } = new Dictionary<string, string>();
|
||||
}
|
||||
|
||||
/// <summary>Mock HTTPS client response</summary>
|
||||
public class HttpsClientResponse
|
||||
{
|
||||
/// <summary>Gets the response code</summary>
|
||||
public int Code { get; set; } = 200;
|
||||
|
||||
/// <summary>Gets the response content</summary>
|
||||
public string ContentString { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>Gets the response data as bytes</summary>
|
||||
public byte[] ContentBytes { get; set; } = Array.Empty<byte>();
|
||||
|
||||
/// <summary>Gets the headers collection</summary>
|
||||
public Dictionary<string, string> Header { get; } = new Dictionary<string, string>();
|
||||
}
|
||||
}
|
||||
|
||||
namespace Crestron.SimplSharp.CrestronLogger
|
||||
{
|
||||
/// <summary>Mock Crestron logger</summary>
|
||||
public static class CrestronLogger
|
||||
{
|
||||
/// <summary>Mock log levels</summary>
|
||||
public enum LogLevel
|
||||
{
|
||||
/// <summary>Debug level</summary>
|
||||
Debug = 0,
|
||||
/// <summary>Info level</summary>
|
||||
Info = 1,
|
||||
/// <summary>Warning level</summary>
|
||||
Warning = 2,
|
||||
/// <summary>Error level</summary>
|
||||
Error = 3
|
||||
}
|
||||
|
||||
/// <summary>Mock logger interface</summary>
|
||||
public interface ILogger
|
||||
{
|
||||
/// <summary>Logs a message</summary>
|
||||
/// <param name="level">Log level</param>
|
||||
/// <param name="message">Message to log</param>
|
||||
void Log(LogLevel level, string message);
|
||||
}
|
||||
|
||||
/// <summary>Gets a logger by name</summary>
|
||||
/// <param name="name">Logger name</param>
|
||||
/// <returns>Mock logger instance</returns>
|
||||
public static ILogger GetLogger(string name)
|
||||
{
|
||||
return new MockLogger();
|
||||
}
|
||||
|
||||
private class MockLogger : ILogger
|
||||
{
|
||||
public void Log(LogLevel level, string message)
|
||||
{
|
||||
// Mock implementation - do nothing in test environment
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
namespace Crestron.SimplSharp.CrestronDataStore
|
||||
{
|
||||
/// <summary>Mock Crestron data store</summary>
|
||||
public static class CrestronDataStore
|
||||
{
|
||||
/// <summary>Mock data store interface</summary>
|
||||
public interface IDataStore
|
||||
{
|
||||
/// <summary>Sets a value</summary>
|
||||
/// <param name="key">Key</param>
|
||||
/// <param name="value">Value</param>
|
||||
void SetValue(string key, string value);
|
||||
|
||||
/// <summary>Gets a value</summary>
|
||||
/// <param name="key">Key</param>
|
||||
/// <returns>Value or null if not found</returns>
|
||||
string? GetValue(string key);
|
||||
}
|
||||
|
||||
/// <summary>Gets the global data store</summary>
|
||||
/// <returns>Mock data store instance</returns>
|
||||
public static IDataStore GetGlobalDataStore()
|
||||
{
|
||||
return new MockDataStore();
|
||||
}
|
||||
|
||||
private class MockDataStore : IDataStore
|
||||
{
|
||||
private readonly Dictionary<string, string> _data = new Dictionary<string, string>();
|
||||
|
||||
public void SetValue(string key, string value)
|
||||
{
|
||||
_data[key] = value;
|
||||
}
|
||||
|
||||
public string? GetValue(string key)
|
||||
{
|
||||
return _data.TryGetValue(key, out var value) ? value : null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Mock HTTPS client request for data store namespace</summary>
|
||||
public class HttpsClientRequest
|
||||
{
|
||||
/// <summary>Gets or sets the request URL</summary>
|
||||
public string Url { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>Gets or sets the HTTP method</summary>
|
||||
public string Method { get; set; } = "GET";
|
||||
|
||||
/// <summary>Gets or sets the request headers</summary>
|
||||
public HttpsHeaderCollection Headers { get; set; }
|
||||
|
||||
/// <summary>Gets or sets the request content</summary>
|
||||
public string Content { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>Initializes a new instance of HttpsClientRequest</summary>
|
||||
public HttpsClientRequest()
|
||||
{
|
||||
Headers = new HttpsHeaderCollection();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Mock HTTPS client response for data store namespace</summary>
|
||||
public class HttpsClientResponse
|
||||
{
|
||||
/// <summary>Gets or sets the response status code</summary>
|
||||
public int StatusCode { get; set; } = 200;
|
||||
|
||||
/// <summary>Gets or sets the response content</summary>
|
||||
public string Content { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>Gets or sets the response headers</summary>
|
||||
public HttpsHeaderCollection Headers { get; set; }
|
||||
|
||||
/// <summary>Initializes a new instance of HttpsClientResponse</summary>
|
||||
public HttpsClientResponse()
|
||||
{
|
||||
Headers = new HttpsHeaderCollection();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Mock HTTPS header</summary>
|
||||
public class HttpsHeader
|
||||
{
|
||||
/// <summary>Gets the header name</summary>
|
||||
public string Name { get; private set; }
|
||||
|
||||
/// <summary>Gets the header value</summary>
|
||||
public string Value { get; private set; }
|
||||
|
||||
/// <summary>Initializes a new instance of HttpsHeader</summary>
|
||||
/// <param name="name">Header name</param>
|
||||
/// <param name="value">Header value</param>
|
||||
public HttpsHeader(string name, string value)
|
||||
{
|
||||
Name = name;
|
||||
Value = value;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Mock HTTPS header collection</summary>
|
||||
public class HttpsHeaderCollection
|
||||
{
|
||||
private readonly List<HttpsHeader> _headers = new List<HttpsHeader>();
|
||||
|
||||
/// <summary>Adds a header to the collection</summary>
|
||||
/// <param name="header">Header to add</param>
|
||||
public void AddHeader(HttpsHeader header)
|
||||
{
|
||||
_headers.Add(header);
|
||||
}
|
||||
|
||||
/// <summary>Gets all headers</summary>
|
||||
/// <returns>Array of headers</returns>
|
||||
public HttpsHeader[] GetHeaders()
|
||||
{
|
||||
return _headers.ToArray();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Mock HTTPS client for data store namespace</summary>
|
||||
public class HttpsClient
|
||||
{
|
||||
/// <summary>Dispatch HTTPS request</summary>
|
||||
/// <param name="request">HTTPS request</param>
|
||||
/// <param name="callback">Callback for response</param>
|
||||
public void Dispatch(HttpsClientRequest request, Action<HttpsClientResponse> callback)
|
||||
{
|
||||
// Mock implementation - invoke callback with empty response
|
||||
var response = new HttpsClientResponse();
|
||||
callback?.Invoke(response);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Mock URL parser</summary>
|
||||
public class UrlParser
|
||||
{
|
||||
/// <summary>Gets the parsed URL</summary>
|
||||
public string Url { get; private set; }
|
||||
|
||||
/// <summary>Initializes a new instance of UrlParser</summary>
|
||||
/// <param name="url">URL to parse</param>
|
||||
public UrlParser(string url)
|
||||
{
|
||||
Url = url;
|
||||
}
|
||||
|
||||
/// <summary>Implicit conversion to string</summary>
|
||||
/// <param name="parser">URL parser</param>
|
||||
public static implicit operator string(UrlParser parser)
|
||||
{
|
||||
return parser.Url;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Mock HTTP exception</summary>
|
||||
public class HttpException : Exception
|
||||
{
|
||||
/// <summary>Initializes a new instance of HttpException</summary>
|
||||
public HttpException() : base()
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>Initializes a new instance of HttpException</summary>
|
||||
/// <param name="message">Exception message</param>
|
||||
public HttpException(string message) : base(message)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>Initializes a new instance of HttpException</summary>
|
||||
/// <param name="message">Exception message</param>
|
||||
/// <param name="innerException">Inner exception</param>
|
||||
public HttpException(string message, Exception innerException) : base(message, innerException)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user