|
| 1 | +using System; |
| 2 | +using System.Net.Http; |
| 3 | +using AssemblyAI.Core; |
| 4 | +using FluentAssertions; |
| 5 | +using NUnit.Framework; |
| 6 | +using WireMock.Server; |
| 7 | +using SystemTask = System.Threading.Tasks.Task; |
| 8 | +using WireMockRequest = WireMock.RequestBuilders.Request; |
| 9 | +using WireMockResponse = WireMock.ResponseBuilders.Response; |
| 10 | + |
| 11 | +namespace AssemblyAI.Test.Core |
| 12 | +{ |
| 13 | + [TestFixture] |
| 14 | + public class RawClientTests |
| 15 | + { |
| 16 | + private WireMockServer _server; |
| 17 | + private HttpClient _httpClient; |
| 18 | + private RawClient _rawClient; |
| 19 | + private string _baseUrl; |
| 20 | + private const int _maxRetries = 3; |
| 21 | + |
| 22 | + [SetUp] |
| 23 | + public void SetUp() |
| 24 | + { |
| 25 | + _server = WireMockServer.Start(); |
| 26 | + _baseUrl = _server.Url ?? ""; |
| 27 | + _httpClient = new HttpClient { BaseAddress = new Uri(_baseUrl) }; |
| 28 | + _rawClient = new RawClient( |
| 29 | + new ClientOptions() { HttpClient = _httpClient, MaxRetries = _maxRetries } |
| 30 | + ); |
| 31 | + } |
| 32 | + |
| 33 | + [Test] |
| 34 | + [TestCase(408)] |
| 35 | + [TestCase(429)] |
| 36 | + [TestCase(500)] |
| 37 | + [TestCase(504)] |
| 38 | + public async SystemTask MakeRequestAsync_ShouldRetry_OnRetryableStatusCodes(int statusCode) |
| 39 | + { |
| 40 | + _server |
| 41 | + .Given(WireMockRequest.Create().WithPath("/test").UsingGet()) |
| 42 | + .InScenario("Retry") |
| 43 | + .WillSetStateTo("Server Error") |
| 44 | + .RespondWith(WireMockResponse.Create().WithStatusCode(statusCode)); |
| 45 | + |
| 46 | + _server |
| 47 | + .Given(WireMockRequest.Create().WithPath("/test").UsingGet()) |
| 48 | + .InScenario("Retry") |
| 49 | + .WhenStateIs("Server Error") |
| 50 | + .WillSetStateTo("Success") |
| 51 | + .RespondWith(WireMockResponse.Create().WithStatusCode(statusCode)); |
| 52 | + |
| 53 | + _server |
| 54 | + .Given(WireMockRequest.Create().WithPath("/test").UsingGet()) |
| 55 | + .InScenario("Retry") |
| 56 | + .WhenStateIs("Success") |
| 57 | + .RespondWith(WireMockResponse.Create().WithStatusCode(200).WithBody("Success")); |
| 58 | + |
| 59 | + var request = new RawClient.BaseApiRequest |
| 60 | + { |
| 61 | + BaseUrl = _baseUrl, |
| 62 | + Method = HttpMethod.Get, |
| 63 | + Path = "/test", |
| 64 | + }; |
| 65 | + |
| 66 | + var response = await _rawClient.MakeRequestAsync(request); |
| 67 | + Assert.That(response.StatusCode, Is.EqualTo(200)); |
| 68 | + |
| 69 | + var content = await response.Raw.Content.ReadAsStringAsync(); |
| 70 | + Assert.That(content, Is.EqualTo("Success")); |
| 71 | + |
| 72 | + Assert.That(_server.LogEntries.Count, Is.EqualTo(_maxRetries)); |
| 73 | + } |
| 74 | + |
| 75 | + [Test] |
| 76 | + [TestCase(400)] |
| 77 | + [TestCase(409)] |
| 78 | + public async SystemTask MakeRequestAsync_ShouldRetry_OnNonRetryableStatusCodes( |
| 79 | + int statusCode |
| 80 | + ) |
| 81 | + { |
| 82 | + _server |
| 83 | + .Given(WireMockRequest.Create().WithPath("/test").UsingGet()) |
| 84 | + .InScenario("Retry") |
| 85 | + .WillSetStateTo("Server Error") |
| 86 | + .RespondWith( |
| 87 | + WireMockResponse.Create().WithStatusCode(statusCode).WithBody("Failure") |
| 88 | + ); |
| 89 | + |
| 90 | + var request = new RawClient.BaseApiRequest |
| 91 | + { |
| 92 | + BaseUrl = _baseUrl, |
| 93 | + Method = HttpMethod.Get, |
| 94 | + Path = "/test", |
| 95 | + }; |
| 96 | + |
| 97 | + var response = await _rawClient.MakeRequestAsync(request); |
| 98 | + Assert.That(response.StatusCode, Is.EqualTo(statusCode)); |
| 99 | + |
| 100 | + var content = await response.Raw.Content.ReadAsStringAsync(); |
| 101 | + Assert.That(content, Is.EqualTo("Failure")); |
| 102 | + |
| 103 | + Assert.That(_server.LogEntries.Count, Is.EqualTo(1)); |
| 104 | + } |
| 105 | + |
| 106 | + [TearDown] |
| 107 | + public void TearDown() |
| 108 | + { |
| 109 | + _server.Dispose(); |
| 110 | + _httpClient.Dispose(); |
| 111 | + } |
| 112 | + } |
| 113 | +} |
0 commit comments