Beispiel #1
0
        public async Task Request_WithValidRequest_ShouldCallSendAsyncOnHttpClient()
        {
            // Setup
            var          httpClient = Substitute.For <HttpClient, IDisposable>();
            IHttpService service    = new JsonHttpService(httpClient);

            var         httpRequestMessage = new HttpRequestMessage();
            HttpRequest request            = new HttpRequest(httpRequestMessage);

            var expectedResponse = new TestClass {
                Name = "Test class"
            };
            var originalResponse = new HttpResponseMessage
            {
                Content = new StringContent(JsonConvert.SerializeObject(expectedResponse))
            };
            var response = new HttpResponse <TestClass>();

            await response.SetResponseAsync(Guid.NewGuid(), originalResponse, default(CancellationToken));

            httpClient
            .SendAsync(httpRequestMessage, default(CancellationToken))
            .Returns(originalResponse);

            // Perform action 'Request'
            await service.RequestAsync <TestObject>(request, default(CancellationToken));

            // Assert that 'ShouldCallSendAsyncOnHttpClient' = 'WithValidRequest'
            httpClient
            .Received()
            .SendAsync(httpRequestMessage, default(CancellationToken));
        }
Beispiel #2
0
        public void Constructor_WithNoParams_ShouldConstruct()
        {
            // Setup

            // Perform action 'Constructor'
            IHttpService actual = new JsonHttpService();

            // Assert that 'ShouldConstruct' = 'WithNoParams'
            Assert.NotNull(actual);
        }
Beispiel #3
0
        public void Constructor_WithHttpClientParam_ShouldConstruct()
        {
            // Setup
            var httpClient = Substitute.For <HttpClient>();

            // Perform action 'Constructor'
            IHttpService actual = new JsonHttpService(httpClient);

            // Assert that 'ShouldConstruct' = 'WithHttpClientParam'
            Assert.NotNull(actual);
        }
Beispiel #4
0
        public void Dispose_WithNoParameters_ShouldDisposeHttpClient()
        {
            // Setup
            var          httpClient = Substitute.For <HttpClient, IDisposable>();
            IHttpService service    = new JsonHttpService(httpClient);

            // Perform action 'Dispose'
            (service as IDisposable).Dispose();

            // Assert that 'ShouldDisposeHttpClient' = 'WithNoParameters'
            httpClient
            .Received()
            .Dispose();
        }
Beispiel #5
0
        public void Request_WithInvalidRequest_ShouldThrowArgumentNullException()
        {
            // Setup
            var          httpClient = Substitute.For <HttpClient, IDisposable>();
            IHttpService service    = new JsonHttpService(httpClient);
            HttpRequest  request    = null;

            // Perform action 'Request'
            var exception = Assert.ThrowsAsync <ArgumentNullException>(async() =>
            {
                await service.RequestAsync <TestObject>(request, default(CancellationToken));
            });

            // Assert that 'ShouldThrowArgumentNullException' = 'WithInvalidRequest'
        }
Beispiel #6
0
        /// <summary>
        /// Construct a default instance of the Plex service.
        /// </summary>
        /// <param name="username">Username used to get auth token.</param>
        /// <param name="password">Username used to get auth token.</param>
        /// <returns>Plex service instance.</returns>
        public static PlexMediaService GetDefaultInstance(string username, string password)
        {
            var plexConfig = new PlexMediaServerConfig
            {
                PlexAuthenticationRequestUser = new BasicAuth
                {
                    Username = username,
                    Password = password
                },
                BasicPlexHeaders = new PlexBasicRequestHeaders()
            };
            IHttpService httpService = new JsonHttpService(new HttpClient());
            IHeaderConstructor <PlexBasicRequestHeaders> plexBasicHeaderConstructorService = new PlexBasicHeaderConstructorService();
            IPlexAuthenticator authenticator = new PlexTvAuthenticator(
                httpService,
                plexBasicHeaderConstructorService,
                new BasicAuthHeaderConstructorService(), plexConfig);
            IPlexServerSettingsProvider settingsProvider = new PlexServerPreferencesProviderService(httpService, plexBasicHeaderConstructorService, plexConfig);

            return(new PlexMediaService(plexConfig, httpService, authenticator, settingsProvider, plexBasicHeaderConstructorService));
        }