Skip to content

Commit 7cc90ab

Browse files
test(message pack): include messagepack sample + fix test (#84)
1 parent 8566939 commit 7cc90ab

File tree

9 files changed

+152
-14
lines changed

9 files changed

+152
-14
lines changed

Directory.Packages.props

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
</PropertyGroup>
55
<ItemGroup>
66
<!-- system -->
7+
<PackageVersion Include="MessagePack.AspNetCoreMvcFormatter" Version="3.0.111-alpha" />
78
<PackageVersion Include="System.Data.HashFunction.xxHash" Version="2.0.0" />
89
<!-- microsoft packages -->
910
<PackageVersion Include="Microsoft.Extensions.Caching.Memory" Version="8.0.0" />
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
using FluentlyHttpClient.Sample.Api.Heroes;
2+
using Microsoft.AspNetCore.Mvc;
3+
using System.ComponentModel.DataAnnotations;
4+
5+
namespace FluentlyHttpClient.Sample.Api.Controllers;
6+
7+
// GET
8+
[Route("api/[controller]")]
9+
[ApiController]
10+
public class HeroesController(
11+
IHeroService service
12+
) : Controller
13+
{
14+
// GET api/heroes
15+
[HttpGet]
16+
public async Task<IEnumerable<Hero>> Get()
17+
{
18+
return await service.GetAll();
19+
}
20+
21+
// GET api/heroes/azmodan
22+
[HttpGet("{key}")]
23+
public async Task<IActionResult> Get(string key)
24+
{
25+
var hero = await service.GetByKey(key);
26+
if (hero == null)
27+
return NotFound();
28+
return Ok(hero);
29+
}
30+
31+
// PUT api/heroes/azmodan
32+
[HttpPost]
33+
public async Task<IActionResult> Post([FromBody][Required] Hero input)
34+
{
35+
if (!ModelState.IsValid)
36+
return BadRequest(ModelState);
37+
38+
//try
39+
//{
40+
await service.Add(input);
41+
//}
42+
//catch (ApiException ex)
43+
//{
44+
// return BadRequest(new
45+
// {
46+
// ex.ErrorCode
47+
// });
48+
//}
49+
return Ok(input);
50+
}
51+
}

samples/FluentlyHttpClient.Sample.Api/FluentlyHttpClient.Sample.Api.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
<ItemGroup>
99
<PackageReference Include="Humanizer.Core" />
10+
<PackageReference Include="MessagePack.AspNetCoreMvcFormatter" />
1011
</ItemGroup>
1112

1213
<ItemGroup>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using System.ComponentModel.DataAnnotations;
2+
3+
namespace FluentlyHttpClient.Sample.Api.Heroes;
4+
5+
public record Hero
6+
{
7+
[Required]
8+
public string Key { get; set; }
9+
10+
[Required]
11+
public string Name { get; set; }
12+
public string Title { get; set; }
13+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
namespace FluentlyHttpClient.Sample.Api.Heroes;
2+
3+
public interface IHeroService
4+
{
5+
Task<ICollection<Hero>> GetAll();
6+
Task<Hero?> GetByKey(string key);
7+
Task Add(Hero input);
8+
}
9+
10+
public class HeroService : IHeroService
11+
{
12+
private readonly ICollection<Hero> _data =
13+
[
14+
new()
15+
{
16+
Key = "azmodan",
17+
Name = "Azmodan",
18+
Title = "Lord of Sin"
19+
},
20+
new()
21+
{
22+
Key = "rexxar",
23+
Name = "Rexxar",
24+
Title = "Champion of the Horde"
25+
},
26+
new()
27+
{
28+
Key = "maiev",
29+
Name = "Maiev",
30+
Title = "The Warden"
31+
},
32+
new()
33+
{
34+
Key = "malthael",
35+
Name = "Malthael",
36+
Title = "Aspect of Death"
37+
},
38+
new()
39+
{
40+
Key = "garrosh",
41+
Name = "Garrosh",
42+
Title = "Son of Hellscream"
43+
},
44+
];
45+
46+
public Task<ICollection<Hero>> GetAll() => Task.FromResult(_data);
47+
48+
public Task<Hero?> GetByKey(string key)
49+
=> Task.FromResult(_data.FirstOrDefault(x => x.Key == key));
50+
51+
public async Task Add(Hero input)
52+
{
53+
var result = await GetByKey(input.Key);
54+
//if (result != null)
55+
//{
56+
// throw new ApiException(HttpStatusCode.BadRequest)
57+
// {
58+
// ErrorCode = "error.hero.key-already-exists"
59+
// };
60+
//}
61+
62+
_data.Add(input);
63+
}
64+
}

samples/FluentlyHttpClient.Sample.Api/Startup.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
using FluentlyHttpClient.Sample.Api.Heroes;
2+
using MessagePack.AspNetCoreMvcFormatter;
3+
using MessagePack.Resolvers;
4+
15
namespace FluentlyHttpClient.Sample.Api;
26

37
public class Startup
@@ -13,9 +17,15 @@ public Startup(IConfiguration configuration)
1317
public void ConfigureServices(IServiceCollection services)
1418
{
1519
services
20+
.AddSingleton<IHeroService, HeroService>()
1621
.AddFluentlyHttpClient()
1722
//.AddFluentlyHttpClientEntity(Configuration.GetConnectionString("FluentlyDatabase"))
18-
.AddControllers()
23+
.AddControllers(opts =>
24+
{
25+
opts.OutputFormatters.Add(new MessagePackOutputFormatter(ContractlessStandardResolver.Options));
26+
opts.InputFormatters.Add(new MessagePackInputFormatter(ContractlessStandardResolver.Options));
27+
}
28+
)
1929
;
2030
}
2131

test/FluentHttpClientFactoryTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System.Net.Http.Formatting;
1+
using System.Net.Http.Formatting;
22
using static FluentlyHttpClient.Test.ServiceTestUtil;
33

44
// ReSharper disable once CheckNamespace

test/Hero.cs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,11 @@
22

33
namespace FluentlyHttpClient.Test;
44

5-
/// <summary>
6-
/// Organization model which is used for tests.
7-
/// </summary>
8-
public class Hero
5+
public record Hero
96
{
10-
public string Key { get; set; }
11-
public string Name { get; set; }
12-
public string Title { get; set; }
7+
public string Key { get; set; } = null!;
8+
public string Name { get; set; } = null!;
9+
public string Title { get; set; } = null!;
1310
}
1411

1512
public enum HeroRole

test/Integration/MessagePackIntegrationTest.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
1-
using Sketch7.MessagePack.MediaTypeFormatter;
1+
using MessagePack.Resolvers;
2+
using Sketch7.MessagePack.MediaTypeFormatter;
23
using System.Net;
34
using static FluentlyHttpClient.Test.ServiceTestUtil;
45

56
namespace FluentlyHttpClient.Test.Integration;
67

78
public class MessagePackIntegrationTest
89
{
9-
private readonly MessagePackMediaTypeFormatter _messagePackMediaTypeFormatter = new();
10+
private readonly MessagePackMediaTypeFormatter _messagePackMediaTypeFormatter = new(ContractlessStandardResolver.Options);
1011

1112
[Fact]
1213
[Trait("Category", "e2e")]
1314
public async void ShouldMakeRequest_Get()
1415
{
1516
var httpClient = GetNewClientFactory().CreateBuilder("sketch7")
16-
.WithBaseUrl("http://localhost:5001")
17+
.WithBaseUrl("http://localhost:5500")
1718
.UseTimer()
1819
.ConfigureFormatters(opts =>
1920
{
@@ -27,15 +28,15 @@ public async void ShouldMakeRequest_Get()
2728
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
2829
Assert.Equal("azmodan", response.Data.Key);
2930
Assert.Equal("Azmodan", response.Data.Name);
30-
Assert.Equal("Lord of Sins", response.Data.Title);
31+
Assert.Equal("Lord of Sin", response.Data.Title);
3132
}
3233

3334
[Fact]
3435
[Trait("Category", "e2e")]
3536
public async void ShouldMakeRequest_Post()
3637
{
3738
var httpClient = GetNewClientFactory().CreateBuilder("sketch7")
38-
.WithBaseUrl("http://localhost:5001")
39+
.WithBaseUrl("http://localhost:5500")
3940
.ConfigureFormatters(opts =>
4041
{
4142
opts.Default = _messagePackMediaTypeFormatter;

0 commit comments

Comments
 (0)