Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Cookies Even Without "RestoreCookies" #201

Merged
merged 1 commit into from
Apr 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading