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"); }
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"); }
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"); }
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); }
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"); }
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); }
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); }
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"); }
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!"); }
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"); }
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()); }
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"); }
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"); }
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!"); }
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"); }
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"); }
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); }
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); }
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); }
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()); }