|
| 1 | +using System.Net; |
| 2 | +using System.Net.Http; |
| 3 | +using System.Net.Sockets; |
| 4 | +using Altinn.AccessManagement.UI.Integration.Clients; |
| 5 | +using Altinn.AccessManagement.UI.Integration.Configuration; |
| 6 | +using Altinn.Authorization.ProblemDetails; |
| 7 | +using Microsoft.AspNetCore.Http; |
| 8 | +using Microsoft.Extensions.Logging.Abstractions; |
| 9 | +using Microsoft.Extensions.Options; |
| 10 | + |
| 11 | +namespace Altinn.AccessManagement.UI.Tests.Clients |
| 12 | +{ |
| 13 | + /// <summary> |
| 14 | + /// Tests that socket errors towards the authentication API surface as a retryable |
| 15 | + /// 503 problem instead of bubbling up as an unhandled exception (500). |
| 16 | + /// </summary> |
| 17 | + public class SystemUserRequestClientTest |
| 18 | + { |
| 19 | + private const string BaseUrl = "http://localhost:5117/authentication/api/v1/"; |
| 20 | + |
| 21 | + private static readonly Guid _requestId = Guid.Parse("55555555-5555-5555-5555-555555555555"); |
| 22 | + |
| 23 | + /// <summary> |
| 24 | + /// Handler that fails every request with the given exception. |
| 25 | + /// </summary> |
| 26 | + private sealed class ThrowingHandler(Exception exception) : HttpMessageHandler |
| 27 | + { |
| 28 | + protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) |
| 29 | + { |
| 30 | + throw exception; |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + private static SystemUserRequestClient CreateClient(Exception exception) |
| 35 | + { |
| 36 | + PlatformSettings platformSettings = new() |
| 37 | + { |
| 38 | + ApiAuthenticationEndpoint = BaseUrl, |
| 39 | + SubscriptionKeyHeaderName = "Ocp-Apim-Subscription-Key", |
| 40 | + SubscriptionKey = "subscription-key", |
| 41 | + JwtCookieName = "AltinnStudioRuntime", |
| 42 | + }; |
| 43 | + |
| 44 | + return new SystemUserRequestClient( |
| 45 | + NullLogger<SystemUserRequestClient>.Instance, |
| 46 | + new HttpClient(new ThrowingHandler(exception)), |
| 47 | + new HttpContextAccessor { HttpContext = new DefaultHttpContext() }, |
| 48 | + Options.Create(platformSettings)); |
| 49 | + } |
| 50 | + |
| 51 | + /// <summary> |
| 52 | + /// A socket error is transient, so callers must get a 503 they can retry on rather than a 500 — |
| 53 | + /// end users approving a request in the frontend, external clients calling the BFF, and e2e tests alike. |
| 54 | + /// </summary> |
| 55 | + [Fact] |
| 56 | + public async Task ApproveSystemUserRequest_SocketException_ReturnsServiceUnavailable() |
| 57 | + { |
| 58 | + // A dropped connection reaches the client as a SocketException nested inside HttpRequestException |
| 59 | + Exception socketError = new HttpRequestException( |
| 60 | + "An error occurred while sending the request.", |
| 61 | + new IOException("The response ended prematurely.", new SocketException((int)SocketError.ConnectionReset))); |
| 62 | + |
| 63 | + Result<bool> result = await CreateClient(socketError).ApproveSystemUserRequest(51329012, _requestId, CancellationToken.None); |
| 64 | + |
| 65 | + Assert.True(result.IsProblem); |
| 66 | + Assert.Equal(HttpStatusCode.ServiceUnavailable, (HttpStatusCode)result.Problem.StatusCode); |
| 67 | + Assert.Equal("AMUI-00100", result.Problem.ErrorCode.ToString()); |
| 68 | + } |
| 69 | + |
| 70 | + /// <summary> |
| 71 | + /// Only socket errors are transient. Every other failure must keep bubbling up as before, so |
| 72 | + /// genuine server-side errors are not disguised as something callers should retry. |
| 73 | + /// </summary> |
| 74 | + [Fact] |
| 75 | + public async Task ApproveSystemUserRequest_OtherException_Rethrows() |
| 76 | + { |
| 77 | + Exception otherError = new HttpRequestException("Bad gateway", new InvalidOperationException("nope")); |
| 78 | + |
| 79 | + await Assert.ThrowsAsync<HttpRequestException>( |
| 80 | + () => CreateClient(otherError).ApproveSystemUserRequest(51329012, _requestId, CancellationToken.None)); |
| 81 | + } |
| 82 | + } |
| 83 | +} |
0 commit comments