Files
csharp/tests/KubernetesClient.Tests/Mock/Server/Controllers/PodPortForwardController.cs
Boshi Lian 5be3cff425 Style fix final (#523)
* all net5

* var

* SA1310

* SA1310

* allow 1031

* SA1805

* fix SA1642

* remove unused code

* allow sa1405

* isempty

* fix CA1714

* fix CA1806

* remove always false if

* fix format

* fix CA1062

* allow SA0001

* fix CA1062

* allow ca1034 and temp allow ca1835

* fix 16XX doc related warnings

* elm SA16XX

* elm SA16XX

* fix CA2213

* revert to pass all test

* move unclear rule to ruleset

* follow up of moving ruleset

* remove this

* fix test flaky
2020-11-22 14:52:09 -08:00

69 lines
2.4 KiB
C#

using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Net.WebSockets;
using System.Threading.Tasks;
namespace k8s.Tests.Mock.Server.Controllers
{
/// <summary>
/// Controller for the mock Kubernetes pod-port-forward API.
/// </summary>
[Route("api/v1")]
public class PodPortForwardController
: Controller
{
/// <summary>
/// Initializes a new instance of the <see cref="PodPortForwardController"/> class.
/// Create a new <see cref="PodPortForwardController"/>.
/// </summary>
/// <param name="webSocketTestAdapter">
/// The adapter used to capture sockets accepted by the test server and provide them to the calling test.
/// </param>
public PodPortForwardController(WebSocketTestAdapter webSocketTestAdapter)
{
if (webSocketTestAdapter == null)
{
throw new ArgumentNullException(nameof(webSocketTestAdapter));
}
WebSocketTestAdapter = webSocketTestAdapter;
}
/// <summary>
/// The adapter used to capture sockets accepted by the test server and provide them to the calling test.
/// </summary>
private WebSocketTestAdapter WebSocketTestAdapter { get; }
/// <summary>
/// Mock Kubernetes API: port-forward for pod.
/// </summary>
/// <param name="kubeNamespace">
/// The target pod's containing namespace.
/// </param>
/// <param name="podName">
/// The target pod's name.
/// </param>
/// <param name="ports">
/// The port(s) to forward to the pod.
/// </param>
[Route("namespaces/{kubeNamespace}/pods/{podName}/portforward")]
public async Task<IActionResult> Exec(string kubeNamespace, string podName, IEnumerable<string> ports)
{
if (!HttpContext.WebSockets.IsWebSocketRequest)
{
return BadRequest("PortForward requires WebSockets");
}
var webSocket = await HttpContext.WebSockets.AcceptWebSocketAsync(
WebSocketProtocol.ChannelWebSocketProtocol).ConfigureAwait(false);
WebSocketTestAdapter.AcceptedPodPortForwardV1Connection.AcceptServerSocket(webSocket);
await WebSocketTestAdapter.TestCompleted.ConfigureAwait(false);
return Ok();
}
}
}