Skip to content

Commit

Permalink
Merge pull request #201 from AdrianJSClark/200-always-use-cookies-fro…
Browse files Browse the repository at this point in the history
…m-cookiecontainer

Use Cookies Even Without "RestoreCookies"
  • Loading branch information
AdrianJSClark authored Apr 20, 2024
2 parents c476296 + 0d873a6 commit 253bcdf
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 7 deletions.
31 changes: 31 additions & 0 deletions src/Aydsko.iRacingData.UnitTests/CookiePersistenceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,35 @@ public async Task GivenOptionsWithARestoreFuncTheFuncIsCalledToGetTheCookiesAsyn
Assert.That(cookies, Has.One.Property(nameof(Cookie.Name)).EqualTo("irsso_members"));
Assert.That(cookies, Has.One.Property(nameof(Cookie.Name)).EqualTo("authtoken_members"));
}

[Test]
public async Task GivenACookieContainerWithCookiesAndNoRestoreOrSaveFunctionsThenTheCookiesAreUsed()
{
CookieContainer.Add(new Cookie("irsso_members", "one", "/", "members-ng.iracing.com"));
CookieContainer.Add(new Cookie("authtoken_members", "two", "/", "members-ng.iracing.com"));

var options = new iRacingDataClientOptions
{
Username = "[email protected]",
Password = "SuperSecretPassword",
RestoreCookies = null,
SaveCookies = null,
};

using var sut = new DataClient(HttpClient,
new TestLogger<DataClient>(),
options,
CookieContainer);

await MessageHandler.QueueResponsesAsync(nameof(CapturedResponseValidationTests.GetLookupsSuccessfulAsync), false).ConfigureAwait(false);
await sut.GetLookupsAsync(CancellationToken.None).ConfigureAwait(false);

Assert.That(sut.IsLoggedIn, Is.True);

var cookies = CookieContainer.GetAllCookies();

Assert.That(cookies, Has.Count.EqualTo(2));
Assert.That(cookies, Has.One.Property(nameof(Cookie.Name)).EqualTo("irsso_members"));
Assert.That(cookies, Has.One.Property(nameof(Cookie.Name)).EqualTo("authtoken_members"));
}
}
14 changes: 7 additions & 7 deletions src/Aydsko.iRacingData/DataClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1869,14 +1869,14 @@ private async Task LoginInternalAsync(CancellationToken cancellationToken)
&& options.RestoreCookies() is CookieCollection savedCookies)
{
cookieContainer.Add(savedCookies);
}

// Assume we're logged in if we have cookies for our target domain
if (cookieContainer.GetCookies(new Uri("https://members-ng.iracing.com")).Count > 0)
{
IsLoggedIn = true;
logger.LoginCookiesRestored(options.Username!);
return;
}
// Assume we're logged in if we have cookies for our target domain
if (cookieContainer.GetCookies(new Uri("https://members-ng.iracing.com")).Count > 0)
{
IsLoggedIn = true;
logger.LoginCookiesRestored(options.Username!);
return;
}

string? encodedHash = null;
Expand Down

0 comments on commit 253bcdf

Please sign in to comment.