Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
278 changes: 277 additions & 1 deletion KGSoft.TinyHttpClient.Test/HttpRequestBuilderTests.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
using KGSoft.TinyHttpClient.Model;
using KGSoft.TinyHttpClient.Logging;
using KGSoft.TinyHttpClient.Model;
using KGSoft.TinyHttpClient.Test.Model;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Polly;
using Polly.Retry;
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;
using Newtonsoft.Json;


namespace KGSoft.TinyHttpClient.Test;
Expand Down Expand Up @@ -108,4 +115,273 @@ public async Task Test_DELETE()

AssertResponse(response);
}

[TestMethod]
public async Task Test_GET_CtorUri()
{
var response = await new HttpRequestBuilder($"{ApiBase}api/users/2")
.Get()
.MakeRequestAsync();

AssertResponse(response);
}

[TestMethod]
public async Task Test_PUT_CtorUri()
{
var response = await new HttpRequestBuilder($"{ApiBase}api/users/2")
.Put()
.AddBody(new Data() { FirstName = "Chewbacca" })
.MakeRequestAsync();

AssertResponse(response);
}

[TestMethod]
public async Task Test_POST_CtorUri()
{
var response = await new HttpRequestBuilder($"{ApiBase}api/users")
.Post()
.AddBody(new Data() { FirstName = "Chewbacca" })
.MakeRequestAsync();

AssertResponse(response);
}

[TestMethod]
public async Task Test_PATCH()
{
var response = await new HttpRequestBuilder()
.Patch($"{ApiBase}api/users/2")
.AddBody(new Data() { FirstName = "Chewbacca" })
.MakeRequestAsync();

AssertResponse(response);
}

[TestMethod]
public async Task Test_PATCH_CtorUri()
{
var response = await new HttpRequestBuilder($"{ApiBase}api/users/2")
.Patch()
.AddBody(new Data() { FirstName = "Chewbacca" })
.MakeRequestAsync();

AssertResponse(response);
}

[TestMethod]
public async Task Test_DELETE_CtorUri()
{
var response = await new HttpRequestBuilder($"{ApiBase}api/users")
.Delete()
.MakeRequestAsync();

AssertResponse(response);
}

[TestMethod]
public async Task Test_HEAD()
{
var response = await new HttpRequestBuilder($"{ApiBase}api/users/2")
.Head()
.MakeRequestAsync();

AssertResponse(response);
}

[TestMethod]
public async Task Test_OPTIONS()
{
var response = await new HttpRequestBuilder()
.Options($"{ApiBase}api/users/2")
.MakeRequestAsync();

AssertResponse(response);
}

[TestMethod]
public async Task Test_WithUri()
{
var response = await new HttpRequestBuilder()
.WithUri($"{ApiBase}api/users/2")
.Get()
.MakeRequestAsync();

AssertResponse(response);
}

[TestMethod]
public async Task Test_WithAliases()
{
var response = await new HttpRequestBuilder($"{ApiBase}api/users")
.Post()
.WithHeader("X-Correlation-ID", "123")
.WithHeader("X-Correlation-ID", "456")
.WithBearerToken("XYZ")
.WithBody(new Data() { FirstName = "Chewbacca" })
.WithJsonSerializerSettings(new JsonSerializerSettings())
.MakeRequestAsync();

AssertResponse(response);
}

[TestMethod]
public async Task Test_AddQueryParams_Enumerable_AllowsDuplicates()
{
var response = await new HttpRequestBuilder($"{ApiBase}api/users/2")
.Get()
.AddQueryParams(new List<KeyValuePair<string, string>>
{
new("page", "1"),
new("page", "2")
})
.MakeRequestAsync();

AssertResponse(response);
}

[TestMethod]
public async Task Test_WithContentFactory_WithRetry()
{
var contentFactoryCount = 0;
var retryPolicy = new ResiliencePipelineBuilder<Response>()
.AddRetry(new RetryStrategyOptions<Response>
{
ShouldHandle = new PredicateBuilder<Response>()
.HandleResult(response => !response.IsSuccess),
MaxRetryAttempts = 2,
Delay = TimeSpan.Zero
})
.Build();

var response = await new HttpRequestBuilder($"{ApiBase}api/unknown/23")
.Post()
.WithContent(() =>
{
contentFactoryCount++;
return new StringContent("{}");
})
.WithRetry(retryPolicy)
.MakeRequestAsync();

Assert.IsFalse(response.IsSuccess);
Assert.AreEqual(3, contentFactoryCount);
}

[TestMethod]
public async Task Test_WithLogger_DefaultsToAllRequests()
{
var logger = new TestLogger();

var response = await new HttpRequestBuilder($"{ApiBase}api/users/2")
.Get()
.WithLogger(logger)
.MakeRequestAsync();

AssertResponse(response);
Assert.AreEqual(2, logger.Messages.Count);
}

[TestMethod]
public async Task Test_WithLogger_OnlyFailedRequests_DoesNotLogSuccess()
{
var logger = new TestLogger();

var response = await new HttpRequestBuilder($"{ApiBase}api/users/2")
.Get()
.WithLogger(logger)
.WithLogScope(Enums.LogScope.OnlyFailedRequests)
.MakeRequestAsync();

AssertResponse(response);
Assert.AreEqual(0, logger.Messages.Count);
}

[TestMethod]
public async Task Test_WithLogger_OnlyFailedRequests_LogsFailure()
{
var logger = new TestLogger();

var response = await new HttpRequestBuilder($"{ApiBase}api/unknown/23")
.Get()
.WithLogger(logger)
.WithLogScope(Enums.LogScope.OnlyFailedRequests)
.MakeRequestAsync();

Assert.IsFalse(response.IsSuccess);
Assert.AreEqual(1, logger.Messages.Count);
}

[TestMethod]
public async Task Test_GET_WithRetry()
{
var retryCount = 0;
var retryPolicy = new ResiliencePipelineBuilder<Response>()
.AddRetry(new RetryStrategyOptions<Response>
{
ShouldHandle = new PredicateBuilder<Response>()
.HandleResult(response => !response.IsSuccess),
MaxRetryAttempts = 2,
Delay = TimeSpan.Zero,
OnRetry = args =>
{
retryCount++;
return ValueTask.CompletedTask;
}
})
.Build();

var response = await new HttpRequestBuilder()
.Get($"{ApiBase}api/unknown/23")
.WithRetry(retryPolicy)
.MakeRequestAsync();

Assert.IsFalse(response.IsSuccess);
Assert.AreEqual(2, retryCount);
}

[TestMethod]
public async Task Test_GET_Result_WithRetry()
{
var retryCount = 0;
var retryPolicy = new ResiliencePipelineBuilder<Response>()
.AddRetry(new RetryStrategyOptions<Response>
{
ShouldHandle = new PredicateBuilder<Response>()
.HandleResult(response => !response.IsSuccess),
MaxRetryAttempts = 2,
Delay = TimeSpan.Zero,
OnRetry = args =>
{
retryCount++;
return ValueTask.CompletedTask;
}
})
.Build();

var response = await new HttpRequestBuilder()
.Get($"{ApiBase}api/unknown/23")
.WithRetry(retryPolicy)
.MakeRequestAsync<TestUser>();

Assert.IsFalse(response.IsSuccess);
Assert.AreEqual(2, retryCount);
}

private sealed class TestLogger : ILogger
{
public List<string> Messages { get; } = new();
public List<Exception> Exceptions { get; } = new();

public void LogMessage(string message)
{
Messages.Add(message);
}

public void LogException(Exception ex)
{
Exceptions.Add(ex);
}
}
}
Loading