using System.Net; using Big.Infrastructure; namespace Big.Tests; public class AclHttpClientTests { private static AclHttpClient Client(StubHandler handler) => new( new HttpClient(handler), new AclOptions { BaseUrl = new("http://acl/") }); [Fact] public async Task Opens_a_zaak_by_posting_the_bsn_and_returns_the_zaak_url() { var capture = new RequestCapture(); var client = Client(capture.Responds(HttpStatusCode.OK, """{"zaakUrl":"http://openzaak/zaken/api/v1/zaken/abc"}""")); var url = await client.OpenZaakAsync("123456782", "reg-42"); Assert.Equal("http://openzaak/zaken/api/v1/zaken/abc", url.ToString()); Assert.Equal(HttpMethod.Post, capture.Seen!.Method); Assert.Equal("http://acl/zaken", capture.Seen.RequestUri!.ToString()); Assert.Contains("\"bsn\":\"123456782\"", capture.Body); Assert.Contains("\"reference\":\"reg-42\"", capture.Body); } [Fact] public async Task Throws_when_the_acl_rejects_the_request() { var capture = new RequestCapture(); var client = Client(capture.Responds(HttpStatusCode.BadGateway)); await Assert.ThrowsAsync(() => client.OpenZaakAsync("123456782", "reg-1")); } [Fact] public async Task Throws_when_the_acl_returns_an_empty_body() { var capture = new RequestCapture(); var client = Client(capture.Responds(HttpStatusCode.OK, "null")); var ex = await Assert.ThrowsAsync(() => client.OpenZaakAsync("123456782", "reg-1")); Assert.Contains("empty", ex.Message, StringComparison.OrdinalIgnoreCase); } [Fact] public async Task Approves_a_zaak_by_posting_its_url_to_statussen() { var capture = new RequestCapture(); var client = Client(capture.Responds(HttpStatusCode.NoContent)); await client.ApproveZaakAsync(new Uri("http://openzaak/zaken/api/v1/zaken/abc")); Assert.Equal(HttpMethod.Post, capture.Seen!.Method); Assert.Equal("http://acl/statussen", capture.Seen.RequestUri!.ToString()); Assert.Contains("\"zaakUrl\":\"http://openzaak/zaken/api/v1/zaken/abc\"", capture.Body); } [Fact] public async Task Approve_throws_when_the_acl_rejects_the_request() { var capture = new RequestCapture(); var client = Client(capture.Responds(HttpStatusCode.BadGateway)); await Assert.ThrowsAsync( () => client.ApproveZaakAsync(new Uri("http://openzaak/zaken/api/v1/zaken/abc"))); } [Fact] public async Task Approve_rejects_a_null_zaak_url_without_sending_a_request() { var capture = new RequestCapture(); var client = Client(capture.Responds(HttpStatusCode.NoContent)); await Assert.ThrowsAsync(() => client.ApproveZaakAsync(null!)); Assert.Null(capture.Seen); } }