Files
csharp/tests/KubernetesClient.Tests/Mock/Server/Controllers/PodPortForwardController.cs
Boshi Lian 16845bae1d Style fix1 (#512)
* fix SA1505 and SA1508

* fix SA1116

* fix SA1009

* fix SA1019

* fix SA1127

* fix SA1128

* fix SA1134

* fix indent

* allow CA2227

* fix CA1810

* using clean up

* fix naming

* fix CA1806

* fix await

* Revert "fix CA1806"

This reverts commit a3b465087fdaf26ec461272373ee9810a90de2cc.

* fix dotnet format

* allow SA1009
2020-11-01 12:24:51 -08:00

68 lines
2.3 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>
/// 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");
}
WebSocket webSocket = await HttpContext.WebSockets.AcceptWebSocketAsync(
subProtocol: WebSocketProtocol.ChannelWebSocketProtocol).ConfigureAwait(false);
WebSocketTestAdapter.AcceptedPodPortForwardV1Connection.AcceptServerSocket(webSocket);
await WebSocketTestAdapter.TestCompleted.ConfigureAwait(false);
return Ok();
}
}
}