Exemplo n.º 1
0
        public void EnsureSpecificApiVersionCommandsUsesCorrectVersion()
        {
            MockApiCommand cmd = new MockApiCommand() { BaseApiVersion = "2.x/" };
            IMusicClientSettings settings = new MockMusicClientSettings(ClientId, Country, null);

            IApiUriBuilder builder = new ApiUriBuilder();
            Uri uri = builder.BuildUri(cmd, settings, null);

            Assert.IsTrue(uri.ToString().Contains("2.x/"), "Expected the correct version to be included in the URI");
        }
Exemplo n.º 2
0
        public void EnsureBlankLanguageCommandsDoNotIncludeLanguage()
        {
            MockApiCommand       cmd      = new MockApiCommand();
            IMusicClientSettings settings = new MockMusicClientSettings(ClientId, Country, null);

            IApiUriBuilder builder = new ApiUriBuilder();
            Uri            uri     = builder.BuildUri(cmd, settings, null);

            Assert.IsFalse(uri.ToString().Contains("lang="), "Expected the language code not to be included in the URI");
        }
Exemplo n.º 3
0
        public void EnsureBlankLanguageCommandsDoNotIncludeLanguage()
        {
            MockApiCommand cmd = new MockApiCommand();
            IMusicClientSettings settings = new MockMusicClientSettings(ClientId, Country, null);

            IApiUriBuilder builder = new ApiUriBuilder();
            Uri uri = builder.BuildUri(cmd, settings, null);

            Assert.IsFalse(uri.ToString().Contains("lang="), "Expected the language code not to be included in the URI");
        }
Exemplo n.º 4
0
        public async Task ServerTimeOffsetIsSetForMulitpleRequests()
        {
            const int          EPSILON = 25; ////25 hours
            IApiRequestHandler handler = new ApiRequestHandler(new TestHttpUriBuilder(new Uri("http://www.nokia.com")));

            var command = new MockApiCommand(null, HttpMethod.Head);
            await handler.SendRequestAsync(command, new MockMusicClientSettings("test", null, null), null, command.HandleRawData, null, null);

            await handler.SendRequestAsync(command, new MockMusicClientSettings("test", null, null), null, command.HandleRawData, null, null);

            Assert.IsTrue(Math.Abs(handler.ServerTimeUtc.Subtract(DateTime.Now).TotalHours) <= EPSILON);
        }
Exemplo n.º 5
0
        public void EnsureSpecificApiVersionCommandsUsesCorrectVersion()
        {
            MockApiCommand cmd = new MockApiCommand()
            {
                BaseApiVersion = "2.x/"
            };
            IMusicClientSettings settings = new MockMusicClientSettings(ClientId, Country, null);

            IApiUriBuilder builder = new ApiUriBuilder();
            Uri            uri     = builder.BuildUri(cmd, settings, null);

            Assert.IsTrue(uri.ToString().Contains("2.x/"), "Expected the correct version to be included in the URI");
        }
Exemplo n.º 6
0
        public async Task SslCertFailureResultsInSendFailureException()
        {
            // Arrange
            var mockHttpClientProxy = new MockHttpClientRequestProxy();

            mockHttpClientProxy.SetupException(new WebException("Message", WebExceptionStatus.SendFailure));
            IApiRequestHandler handler = new ApiRequestHandler(new TestHttpUriBuilder(new Uri("http://www.nokia.com")), null, mockHttpClientProxy);
            var rawMessage             = "This is the raw body of the message";
            var command = new MockApiCommand(rawMessage, HttpMethod.Head, true);

            // Act
            var response = await handler.SendRequestAsync(command, new MockMusicClientSettings("test", null, null), null, command.HandleRawData, null, null);

            // Assert
            Assert.IsFalse(response.Succeeded);
            Assert.IsTrue(response.Error is SendFailureException);
        }
Exemplo n.º 7
0
        public async Task ServerOffsetGotSet(int clientDateOffset, bool expectedResult)
        {
            const int          EPSILON = 25; ////25 hours
            IApiRequestHandler handler = new ApiRequestHandler(new TestHttpUriBuilder(new Uri("http://www.nokia.com")));

            var command = new MockApiCommand(null, HttpMethod.Head);

            await handler.SendRequestAsync(
                command,
                new MockMusicClientSettings("test", null, null),
                null,
                command.HandleRawData,
                null,
                null);

            Assert.AreEqual(expectedResult, Math.Abs(handler.ServerTimeUtc.Subtract(DateTime.Now.AddHours(clientDateOffset)).TotalHours) <= EPSILON);
        }
Exemplo n.º 8
0
        public async Task TimeoutDuringEndRequestStreamGivesNoStatusCode()
        {
            IApiRequestHandler handler = new ApiRequestHandler(new TestHttpUriBuilder(new Uri("http://localhost:8123/")));

            var command = new MockApiCommand();

            var result = await handler.SendRequestAsync(
                command,
                new MockMusicClientSettings("test", null, null),
                null,
                command.HandleRawData,
                null,
                null);

            Assert.IsNotNull(result, "Expected a result");
            Assert.IsNotNull(result.Error, "Expected an error, status code:" + result.StatusCode);
            Assert.IsNull(result.Result, "Expected no result object");
            Assert.IsNull(result.StatusCode, "Expected no status code");
        }
Exemplo n.º 9
0
        public async Task AttemptToGetStatusCodeFromRealPost()
        {
            IApiRequestHandler handler = new ApiRequestHandler(new TestHttpUriBuilder(new Uri("http://www.nokia.com")));

            var command = new MockApiCommand();

            var result = await handler.SendRequestAsync(
                command,
                new MockMusicClientSettings("test", null, null),
                null,
                command.HandleRawData,
                null,
                null);

            Assert.IsNotNull(result, "Expected a result");
            Assert.IsNotNull(result.Error, "Expected an error");
            Assert.IsNull(result.Result, "Expected no result object");
            Assert.IsNotNull(result.StatusCode, "Expected a status code - this test can fail when you run tests with no internet connection!");
        }
Exemplo n.º 10
0
        public async Task NullResponseWithoutMixRadioHeaderRaisesNetworkLimitedException()
        {
            IApiRequestHandler handler = new ApiRequestHandler(new TestHttpUriBuilder(new Uri("http://www.nokia.com")));

            var command = new MockApiCommand(null, HttpMethod.Head);

            var result = await handler.SendRequestAsync(
                command,
                new MockMusicClientSettings("test", null, null),
                null,
                command.HandleRawData,
                null,
                null);

            Assert.IsNotNull(result, "Expected a result");
            Assert.IsNotNull(result.Error, "Expected an error");
            Assert.IsNull(result.Result, "Expected no result object");
            Assert.IsNotNull(result.StatusCode, "Expected a status code - this test can fail when you run tests with no internet connection!");
            Assert.IsInstanceOf <NetworkLimitedException>(result.Error, "Expected a NetworkLimitedException");
        }
Exemplo n.º 11
0
        public async Task ContentIsGzippedIfCommandSpecifies()
        {
            // Arrange
            var mockHttpClientProxy    = new MockHttpClientRequestProxy();
            IApiRequestHandler handler = new ApiRequestHandler(new TestHttpUriBuilder(new Uri("http://www.nokia.com")), null, mockHttpClientProxy);
            var    rawMessage          = "This is the raw body of the message";
            string actualMessage;
            var    command = new MockApiCommand(rawMessage, HttpMethod.Head, true);

            // Act
            await handler.SendRequestAsync(command, new MockMusicClientSettings("test", null, null), null, command.HandleRawData, null, null);

            using (Stream decompressedStream = new GZipStream(await mockHttpClientProxy.RequestMessage.Content.ReadAsStreamAsync(), CompressionMode.Decompress, true))
            {
                using (TextReader reader = new StreamReader(decompressedStream, Encoding.UTF8))
                {
                    actualMessage = reader.ReadToEnd();
                }
            }

            // Assert
            Assert.AreEqual(rawMessage, actualMessage);
            Assert.AreEqual("gzip", mockHttpClientProxy.RequestMessage.Content.Headers.ContentEncoding.First());
        }
Exemplo n.º 12
0
        public async Task CancellationDoesCancelApiCall()
        {
            var cancellationTokenSource = new CancellationTokenSource();

            IApiRequestHandler handler = new ApiRequestHandler(new TestHttpUriBuilder(new Uri("http://www.nokia.com")));

            cancellationTokenSource.Cancel();

            var command = new MockApiCommand();

            var result = await handler.SendRequestAsync(
                command,
                new MockMusicClientSettings("test", null, null),
                null,
                command.HandleRawData,
                null,
                cancellationTokenSource.Token);

            Assert.IsNotNull(result, "Expected a result");
            Assert.IsNotNull(result.Error, "Expected an error, status code:" + result.StatusCode);
            Assert.IsInstanceOf <ApiCallCancelledException>(result.Error, "Expected an ApiCallCancelledException");
            Assert.IsNull(result.Result, "Expected no result object");
            Assert.IsNull(result.StatusCode, "Expected no status code");
        }
Exemplo n.º 13
0
        public async Task TimeoutDuringEndRequestStreamGivesNoStatusCode()
        {
            IApiRequestHandler handler = new ApiRequestHandler(new TestHttpUriBuilder(new Uri("http://localhost:8123/")));

            var command = new MockApiCommand();

            var result = await handler.SendRequestAsync(
                command,
                new MockMusicClientSettings("test", null, null),
                null,
                command.HandleRawData,
                null,
                null);

            Assert.IsNotNull(result, "Expected a result");
            Assert.IsNotNull(result.Error, "Expected an error, status code:" + result.StatusCode);
            Assert.IsNull(result.Result, "Expected no result object");
            Assert.IsNull(result.StatusCode, "Expected no status code");
        }
Exemplo n.º 14
0
        public async Task AttemptToGetStatusCodeWithCustomUserAgent()
        {
            IApiRequestHandler handler = new ApiRequestHandler(new TestHttpUriBuilder(new Uri("http://www.nokia.com")), "MusicAPI-Tests/1.0.0.0");

            var command = new MockApiCommand();

            var result = await handler.SendRequestAsync(
                command,
                new MockMusicClientSettings("test", null, null),
                null,
                command.HandleRawData,
                null,
                null);

            // Wait for the response and parsing...
            Assert.IsNotNull(result, "Expected a result");
            Assert.IsNotNull(result.Error, "Expected an error");
            Assert.IsNull(result.Result, "Expected no result object");
            Assert.IsNotNull(result.StatusCode, "Expected a status code - this test can fail when you run tests with no internet connection!");
        }
Exemplo n.º 15
0
        public async Task CancellationDoesCancelApiCall()
        {
            var cancellationTokenSource = new CancellationTokenSource();

            IApiRequestHandler handler = new ApiRequestHandler(new TestHttpUriBuilder(new Uri("http://www.nokia.com")));

            cancellationTokenSource.Cancel();

            var command = new MockApiCommand();

            var result = await handler.SendRequestAsync(
                command,
                new MockMusicClientSettings("test", null, null),
                null,
                command.HandleRawData,
                null,
                cancellationTokenSource.Token);

            Assert.IsNotNull(result, "Expected a result");
            Assert.IsNotNull(result.Error, "Expected an error, status code:" + result.StatusCode);
            Assert.IsInstanceOf<ApiCallCancelledException>(result.Error, "Expected an ApiCallCancelledException");
            Assert.IsNull(result.Result, "Expected no result object");
            Assert.IsNull(result.StatusCode, "Expected no status code");
        }
Exemplo n.º 16
0
        public async Task NullResponseWithoutMixRadioHeaderRaisesNetworkLimitedException()
        {
            IApiRequestHandler handler = new ApiRequestHandler(new TestHttpUriBuilder(new Uri("http://www.nokia.com")));

            var command = new MockApiCommand(null, HttpMethod.Head);

            var result = await handler.SendRequestAsync(
                command,
                new MockMusicClientSettings("test", null, null),
                null,
                command.HandleRawData,
                null,
                null);

            Assert.IsNotNull(result, "Expected a result");
            Assert.IsNotNull(result.Error, "Expected an error");
            Assert.IsNull(result.Result, "Expected no result object");
            Assert.IsNotNull(result.StatusCode, "Expected a status code - this test can fail when you run tests with no internet connection!");
            Assert.IsInstanceOf<NetworkLimitedException>(result.Error, "Expected a NetworkLimitedException");
        }
Exemplo n.º 17
0
        public async Task ServerOffsetGotSet(int clientDateOffset, bool expectedResult)
        {
            const int EPSILON = 25; ////25 hours
            IApiRequestHandler handler = new ApiRequestHandler(new TestHttpUriBuilder(new Uri("http://www.nokia.com")));

            var command = new MockApiCommand(null, HttpMethod.Head);

            await handler.SendRequestAsync(
                command,
                new MockMusicClientSettings("test", null, null),
                null,
                command.HandleRawData,
                null,
                null);

            Assert.AreEqual(expectedResult, Math.Abs(handler.ServerTimeUtc.Subtract(DateTime.Now.AddHours(clientDateOffset)).TotalHours) <= EPSILON);
        }
Exemplo n.º 18
0
        public async Task ServerTimeOffsetIsSetForMulitpleRequests()
        {
            const int EPSILON = 25; ////25 hours
            IApiRequestHandler handler = new ApiRequestHandler(new TestHttpUriBuilder(new Uri("http://www.nokia.com")));

            var command = new MockApiCommand(null, HttpMethod.Head);
            await handler.SendRequestAsync(command, new MockMusicClientSettings("test", null, null), null, command.HandleRawData, null, null);
            await handler.SendRequestAsync(command, new MockMusicClientSettings("test", null, null), null, command.HandleRawData, null, null);
            
            Assert.IsTrue(Math.Abs(handler.ServerTimeUtc.Subtract(DateTime.Now).TotalHours) <= EPSILON);
        }
Exemplo n.º 19
0
        public async Task SslCertFailureResultsInSendFailureException()
        {
            // Arrange
            var mockHttpClientProxy = new MockHttpClientRequestProxy();
            mockHttpClientProxy.SetupException(new WebException("Message", WebExceptionStatus.SendFailure));
            IApiRequestHandler handler = new ApiRequestHandler(new TestHttpUriBuilder(new Uri("http://www.nokia.com")), null, mockHttpClientProxy);
            var rawMessage = "This is the raw body of the message";
            var command = new MockApiCommand(rawMessage, HttpMethod.Head, true);

            // Act
            var response = await handler.SendRequestAsync(command, new MockMusicClientSettings("test", null, null), null, command.HandleRawData, null, null);

            // Assert
            Assert.IsFalse(response.Succeeded);
            Assert.IsTrue(response.Error is SendFailureException);
        }
Exemplo n.º 20
0
        public async Task ContentIsGzippedIfCommandSpecifies()
        {
            // Arrange
            var mockHttpClientProxy = new MockHttpClientRequestProxy();
            IApiRequestHandler handler = new ApiRequestHandler(new TestHttpUriBuilder(new Uri("http://www.nokia.com")), null, mockHttpClientProxy);
            var rawMessage = "This is the raw body of the message";
            string actualMessage;
            var command = new MockApiCommand(rawMessage, HttpMethod.Head, true);

            // Act
            await handler.SendRequestAsync(command, new MockMusicClientSettings("test", null, null), null, command.HandleRawData, null, null);
            using (Stream decompressedStream = new GZipStream(await mockHttpClientProxy.RequestMessage.Content.ReadAsStreamAsync(), CompressionMode.Decompress, true))
            {
                using (TextReader reader = new StreamReader(decompressedStream, Encoding.UTF8))
                {
                    actualMessage = reader.ReadToEnd();
                }    
            }

            // Assert
            Assert.AreEqual(rawMessage, actualMessage);
            Assert.AreEqual("gzip", mockHttpClientProxy.RequestMessage.Content.Headers.ContentEncoding.First());
        }