Skip to content

Commit

Permalink
Merge pull request #68 from billybimbob/master
Browse files Browse the repository at this point in the history
use case-insensitive HeaderManager
  • Loading branch information
jregnier authored Oct 22, 2021
2 parents d0426ba + dacfb84 commit ddbb846
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 16 deletions.
26 changes: 17 additions & 9 deletions src/MtgApiManager.Lib.Test/Core/HeaderManagerTests.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;
using System.Collections.Generic;
using MtgApiManager.Lib.Core;
using Xunit;
using Flurl.Util;

namespace MtgApiManager.Lib.Test.Core
{
Expand All @@ -24,14 +24,16 @@ public void Get_CacheNull_ReturnsDefault()
public void Get_FailsConvertion_ReturnsDefault()
{
// arrange
var items = new List<(string, string)>
var items = new []
{
(ResponseHeader.PageSize.Name, "Not an integer"),
(ResponseHeader.Count.Name, "100"),
};

var headers = new NameValueList<string>(items, false);

var manager = new HeaderManager();
manager.Update(items);
manager.Update(headers);

// act
var result = manager.Get<int>(ResponseHeader.PageSize);
Expand All @@ -44,14 +46,16 @@ public void Get_FailsConvertion_ReturnsDefault()
public void Get_KeyDoesNotExist_ReturnsDefault()
{
// arrange
var items = new List<(string, string)>
var items = new []
{
("key1", "value1"),
("key2", "value2"),
};

var headers = new NameValueList<string>(items, false);

var manager = new HeaderManager();
manager.Update(items);
manager.Update(headers);

// act
var result = manager.Get<string>(ResponseHeader.PageSize);
Expand All @@ -65,14 +69,16 @@ public void Get_KeyExists_Success()
{
// arrange
const int PAGE_SIZE = 50;
var items = new List<(string, string)>
var items = new []
{
(ResponseHeader.PageSize.Name, PAGE_SIZE.ToString()),
(ResponseHeader.Count.Name, "100"),
};

var headers = new NameValueList<string>(items, false);

var manager = new HeaderManager();
manager.Update(items);
manager.Update(headers);

// act
var result = manager.Get<int>(ResponseHeader.PageSize);
Expand All @@ -85,16 +91,18 @@ public void Get_KeyExists_Success()
public void Update_CacheUpdated_Success()
{
// arrange
var items = new List<(string, string)>
var items = new []
{
(ResponseHeader.PageSize.Name, "50"),
(ResponseHeader.Count.Name, "100"),
};

var headers = new NameValueList<string>(items, false);

var manager = new HeaderManager();

// act
manager.Update(items);
manager.Update(headers);

// assert
Assert.NotEmpty(manager.Get<string>(ResponseHeader.PageSize));
Expand Down
15 changes: 15 additions & 0 deletions src/MtgApiManager.Lib.Test/Service/CardServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,21 @@ public async Task AllAsync_Success()
_mockRepository.VerifyAll();
}

[Fact]
public async Task AllAsync_NoMock_EqualPageSize()
{
const int pageSize = 50;
var serviceProvider = new MtgServiceProvider();
var service = serviceProvider.GetCardService();

var result = await service
.Where(x => x.Page, 1)
.Where(x => x.PageSize, pageSize)
.AllAsync();

Assert.Equal(pageSize, result.PagingInfo.PageSize);
}

[Fact]
public async Task FindAsync_ById_Success()
{
Expand Down
17 changes: 12 additions & 5 deletions src/MtgApiManager.Lib/Core/HeaderManager.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using Flurl.Util;

namespace MtgApiManager.Lib.Core
{
Expand Down Expand Up @@ -39,7 +39,7 @@ public T Get<T>(ResponseHeader responseHeader)
}
}

public void Update(IReadOnlyList<(string Name, string Value)> headers)
public void Update(IReadOnlyNameValueList<string> headers)
{
if (headers == null)
{
Expand All @@ -50,9 +50,16 @@ public void Update(IReadOnlyList<(string Name, string Value)> headers)

try
{
_headersCache = headers.ToLookup(
k => k.Name,
v => v.Value);
// nested GetAll is technically O(n^2) instead of the original O(n)

_headersCache = Enumeration
.GetAll<ResponseHeader>()
.SelectMany(
rh => headers.GetAll(rh.Name),
(rh, Value) => (rh.Name, Value))
.ToLookup(
k => k.Name,
v => v.Value);
}
finally
{
Expand Down
4 changes: 2 additions & 2 deletions src/MtgApiManager.Lib/Core/IHeaderManager.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
using System.Collections.Generic;
using Flurl.Util;

namespace MtgApiManager.Lib.Core
{
internal interface IHeaderManager
{
T Get<T>(ResponseHeader responseHeader);

void Update(IReadOnlyList<(string Name, string Value)> headers);
void Update(IReadOnlyNameValueList<string> headers);
}
}

0 comments on commit ddbb846

Please sign in to comment.