public void ApiClient_SetByConstructor2_Equal() { var client = new ImgurClient("ClientId", "ClientSecret"); var endpoint = new MockEndpoint(client); Assert.Same(client, endpoint.ApiClient); }
public void HttpClientBaseAddress_WithImgurClient_IsImgurUrl() { var client = new ImgurClient("123", "1234"); var endpoint = new MockEndpoint(client); Assert.Equal(new Uri("https://api.imgur.com/3/"), endpoint.HttpClient.BaseAddress); }
public void HttpClient_SetByConstructor1_NotNull() { var client = new ImgurClient("123", "1234"); var endpoint = new MockEndpoint(client); Assert.NotNull(endpoint.HttpClient); }
public void HttpClientBaseAddress_WithMashapeClient_IsMashapeUrl() { var client = new MashapeClient("123", "1234", "12345"); var endpoint = new MockEndpoint(client); Assert.Equal(new Uri("https://imgur-apiv3.p.mashape.com/3/"), endpoint.HttpClient.BaseAddress); }
public void UpdateRateLimit_WithMashapeClientHeaders_Equal() { var client = new MashapeClient("123", "1234", "jhjhjhjh"); var endpoint = new MockEndpoint(client); var response = new HttpResponseMessage(); response.Headers.TryAddWithoutValidation("X-RateLimit-Requests-Limit", "123"); response.Headers.TryAddWithoutValidation("X-RateLimit-Requests-Remaining", "345"); endpoint.UpdateRateLimit(response.Headers); Assert.Equal(123, endpoint.ApiClient.RateLimit.ClientLimit); Assert.Equal(345, endpoint.ApiClient.RateLimit.ClientRemaining); response.Headers.Remove("X-RateLimit-Requests-Limit"); response.Headers.Remove("X-RateLimit-Requests-Remaining"); response.Headers.TryAddWithoutValidation("X-RateLimit-Requests-Limit", "122"); response.Headers.TryAddWithoutValidation("X-RateLimit-Requests-Remaining", "344"); endpoint.UpdateRateLimit(response.Headers); Assert.Equal(122, endpoint.ApiClient.RateLimit.ClientLimit); Assert.Equal(344, endpoint.ApiClient.RateLimit.ClientRemaining); }
public void _httpClient_SetByConstructor_AreSame() { var apiClient = new ApiClient("123"); var httpCLient = new HttpClient(); var endpoint = new MockEndpoint(apiClient, httpCLient); Assert.Same(httpCLient, endpoint._httpClient); }
public void SwitchClient_SetNull_ThrowsArgumentNullException() { var endpoint = new MockEndpoint(new ImgurClient("123", "1234")); var exception = Record.Exception(() => endpoint.SwitchClient(null)); Assert.NotNull(exception); Assert.IsType <ArgumentNullException>(exception); }
public void HttpClient_SetByConstructor2_AreSame() { var client = new ImgurClient("123", "1234"); var httpCLient = new HttpClient(); var endpoint = new MockEndpoint(client, httpCLient); Assert.Same(httpCLient, endpoint.HttpClient); }
public void SwitchClient_SetMashapeClient_True() { var client = new ImgurClient("123", "1234"); var mashapeClient = new MashapeClient("123444", "567567", "12354564"); var endpoint = new MockEndpoint(client); Assert.True(endpoint.ApiClient is IImgurClient); endpoint.SwitchClient(mashapeClient); Assert.True(endpoint.ApiClient is IMashapeClient); }
public void HttpClientWithImgurClient_SetByConstructor_HeadersEqual() { var client = new ImgurClient("123", "1234"); var endpoint = new MockEndpoint(client); var authHeader = endpoint.HttpClient.DefaultRequestHeaders.GetValues("Authorization").First(); var accept = endpoint.HttpClient.DefaultRequestHeaders.Accept.First(); Assert.Equal("Client-ID 123", authHeader); Assert.Equal("application/json", accept.MediaType); }
public void UpdateRateLimit_WithHeadersNull_ThrowArgumentException() { var client = new ImgurClient("123", "1234"); var endpoint = new MockEndpoint(client); var exception = Record.Exception(() => endpoint.UpdateRateLimit(null)); Assert.NotNull(exception); Assert.IsType <ArgumentNullException>(exception); }
public void ListAllTest() { var context = new MockContext <IEndpoint>(); context.Arrange(m => m.List("domainId", The <EndpointQuery> .Is(q => q.Size.Value == 1000), null)).Returns(new Endpoint[1]); var endpoint = new MockEndpoint(context); var items = endpoint.ListAll("domainId"); Assert.Equal(1, items.Count()); }
public void _httpClient_SetByConstructor_HasHeadersSet() { var apiClient = new ApiClient("123"); var endpoint = new MockEndpoint(apiClient, new HttpClient()); var authHeader = endpoint._httpClient.DefaultRequestHeaders.GetValues("Authorization").First(); var accept = endpoint._httpClient.DefaultRequestHeaders.Accept.First(); Assert.Equal("Client-ID 123", authHeader); Assert.Equal("application/json", accept.MediaType); }
public void ProcessEndpointBaseResponse_WithResponseNull_ThrowsImgurException() { var client = new ImgurClient("123", "1234"); var endpoint = new MockEndpoint(client); var exception = Record.Exception(() => endpoint.ProcessEndpointResponse <bool>(null)); Assert.NotNull(exception); Assert.IsType <ImgurException>(exception); }
public void FromUri() { var protocolVersions = new[] { new Version(1, 0), }; var configuration = new Mock <IConfiguration>(); { configuration.Setup(c => c.HasValueFor(It.IsAny <ConfigurationKey>())) .Returns(false); } var template = new NamedPipeDiscoveryChannelTemplate(configuration.Object); Func <ChannelTemplate, IDiscoveryChannelTemplate> templateBuilder = t => template; var diagnostics = new SystemDiagnostics((l, s) => { }, null); var translator = new DiscoveryChannelTranslator( protocolVersions, templateBuilder, diagnostics); var info = new VersionedChannelInformation { ProtocolVersion = new Version(1, 0), Address = new Uri("http://localhost/protocol/invalid") }; var receiver = new MockEndpoint( () => DiscoveryVersions.V1, () => new[] { new Version(1, 0), }, v => info); var uri = new Uri("net.pipe://localhost/pipe/discovery"); var host = new ServiceHost(receiver, uri); var binding = new NetNamedPipeBinding(NetNamedPipeSecurityMode.None) { TransferMode = TransferMode.Buffered, }; var address = string.Format("{0}_{1}", "ThroughNamedPipe", Process.GetCurrentProcess().Id); var endpoint = host.AddServiceEndpoint(typeof(IInformationEndpoint), binding, address); host.Open(); try { var receivedInfo = translator.FromUri(endpoint.ListenUri); Assert.IsNotNull(receivedInfo); Assert.AreEqual(info.ProtocolVersion, receivedInfo.Version); Assert.AreEqual(info.Address, receivedInfo.MessageAddress); Assert.IsNull(receivedInfo.DataAddress); } finally { host.Close(); } }
public void HttpClientWithImgurClientAndOAuth2Token_SetByConstructor_HeadersEqual() { var oAuth2Token = new OAuth2Token("access_token", "refresh_token", "bearer", "11345", "bob", 2419200); var client = new ImgurClient("123", "1234", oAuth2Token); var endpoint = new MockEndpoint(client); var authHeader = endpoint.HttpClient.DefaultRequestHeaders.GetValues("Authorization").First(); var accept = endpoint.HttpClient.DefaultRequestHeaders.Accept.First(); Assert.Equal("Bearer access_token", authHeader); Assert.Equal("application/json", accept.MediaType); }
public void RecentlyConnectedEndpointWithCommunicationFault() { var translator = new Mock <ITranslateVersionedChannelInformation>(); var translators = new[] { new Tuple <Version, ITranslateVersionedChannelInformation>(new Version(1, 0), translator.Object), }; var configuration = new Mock <IConfiguration>(); { configuration.Setup(c => c.HasValueFor(It.IsAny <ConfigurationKey>())) .Returns(false); } var template = new NamedPipeDiscoveryChannelTemplate(configuration.Object); Func <ChannelTemplate, IDiscoveryChannelTemplate> templateBuilder = t => template; var diagnostics = new SystemDiagnostics((l, s) => { }, null); var discovery = new ManualDiscoverySource( translators, templateBuilder, diagnostics); discovery.OnEndpointBecomingAvailable += (s, e) => Assert.Fail(); discovery.StartDiscovery(); var uri = new Uri("net.pipe://localhost/pipe/discovery"); var receiver = new MockEndpoint( () => { throw new ArgumentException(); }, null); var host = new ServiceHost(receiver, uri); var binding = new NetNamedPipeBinding(NetNamedPipeSecurityMode.None) { TransferMode = TransferMode.Buffered, }; var address = string.Format("{0}_{1}", "ThroughNamedPipe", Process.GetCurrentProcess().Id); var endpoint = host.AddServiceEndpoint(typeof(IBootstrapEndpoint), binding, address); host.Open(); try { discovery.RecentlyConnectedEndpoint( EndpointIdExtensions.CreateEndpointIdForCurrentProcess(), endpoint.ListenUri); } finally { host.Close(); } }
public void ProcessEndpointResponse_WithSuccessfulResponse_Equal() { var client = new ImgurClient("123", "1234"); var endpoint = new MockEndpoint(client); var response = new HttpResponseMessage { Content = new StringContent(MockGenericEndpointResponses.SuccessfulResponse) }; var result = endpoint.ProcessEndpointResponse <bool>(response); Assert.True(result); }
public void ProcessEndpointBaseResponse_WithStringResponseNull_ThrowsImgurException() { var client = new ImgurClient("123", "1234"); var endpoint = new MockEndpoint(client); var response = new HttpResponseMessage { Content = new StringContent("") }; var exception = Record.Exception(() => endpoint.ProcessEndpointResponse <bool>(response)); Assert.NotNull(exception); Assert.IsType <ImgurException>(exception); }
public void ProcessMashapeEndpointResponse_WithoutAuthorization_ThrowMashapeException() { var client = new MashapeClient("123", "567567", "1234"); var endpoint = new MockEndpoint(client); var response = new HttpResponseMessage { Content = new StringContent(MockErrors.MashapeError) }; var exception = Record.Exception(() => endpoint.ProcessEndpointResponse <RateLimit>(response)); Assert.NotNull(exception); Assert.IsType <MashapeException>(exception); }
public void ProcessImgurEndpointResponse_WithImgurCacheErrorResponse_ThrowsImgurException() { var client = new ImgurClient("123", "1234"); var endpoint = new MockEndpoint(client); var response = new HttpResponseMessage { Content = new StringContent("<html>") }; var exception = Record.Exception(() => endpoint.ProcessEndpointResponse <RateLimit>(response)); Assert.NotNull(exception); Assert.IsType <ImgurException>(exception); }
public async Task SendRequestAsync_WithMessageNull_ThrowsArgumentNullException() { var httpClient = new HttpClient(new MockHttpMessageHandler()); var endpoint = new MockEndpoint(new ImgurClient("123", "1234"), httpClient); var exception = await Record.ExceptionAsync( async() => await endpoint.SendRequestAsync <Image>(null).ConfigureAwait(false)) .ConfigureAwait(false); Assert.NotNull(exception); Assert.IsType <ArgumentNullException>(exception); }
private IOAuth2Token GetToken() { if (_token != null) { return(_token); } var endpoint = new MockEndpoint(new ImgurClient("a", "b")); var response = new HttpResponseMessage { Content = new StringContent(MockOAuth2EndpointResponses.GetTokenByCode) }; _token = endpoint.ProcessEndpointResponse <OAuth2Token>(response); return(_token); }
public void GetByNameTest() { var context = new MockContext <IEndpoint>(); context.Arrange(m => m.List("domainId", The <EndpointQuery> .Is(q => q.Size.Value == 1000), null)).Returns(new [] { new Endpoint { Id = "endpointId1", Name = "endpoint" } }); var endpoint = new MockEndpoint(context); var item = endpoint.GetByName("domainId", "endpoint"); Assert.Equal("endpointId1", item.Id); }
public async Task SendRequestAsync_WithNullMessage_ThrowsArgumentNullException() { var endpoint = new MockEndpoint(new ApiClient("123"), new HttpClient()); var exception = await Record.ExceptionAsync(async() => { await endpoint.SendRequestAsync <Image>(null); }); Assert.NotNull(exception); Assert.IsType <ArgumentNullException>(exception); var argNullException = (ArgumentNullException)exception; Assert.Equal("httpRequestMessage", argNullException.ParamName); }
public async void GetOrCreateAsyncTest3() { var context = new MockContext <IEndpoint>(); context.Arrange(m => m.List("domainId", The <EndpointQuery> .IsAnyValue, null)).Returns(new Endpoint[0]); context.Arrange(m => m.CreateAsync(The <CreateEndpointData> .Is(d => d.Name == "endpoint" && d.ApplicationId == "appId" && d.DomainId == "domainId"), null)).Returns(Task.FromResult("endpointId4")); context.Arrange(m => m.GetAsync("domainId", "endpointId4", null)).Returns(Task.FromResult(new Endpoint { Id = "endpointId4", Name = "endpoint" })); var endpoint = new MockEndpoint(context); var item = await endpoint.GetOrCreateAsync("appId", "domainId", "endpoint"); Assert.Equal("endpointId4", item.Id); }
public void SwitchClient_SetMashapeClientThenImgurClient_BaseAddressEqual() { var client = new ImgurClient("123", "1234"); var mashapeClient = new MashapeClient("123444", "567567", "12354564"); var endpoint = new MockEndpoint(mashapeClient); Assert.True(endpoint.ApiClient is IMashapeClient); Assert.Equal(new Uri("https://imgur-apiv3.p.mashape.com/3/"), endpoint.HttpClient.BaseAddress); endpoint.SwitchClient(client); Assert.True(endpoint.ApiClient is IImgurClient); Assert.Equal(new Uri("https://api.imgur.com/3/"), endpoint.HttpClient.BaseAddress); }
public async Task SendRequestInternalAsync_WithResponseNull_ThrowsImgurException() { var mockUrl = "http://example.org/test"; var mockResponse = new HttpResponseMessage(HttpStatusCode.InternalServerError); var httpClient = new HttpClient(new MockHttpMessageHandler(mockUrl, mockResponse)); var endpoint = new MockEndpoint(new ApiClient("123"), httpClient); var request = new HttpRequestMessage(HttpMethod.Get, "http://example.org/test"); var exception = await Record.ExceptionAsync(async() => { await endpoint.SendRequestInternalAsync <Image>(request); }); Assert.NotNull(exception); Assert.IsType <ImgurException>(exception); }
public void SwitchClient_SetMashapeClientAndOAuth2TokenThenImgurClient_HeadersEqual() { var oAuth2Token = new OAuth2Token("access_token", "refresh_token", "bearer", "11345", "bob", 2419200); var client = new ImgurClient("123", "1234", oAuth2Token); var mashapeClient = new MashapeClient("123444", "567567", "12354564"); var endpoint = new MockEndpoint(mashapeClient); Assert.True(endpoint.ApiClient is IMashapeClient); var authHeader = endpoint.HttpClient.DefaultRequestHeaders.GetValues("Authorization").First(); Assert.Equal("Client-ID 123444", authHeader); endpoint.SwitchClient(client); authHeader = endpoint.HttpClient.DefaultRequestHeaders.GetValues("Authorization").First(); Assert.True(endpoint.ApiClient is IImgurClient); Assert.Equal("Bearer access_token", authHeader); }
public async Task SendRequestAsync_WithMessage_Equal() { var constructorObjects = new object[2]; constructorObjects[0] = new ApiClient("123"); var mockUrl = "http://example.org/test"; var mockResponse = new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent(MockImageResponses.GetImage) }; var httpClient = new HttpClient(new MockHttpMessageHandler(mockUrl, mockResponse)); var endpoint = new MockEndpoint(new ApiClient("123"), httpClient); var request = new HttpRequestMessage(HttpMethod.Get, "http://example.org/test"); var image = await endpoint.SendRequestAsync <Image>(request); Assert.NotNull(image); }