Files
csharp/tests/KubernetesClient.Tests/Mock/Server/Controllers/PodExecController.cs
Boshi Lian eca9898902 API v1.23.0 + system.text.json + remove WatchXXX API (#750)
* gen v1.23.0

* fix converter

* bump ver

* update readme runtime

* fix warning

* update dep ver

* newtonjson -> system.text.json

* generate for new json api

* readme lf

* dotnet fmt

* dotnet fmt tests/

* dotnet fmt

* Revert "dotnet fmt"

This reverts commit e14c59076143fe2218ed899295a00762f0ea2bd6.

* fix err introduce by dotnet fmt

* fix test

* remove deprecated /watch api

* generate code after /watch removed

* remove /watch related code

* trim Microsoft.Rest.Serialization
2021-12-13 07:31:59 -08:00

66 lines
2.4 KiB
C#

using Microsoft.AspNetCore.Mvc;
using System;
using System.Threading.Tasks;
namespace k8s.Tests.Mock.Server.Controllers
{
/// <summary>
/// Controller for the mock Kubernetes exec-in-pod API.
/// </summary>
[Route("api/v1")]
public class PodExecController
: Controller
{
/// <summary>
/// Initializes a new instance of the <see cref="PodExecController"/> class.
/// Create a new <see cref="PodExecController"/>.
/// </summary>
/// <param name="webSocketTestAdapter">
/// The adapter used to capture sockets accepted by the test server and provide them to the calling test.
/// </param>
public PodExecController(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: exec-in-pod.
/// </summary>
/// <param name="kubeNamespace">
/// The target pod's containing namespace.
/// </param>
/// <param name="podName">
/// The target pod's name.
/// </param>
/// <returns><placeholder>A <see cref="Task"/> representing the asynchronous operation.</placeholder></returns>
/// <returns><placeholder>A <see cref="Task"/> representing the asynchronous operation.</placeholder></returns>
[Route("namespaces/{kubeNamespace}/pods/{podName}/exec")]
public async Task<IActionResult> Exec(string kubeNamespace, string podName)
{
if (!HttpContext.WebSockets.IsWebSocketRequest)
{
return BadRequest("Exec requires WebSockets");
}
var webSocket = await HttpContext.WebSockets.AcceptWebSocketAsync(
WebSocketProtocol.ChannelWebSocketProtocol).ConfigureAwait(false);
WebSocketTestAdapter.AcceptedPodExecV1Connection.AcceptServerSocket(webSocket);
await WebSocketTestAdapter.TestCompleted.ConfigureAwait(false);
return Ok();
}
}
}