Skip to content

Commit

Permalink
Merge pull request #149 from bmazzarol/feat/upgrade-bb-to-7
Browse files Browse the repository at this point in the history
chore: upgrade to BunsenBurner 7
  • Loading branch information
bmazzarol committed Jun 25, 2024
2 parents 3f82d22 + e05ad18 commit 0309432
Show file tree
Hide file tree
Showing 10 changed files with 130 additions and 128 deletions.
2 changes: 1 addition & 1 deletion Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
</ItemGroup>
<!-- Test packages -->
<ItemGroup>
<PackageVersion Include="BunsenBurner" Version="6.2.4" />
<PackageVersion Include="BunsenBurner" Version="7.0.0" />
<PackageVersion Include="Docfx.ResultSnippets" Version="2.0.0" />
<PackageVersion Include="FluentAssertions" Version="6.12.0" />
<PackageVersion Include="Meziantou.Xunit.ParallelTestFramework" Version="2.2.0" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System.Net;
using System.Text.Json;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
Expand All @@ -9,7 +8,7 @@

namespace HttpBuildR.ActionResult.Tests;

public static class ActionResultBuilderTests
public sealed class ActionResultBuilderTests
{
private static async Task<HttpResponse> ConvertToResponse(ActionResult<string> ar)
{
Expand All @@ -24,23 +23,23 @@ private static async Task<HttpResponse> ConvertToResponse(ActionResult<string> a
}

[Fact(DisplayName = "A T can be converted to an OK response")]
public static void Case1() => ActionResultBuilder.Ok("this is a test").Should().NotBeNull();
public void Case1() => ActionResultBuilder.Ok("this is a test").Should().NotBeNull();

[Fact(DisplayName = "A T can be converted to an OK response and cookie")]
public static async Task Case2() =>
await ActionResultBuilder
public Task Case2() =>
ActionResultBuilder
.Ok("this is a test", Cookie.New("a", "c"))
.ArrangeData()
.Arrange()
.Act(ConvertToResponse)
.Assert(r => r.StatusCode.Should().Be((int)HttpStatusCode.OK))
.Assert(r => r.StatusCode.Should().Be((int)Resp.OK))
.And(r => r.Headers.Should().ContainKey("Set-Cookie").And.ContainValue("a=c; path=/"))
.And(async r =>
(await new StreamReader(r.Body).ReadToEndAsync()).Should().Be("\"this is a test\"")
);

[Fact(DisplayName = "A response can be converted to an action response")]
public static async Task Case3() =>
await Resp
public Task Case3() =>
Resp
.BadRequest.Result()
.WithProblemDetails(
"a",
Expand All @@ -50,9 +49,9 @@ await Resp
new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase }
)
.ToActionResult<string>(Cookie.New("a", "b"))
.ArrangeData()
.Arrange()
.Act(ConvertToResponse)
.Assert(r => r.StatusCode.Should().Be((int)HttpStatusCode.BadRequest))
.Assert(r => r.StatusCode.Should().Be((int)Resp.BadRequest))
.And(r => r.Headers.Should().ContainKey("Set-Cookie").And.ContainValue("a=b; path=/"))
.And(async r =>
(await new StreamReader(r.Body).ReadToEndAsync())
Expand All @@ -63,14 +62,14 @@ await Resp
);

[Fact(DisplayName = "A response can be converted to an action response with no content")]
public static async Task Case4() =>
await Resp
public Task Case4() =>
Resp
.NotAcceptable.Result()
.WithHeader("a", "b")
.ToActionResult<string>()
.ArrangeData()
.Arrange()
.Act(ConvertToResponse)
.Assert(r => r.StatusCode.Should().Be((int)HttpStatusCode.NotAcceptable))
.Assert(r => r.StatusCode.Should().Be((int)Resp.NotAcceptable))
.And(r => r.Headers.Should().ContainKey("a").And.ContainValue("b"))
.And(r => r.ContentType.Should().BeNullOrEmpty())
.And(r => r.ContentLength.Should().Be(0L));
Expand Down
1 change: 0 additions & 1 deletion src/Request/HttpBuildR.Request.Tests/GlobalUsings.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
global using BunsenBurner;
global using FluentAssertions;
global using Xunit;
global using Scenario = BunsenBurner.Scenario<BunsenBurner.Syntax.Aaa>;
48 changes: 23 additions & 25 deletions src/Request/HttpBuildR.Request.Tests/RequestContentTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,19 @@

namespace HttpBuildR.Request.Tests;

public static class RequestContentTests
using Scenario = TestBuilder<ArrangeActAssertSyntax>.Acted<HttpRequestMessage, HttpContent>;

public sealed class RequestContentTests
{
private static Scenario.Acted<HttpRequestMessage, HttpContent> ArrangeAndAct(
Func<HttpRequestMessage, HttpRequestMessage> fn
) =>
private static Scenario Arrange(Func<HttpRequestMessage, HttpRequestMessage> fn) =>
new HttpRequestMessage()
.ArrangeData()
.Arrange()
.Act(fn)
.And((_, req) => req.Content ?? throw new InvalidOperationException());

[Fact(DisplayName = "Json content can be added to a request")]
public static async Task Case1() =>
await ArrangeAndAct(x =>
x.WithJsonContent(new { A = 1, B = "2" }, JsonSerializerOptions.Default)
)
public Task Case1() =>
Arrange(x => x.WithJsonContent(new { A = 1, B = "2" }, JsonSerializerOptions.Default))
.Assert(async content =>
{
content.Headers.ContentType!.MediaType.Should().Be("application/json");
Expand All @@ -27,8 +25,8 @@ public static class RequestContentTests
});

[Fact(DisplayName = "Json content can be added to a request without options")]
public static async Task Case2() =>
await ArrangeAndAct(x => x.WithJsonContent(new { A = 1, B = "2" }))
public Task Case2() =>
Arrange(x => x.WithJsonContent(new { A = 1, B = "2" }))
.Assert(async content =>
{
content.Headers.ContentType!.MediaType.Should().Be("application/json");
Expand All @@ -43,8 +41,8 @@ public class Person
}

[Fact(DisplayName = "Xml content can be added to a request")]
public static async Task Case3() =>
await ArrangeAndAct(x =>
public Task Case3() =>
Arrange(x =>
x.WithXmlContent(
new Person { Name = "John", Age = 36 },
new XmlWriterSettings { Indent = false }
Expand All @@ -62,8 +60,8 @@ public class Person
});

[Fact(DisplayName = "Xml content can be added and the writer customized")]
public static async Task Case4() =>
await ArrangeAndAct(x =>
public Task Case4() =>
Arrange(x =>
x.WithXmlContent(new Person { Name = "John", Age = 36 }, modifyWriterFunc: w => w)
)
.Assert(async content =>
Expand All @@ -78,8 +76,8 @@ public class Person
});

[Fact(DisplayName = "Text content can be added to a request")]
public static async Task Case5() =>
await ArrangeAndAct(x => x.WithTextContent("<div>some text</div>", "text/html"))
public Task Case5() =>
Arrange(x => x.WithTextContent("<div>some text</div>", "text/html"))
.Assert(async content =>
{
content.Headers.ContentType!.MediaType.Should().Be(MediaTypeNames.Text.Html);
Expand All @@ -88,8 +86,8 @@ await ArrangeAndAct(x => x.WithTextContent("<div>some text</div>", "text/html"))
});

[Fact(DisplayName = "Text content can be added to a request without media type")]
public static async Task Case6() =>
await ArrangeAndAct(x => x.WithTextContent("hello world"))
public Task Case6() =>
Arrange(x => x.WithTextContent("hello world"))
.Assert(async content =>
{
content.Headers.ContentType!.MediaType.Should().Be(MediaTypeNames.Text.Plain);
Expand All @@ -98,8 +96,8 @@ await ArrangeAndAct(x => x.WithTextContent("hello world"))
});

[Fact(DisplayName = "Form url encoded content can be added to a request")]
public static async Task Case7() =>
await ArrangeAndAct(x =>
public Task Case7() =>
Arrange(x =>
x.WithFormUrlContent(
new Dictionary<string, string>(StringComparer.Ordinal)
{
Expand All @@ -118,8 +116,8 @@ await ArrangeAndAct(x => x.WithTextContent("hello world"))
});

[Fact(DisplayName = "Form url encoded content can be added to a request")]
public static async Task Case7b() =>
await ArrangeAndAct(x =>
public Task Case7B() =>
Arrange(x =>
x.WithFormUrlContent(KeyValuePair.Create("A", "1"), KeyValuePair.Create("B", "2"))
)
.Assert(async content =>
Expand All @@ -132,8 +130,8 @@ await ArrangeAndAct(x => x.WithTextContent("hello world"))
});

[Fact(DisplayName = "Json content can be added to a request using a source generator")]
public static async Task Case8() =>
await ArrangeAndAct(x =>
public Task Case8() =>
Arrange(x =>
x.WithJsonContent(
new Widget("Test", 123.50),
ExampleJsonSourceGenerator.Default.Widget
Expand Down
42 changes: 21 additions & 21 deletions src/Request/HttpBuildR.Request.Tests/RequestHeadersTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,31 @@

namespace HttpBuildR.Request.Tests;

public static class RequestHeadersTests
public sealed class RequestHeadersTests
{
[Fact(DisplayName = "Base authentication can be set")]
public static void Case1() =>
public void Case1() =>
new HttpRequestMessage()
.WithBasicToken("abcd")
.Headers.Authorization.Should()
.BeEquivalentTo(new { Scheme = "Basic", Parameter = "abcd" });

[Fact(DisplayName = "Bearer authentication can be set")]
public static void Case2() =>
public void Case2() =>
new HttpRequestMessage()
.WithBearerToken("abcde")
.Headers.Authorization.Should()
.BeEquivalentTo(new { Scheme = "Bearer", Parameter = "abcde" });

[Fact(DisplayName = "Custom header can be set with a single value")]
public static void Case3() =>
public void Case3() =>
new HttpRequestMessage()
.WithHeader("a", "1")
.Headers.Should()
.Contain(x => x.Key == "a" && x.Value.Count() == 1 && x.Value.First() == "1");

[Fact(DisplayName = "Custom header can be set with multiple values")]
public static void Case4() =>
public void Case4() =>
new HttpRequestMessage()
.WithHeader("a", "1", "2", "3")
.Headers.Should()
Expand All @@ -39,14 +39,14 @@ public static class RequestHeadersTests
);

[Fact(DisplayName = "Proxy authorization header can be set")]
public static void Case5() =>
public void Case5() =>
new HttpRequestMessage()
.WithProxyAuthorization("Test", "abcdef")
.Headers.ProxyAuthorization.Should()
.BeEquivalentTo(new { Scheme = "Test", Parameter = "abcdef" });

[Fact(DisplayName = "Cache control header can be set")]
public static void Case6() =>
public void Case6() =>
new HttpRequestMessage()
.WithCacheControl(
new CacheControlHeaderValue { MaxAge = TimeSpan.FromSeconds(20), NoStore = true }
Expand All @@ -55,88 +55,88 @@ public static class RequestHeadersTests
.BeEquivalentTo(new { MaxAge = TimeSpan.FromSeconds(20), NoStore = true });

[Fact(DisplayName = "Connection close header can be set")]
public static void Case7() =>
public void Case7() =>
new HttpRequestMessage()
.WithConnectionClose(true)
.WithConnectionClose(value: true)
.Headers.ConnectionClose.Should()
.BeTrue();

[Fact(DisplayName = "Date header can be set")]
public static void Case8() =>
public void Case8() =>
new HttpRequestMessage()
.WithDate(DateTimeOffset.UtcNow)
.Headers.Date.Should()
.BeCloseTo(DateTimeOffset.Now, TimeSpan.FromSeconds(1));

[Fact(DisplayName = "Accept headers can be set")]
public static void Case9() =>
public void Case9() =>
new HttpRequestMessage()
.WithAccept("text/json", 0.20)
.Headers.Accept.Should()
.Contain(x => x.MediaType == "text/json" && x.Quality == 0.20);

[Fact(DisplayName = "Accept headers can be set without quality")]
public static void Case10() =>
public void Case10() =>
new HttpRequestMessage()
.WithAccept("text/json2")
.Headers.Accept.Should()
.Contain(x => x.MediaType == "text/json2" && !x.Quality.HasValue);

[Fact(DisplayName = "If-Modified-Since header can be set")]
public static void Case11() =>
public void Case11() =>
new HttpRequestMessage()
.WithIfModifiedSince(DateTimeOffset.UtcNow)
.Headers.IfModifiedSince.Should()
.BeCloseTo(DateTimeOffset.UtcNow, TimeSpan.FromSeconds(1));

[Fact(DisplayName = "Range header can be set")]
public static void Case12() =>
public void Case12() =>
new HttpRequestMessage()
.WithRange(20, 50)
.Headers.Range.Should()
.BeEquivalentTo(new RangeHeaderValue(20, 50));

[Fact(DisplayName = "If-Range header can be set using data time")]
public static void Case13() =>
public void Case13() =>
new HttpRequestMessage()
.WithIfRange(DateTimeOffset.Now)
.Headers.IfRange!.Date.Should()
.BeCloseTo(DateTimeOffset.Now, TimeSpan.FromSeconds(1));

[Fact(DisplayName = "If-Range header can be set using e-tag")]
public static void Case14() =>
public void Case14() =>
new HttpRequestMessage()
.WithIfRange(new EntityTagHeaderValue("\"a\""))
.Headers.IfRange!.EntityTag.Should()
.BeEquivalentTo(new EntityTagHeaderValue("\"a\""));

[Fact(DisplayName = "If-Unmodified-Since header can be set")]
public static void Case15() =>
public void Case15() =>
new HttpRequestMessage()
.WithIfUnmodifiedSince(DateTimeOffset.UtcNow)
.Headers.IfUnmodifiedSince.Should()
.BeCloseTo(DateTimeOffset.UtcNow, TimeSpan.FromSeconds(1));

[Fact(DisplayName = "Max-Forwards header can be set")]
public static void Case16() =>
public void Case16() =>
new HttpRequestMessage().WithMaxForwards(3).Headers.MaxForwards.Should().Be(3);

[Fact(DisplayName = "Referrer header can be set")]
public static void Case17() =>
public void Case17() =>
new HttpRequestMessage()
.WithReferrer("https://some-domain")
.Headers.Referrer.Should()
.Be(new Uri("https://some-domain"));

[Fact(DisplayName = "Transfer-Encoding header can be set")]
public static void Case18() =>
public void Case18() =>
new HttpRequestMessage()
.WithTransferEncodingChunked(true)
.Headers.TransferEncodingChunked.Should()
.BeTrue();

[Fact(DisplayName = "Headers can be modified using an action")]
public static void Case19() =>
public void Case19() =>
new HttpRequestMessage()
.WithHeaderModifications(h =>
{
Expand Down
Loading

0 comments on commit 0309432

Please sign in to comment.