ASP.NET Core TestServer + xUnit + IdentityServer SocketException

I hope that someone can understand the root of this problem and help me solve it or even reproduce it.

I have my own Xamarin PCL, which has some asynchronous methods exposed in its virtual machines. Virtual machines use their own subclass HttpClient, which takes HttpMessageHandlerto its constructor.

I use my own handler for proxying all requests and authorization, here is the handler:

public class LoginMessageHandler : DelegatingHandler
{
  protected override async Task<HttpResponseMessage>
    SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
  {
    if (!await EnsureLoggedInAsync())
      throw new UnauthorizedAccessException("User not logged in.");

    request.Headers.Authorization =
      new AuthenticationHeaderValue("Bearer", Principal.AccessToken);

    //fails here on debug (in more complex tests it'll fail in cold run too)
    return await base.SendAsync(request, cancellationToken);
  }
}

The method EnsureLoggedInAsynccalls IdentityServer4 TokenClientand calls UserInfoClientto load user information using the HttpMessageHandlergenerated one TestServer.CreateHandler.

/ base.SendAsync System.Net.Sockets.SocketException: " , ". - . .

-, , async , , .

:

[Fact]
public async Task RefreshRunsheetsPlainTest()
{
  var loginHandler = new TestLoginManager(testServer.CreateHandler());
  var client = new HttpClient(loginHandler)
  {
    BaseAddress = new Uri(_Services.TestServer.BaseAddress, "api"),
    Timeout = Timeout.InfiniteTimeSpan,
  };      
  var response = await client.GetAsync("runsheets");
  var result = await response.Content.ReadAsStringAsync();
  Assert.True(response.IsSuccessStatusCode, result);
}

result ( ASP.NET Core).

CreateHandler , .

, , , , , , , , .

+4

Source: https://habr.com/ru/post/1679540/


All Articles