Skip to content

Commit

Permalink
Merge pull request #11 from wonderbird/refactor/readability
Browse files Browse the repository at this point in the history
refactor/readability
  • Loading branch information
wonderbird authored Nov 4, 2024
2 parents 817a390 + 2874398 commit eb0491a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 18 deletions.
9 changes: 9 additions & 0 deletions src/SaveEnergy.Specs/Hooks/MockServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ public void Stop()
}

public void ConfigureSuccessfulDeviceAuthorization()
{
ConfigureDeviceCodeResponse();
ConfigureAccesTokenResponse();
}

private void ConfigureDeviceCodeResponse()
{
_mockServer
?.Given(Request.Create().WithPath("/login/device/code").UsingPost())
Expand All @@ -42,7 +48,10 @@ public void ConfigureSuccessfulDeviceAuthorization()
}
)
);
}

private void ConfigureAccesTokenResponse()
{
_mockServer
?.Given(Request.Create().WithPath("/login/oauth/access_token").UsingPost())
.RespondWith(
Expand Down
32 changes: 14 additions & 18 deletions src/SaveEnergy.Tests/Adapters/Outbound/RepositoryQueryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -250,31 +250,27 @@ CancellationToken cancellationToken
var queryString = request.RequestUri?.Query[1..] ?? "";
var queryParameters = queryString.Split(separator);

const string pageParameter = "page=";
var page = int.Parse(
Array
.Find(
queryParameters,
x => x.StartsWith(pageParameter, StringComparison.OrdinalIgnoreCase)
)
?.Split('=')[1] ?? "1"
);
var page = ParseQueryParameter("page=", queryParameters);
var perPage = ParseQueryParameter("per_page=", queryParameters);

var start = (page - 1) * perPage + 1;
var count = Math.Min(perPage, availableRepositories - start + 1);
count = Math.Max(count, 0);

const string perPageParameter = "per_page=";
var perPage = int.Parse(
return (start, count);
}

private static int ParseQueryParameter(string parameterName, string[] queryParameters)
{
var page = int.Parse(
Array
.Find(
queryParameters,
x => x.StartsWith(perPageParameter, StringComparison.OrdinalIgnoreCase)
x => x.StartsWith(parameterName, StringComparison.OrdinalIgnoreCase)
)
?.Split('=')[1] ?? "1"
);

var start = (page - 1) * perPage + 1;
var count = Math.Min(perPage, availableRepositories - start + 1);
count = Math.Max(count, 0);

return (start, count);
return page;
}
}
}
Expand Down

0 comments on commit eb0491a

Please sign in to comment.