| 
 | 1 | +using FluentHttpClient.Tests.Models;  | 
 | 2 | +using Microsoft.Extensions.Configuration;  | 
 | 3 | +using Microsoft.Extensions.DependencyInjection;  | 
 | 4 | + | 
 | 5 | +namespace FluentHttpClient.Tests;  | 
 | 6 | + | 
 | 7 | +public class ClientUnitTests : UnitTestBase  | 
 | 8 | +{  | 
 | 9 | +    private readonly HttpClient _client;  | 
 | 10 | + | 
 | 11 | +    private readonly int MaxPostId = 100;  | 
 | 12 | +    private readonly int MinPostId = 1;  | 
 | 13 | +    private readonly int MaxUserId = 10;  | 
 | 14 | +    private readonly int MinUserId = 1;  | 
 | 15 | +    private readonly int PostsPerUser = 10;  | 
 | 16 | + | 
 | 17 | +    public ClientUnitTests()  | 
 | 18 | +    {  | 
 | 19 | +        _client = GetRequiredService<HttpClient>();  | 
 | 20 | +        _client.BaseAddress = new Uri("https://jsonplaceholder.typicode.com");  | 
 | 21 | +    }  | 
 | 22 | + | 
 | 23 | +    protected override void ConfigureServices(IServiceCollection services, IConfiguration configuration)  | 
 | 24 | +    {  | 
 | 25 | +        services.AddHttpClient();  | 
 | 26 | +    }  | 
 | 27 | + | 
 | 28 | +    [Fact]  | 
 | 29 | +    public async Task OnFailureTest()  | 
 | 30 | +    {  | 
 | 31 | +        var route = $"/posts";  | 
 | 32 | +        var passed = false;  | 
 | 33 | + | 
 | 34 | +        await _client.UsingRoute(route)  | 
 | 35 | +            .DeleteAsync()  | 
 | 36 | +            .OnFailure(msg =>  | 
 | 37 | +            {  | 
 | 38 | +                passed = true;  | 
 | 39 | +            })  | 
 | 40 | +            .OnSuccess(msg =>  | 
 | 41 | +            {  | 
 | 42 | +                throw new Exception("The request returned a success status code.");  | 
 | 43 | +            });  | 
 | 44 | + | 
 | 45 | +        passed.ShouldBeTrue();  | 
 | 46 | +    }  | 
 | 47 | + | 
 | 48 | +    [Fact]  | 
 | 49 | +    public async Task OnSuccessTest()  | 
 | 50 | +    {  | 
 | 51 | +        var postId = Random.Shared.Next(MinPostId, MaxPostId);  | 
 | 52 | +        var route = $"/posts/{postId}";  | 
 | 53 | +        var passed = false;  | 
 | 54 | + | 
 | 55 | +        await _client.UsingRoute(route)  | 
 | 56 | +            .DeleteAsync()  | 
 | 57 | +            .OnFailure(msg =>  | 
 | 58 | +            {  | 
 | 59 | +                throw new Exception("The request did not return a success status code.");  | 
 | 60 | +            })  | 
 | 61 | +            .OnSuccess(msg =>  | 
 | 62 | +            {  | 
 | 63 | +                passed = true;  | 
 | 64 | +            });  | 
 | 65 | + | 
 | 66 | +        passed.ShouldBeTrue();  | 
 | 67 | +    }  | 
 | 68 | + | 
 | 69 | +    [Fact]  | 
 | 70 | +    public async Task DefaultActionTest()  | 
 | 71 | +    {  | 
 | 72 | +        var route = $"/post/1";  | 
 | 73 | +        var passed = false;  | 
 | 74 | + | 
 | 75 | +        var builder = _client.UsingRoute(route);  | 
 | 76 | + | 
 | 77 | +        var response = await builder  | 
 | 78 | +            .GetAsync();  | 
 | 79 | + | 
 | 80 | +        var post1 = await response.DeserializeJsonAsync<Post>();  | 
 | 81 | +        var post2 = await response.DeserializeJsonAsync<Post>((msg, ex) =>  | 
 | 82 | +            {  | 
 | 83 | +                passed = true;  | 
 | 84 | +                return (Post)null;  | 
 | 85 | +            });  | 
 | 86 | + | 
 | 87 | +        post1.ShouldNotBeNull();  | 
 | 88 | +        post2.ShouldBeNull();  | 
 | 89 | +        passed.ShouldBeTrue();  | 
 | 90 | +    }  | 
 | 91 | + | 
 | 92 | +    [Fact]  | 
 | 93 | +    public async Task GetAllAsyncTest()  | 
 | 94 | +    {  | 
 | 95 | +        var route = "/posts";  | 
 | 96 | + | 
 | 97 | +        var builder = _client.UsingRoute(route);  | 
 | 98 | + | 
 | 99 | +        var response = await builder.GetAsync();  | 
 | 100 | + | 
 | 101 | +        response.EnsureSuccessStatusCode();  | 
 | 102 | + | 
 | 103 | +        var posts = await response  | 
 | 104 | +            .DeserializeJsonAsync<IEnumerable<Post>>();  | 
 | 105 | + | 
 | 106 | +        posts.ShouldNotBeNull();  | 
 | 107 | +        posts.ShouldNotBeEmpty();  | 
 | 108 | +        posts.Count().ShouldBe(MaxPostId);  | 
 | 109 | +    }  | 
 | 110 | + | 
 | 111 | +    [Fact]  | 
 | 112 | +    public async Task GetPostsForUserAsyncTest()  | 
 | 113 | +    {  | 
 | 114 | +        var userId = Random.Shared.Next(MinUserId, MaxUserId);  | 
 | 115 | +        var route = $"/posts";  | 
 | 116 | + | 
 | 117 | +        var builder = _client.UsingRoute(route);  | 
 | 118 | + | 
 | 119 | +        builder.WithQueryParam("userId", userId);  | 
 | 120 | + | 
 | 121 | +        var response = await builder.GetAsync();  | 
 | 122 | + | 
 | 123 | +        response.EnsureSuccessStatusCode();  | 
 | 124 | + | 
 | 125 | +        var posts = await response  | 
 | 126 | +            .DeserializeJsonAsync<IEnumerable<Post>>();  | 
 | 127 | + | 
 | 128 | +        posts.ShouldNotBeNull();  | 
 | 129 | +        posts.ShouldNotBeEmpty();  | 
 | 130 | +        posts.Count().ShouldBe(PostsPerUser);  | 
 | 131 | +        posts.All(x => x.UserId == userId).ShouldBeTrue();  | 
 | 132 | +    }  | 
 | 133 | + | 
 | 134 | +    [Fact]  | 
 | 135 | +    public async Task GetPostByIdAsyncTest()  | 
 | 136 | +    {  | 
 | 137 | +        var postId = Random.Shared.Next(MinPostId, MaxPostId);  | 
 | 138 | +        var route = $"/posts/{postId}";  | 
 | 139 | + | 
 | 140 | +        var builder = _client.UsingRoute(route);  | 
 | 141 | + | 
 | 142 | +        var response = await builder.GetAsync();  | 
 | 143 | + | 
 | 144 | +        response.EnsureSuccessStatusCode();  | 
 | 145 | + | 
 | 146 | +        var post = await response  | 
 | 147 | +            .DeserializeJsonAsync<Post>();  | 
 | 148 | + | 
 | 149 | +        post.ShouldNotBeNull();  | 
 | 150 | +        post.Id.ShouldBe(postId);  | 
 | 151 | +    }  | 
 | 152 | + | 
 | 153 | +    [Fact]  | 
 | 154 | +    public async Task DeletePostAsync()  | 
 | 155 | +    {  | 
 | 156 | +        var postId = Random.Shared.Next(MinPostId, MaxPostId);  | 
 | 157 | +        var route = $"/posts/{postId}";  | 
 | 158 | + | 
 | 159 | +        var builder = _client.UsingRoute(route);  | 
 | 160 | + | 
 | 161 | +        var response = await builder.DeleteAsync();  | 
 | 162 | + | 
 | 163 | +        response.EnsureSuccessStatusCode();  | 
 | 164 | +    }  | 
 | 165 | + | 
 | 166 | +    [Fact]  | 
 | 167 | +    public async Task CreatePostAsync()  | 
 | 168 | +    {  | 
 | 169 | +        var post = new Post  | 
 | 170 | +        {  | 
 | 171 | +            Id = 0,  | 
 | 172 | +            UserId = Random.Shared.Next(MinUserId, MaxUserId),  | 
 | 173 | +            Title = Guid.NewGuid().ToString(),  | 
 | 174 | +            Body = Guid.NewGuid().ToString()  | 
 | 175 | +        };  | 
 | 176 | + | 
 | 177 | +        var newPost = await _client  | 
 | 178 | +            .UsingRoute("/posts")  | 
 | 179 | +            .WithJsonContent(post)  | 
 | 180 | +            .PostAsync()  | 
 | 181 | +            .DeserializeJsonAsync<Post>();  | 
 | 182 | + | 
 | 183 | +        newPost.ShouldNotBeNull();  | 
 | 184 | +        newPost.ShouldSatisfyAllConditions  | 
 | 185 | +        (  | 
 | 186 | +            () => newPost.Id.ShouldBe(MaxPostId + 1),  | 
 | 187 | +            () => newPost.UserId.ShouldBe(post.UserId),  | 
 | 188 | +            () => newPost.Title.ShouldBe(post.Title),  | 
 | 189 | +            () => newPost.Body.ShouldBe(post.Body)  | 
 | 190 | +        );  | 
 | 191 | +    }  | 
 | 192 | + | 
 | 193 | +    [Fact]  | 
 | 194 | +    public async Task UpdatePostAsync()  | 
 | 195 | +    {  | 
 | 196 | +        var expected = new Post  | 
 | 197 | +        {  | 
 | 198 | +            Id = Random.Shared.Next(MinPostId, MaxPostId),  | 
 | 199 | +            UserId = Random.Shared.Next(MinUserId, MaxUserId),  | 
 | 200 | +            Title = Guid.NewGuid().ToString(),  | 
 | 201 | +            Body = Guid.NewGuid().ToString()  | 
 | 202 | +        };  | 
 | 203 | + | 
 | 204 | +        var route = $"/posts/{expected.Id}";  | 
 | 205 | +        var builder = _client.UsingRoute(route);  | 
 | 206 | + | 
 | 207 | +        builder.WithJsonContent(expected);  | 
 | 208 | + | 
 | 209 | +        var response = await builder.PutAsync();  | 
 | 210 | + | 
 | 211 | +        response.EnsureSuccessStatusCode();  | 
 | 212 | + | 
 | 213 | +        var actual =  await response  | 
 | 214 | +            .DeserializeJsonAsync<Post>();  | 
 | 215 | + | 
 | 216 | +        actual.ShouldNotBeNull();  | 
 | 217 | +        actual.ShouldSatisfyAllConditions  | 
 | 218 | +        (  | 
 | 219 | +            () => actual.Id.ShouldBe(expected.Id),  | 
 | 220 | +            () => actual.UserId.ShouldBe(expected.UserId),  | 
 | 221 | +            () => actual.Title.ShouldBe(expected.Title),  | 
 | 222 | +            () => actual.Body.ShouldBe(expected.Body)  | 
 | 223 | +        );  | 
 | 224 | +    }  | 
 | 225 | +}  | 
0 commit comments