Skip to content

Commit

Permalink
Add tests for the TransmissionClient project, minor changes to the te…
Browse files Browse the repository at this point in the history
…st projects
  • Loading branch information
aannenko committed Sep 19, 2024
1 parent 4ec721c commit ef97f35
Show file tree
Hide file tree
Showing 6 changed files with 466 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ private HttpResponseMessage SendInternal(TestRequest testRequest)
return response;
}

// My hope here is that none of the faked endpoints are expected to return "418 I'm a teapot".
// This is done in order not to occupy an exception type which could be asserted in the tests.
// My hope here is that none of the faked endpoints is expected to return "418 I'm a teapot".
// We could throw instead, but it would occupy an exception type which could be asserted in the tests.
return new() { StatusCode = (HttpStatusCode)418 };
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public sealed class TorrentWebPageClientTests
[Test]
public async Task FindMagnetUriAsync_FindsMagnetUri_IfGivenProperWebPage()
{
const string magnetUri = "magnet:?xt=urn:btih:EXAMPLEHASH&dn=Example+Name";
const string magnetUri = "magnet:?xt=urn:btih:3A81AAA70E75439D332C146ABDE899E546356BE2&dn=Example+Name";
const string webPageContentWithMagnet = $"""
<!DOCTYPE html>
<html lang="en">
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using System.Net;
using TransmissionManager.BaseTests.HttpClient;
using TransmissionManager.BaseTests.Options;
using TransmissionManager.Transmission.Options;
using TransmissionManager.Transmission.Services;

namespace TransmissionManager.Transmission.Tests;

[Parallelizable(ParallelScope.Self)]
public sealed class SessionHeaderHandlerTests
{
private const string _requestUri = "http://transmission:9091/transmission/rpc";
private const string _sessionHeaderName = "X-Transmission-Session-Id";
private const string _sessionHeaderValue = "TestSessionHeaderValue";

private static readonly Dictionary<string, string> _emptyHeaders = new()
{
[_sessionHeaderName] = string.Empty
};

private static readonly Dictionary<string, string> _filledHeaders = new()
{
[_sessionHeaderName] = _sessionHeaderValue
};

private static readonly FakeOptionsMonitor<SessionHeaderProviderOptions> _options = new(new()
{
SessionHeaderName = _sessionHeaderName
});

[Test]
public async Task SendAsync_HandlesSessionHeaderUpdate_WhenSessionHeaderIsInvalid()
{
var requestToResponseMap = new Dictionary<TestRequest, TestResponse>
{
[new(HttpMethod.Get, new(_requestUri), _emptyHeaders)] = new(HttpStatusCode.Conflict, _filledHeaders),
[new(HttpMethod.Get, new(_requestUri), _filledHeaders)] = new(HttpStatusCode.OK)
};

var (client, provider) = CreateClientProviderPair(requestToResponseMap);

Assert.That(provider.SessionHeaderValue, Is.Empty);

var result = await client.GetAsync(_requestUri);

Assert.Multiple(() =>
{
Assert.That(result.IsSuccessStatusCode);
Assert.That(provider.SessionHeaderValue, Is.EqualTo(_sessionHeaderValue));
});
}

private static (HttpClient client, SessionHeaderProvider provider) CreateClientProviderPair(
IReadOnlyDictionary<TestRequest, TestResponse> requestToResponseMap)
{
var provider = new SessionHeaderProvider(_options);
var fakeMessageHandler = new FakeHttpMessageHandler(requestToResponseMap);
var sessionHandler = new SessionHeaderHandler(provider) { InnerHandler = fakeMessageHandler };
return (new(sessionHandler), provider);
}
}
Loading

0 comments on commit ef97f35

Please sign in to comment.