Ejemplo n.º 1
0
        public void ApiClient_SetByConstructor2_Equal()
        {
            var client   = new ImgurClient("ClientId", "ClientSecret");
            var endpoint = new MockEndpoint(client);

            Assert.Same(client, endpoint.ApiClient);
        }
Ejemplo n.º 2
0
        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);
        }
Ejemplo n.º 3
0
        public void HttpClient_SetByConstructor1_NotNull()
        {
            var client   = new ImgurClient("123", "1234");
            var endpoint = new MockEndpoint(client);

            Assert.NotNull(endpoint.HttpClient);
        }
Ejemplo n.º 4
0
        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);
        }
Ejemplo n.º 5
0
        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);
        }
Ejemplo n.º 6
0
        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);
        }
Ejemplo n.º 7
0
        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);
        }
Ejemplo n.º 8
0
        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);
        }
Ejemplo n.º 9
0
        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);
        }
Ejemplo n.º 10
0
        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);
        }
Ejemplo n.º 11
0
        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());
        }
Ejemplo n.º 13
0
        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);
        }
Ejemplo n.º 14
0
        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);
        }
Ejemplo n.º 15
0
        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();
            }
        }
Ejemplo n.º 16
0
        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);
        }
Ejemplo n.º 17
0
        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();
            }
        }
Ejemplo n.º 18
0
        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);
        }
Ejemplo n.º 19
0
        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);
        }
Ejemplo n.º 20
0
        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);
        }
Ejemplo n.º 21
0
        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);
        }
Ejemplo n.º 22
0
        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);
        }
Ejemplo n.º 23
0
        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);
        }
Ejemplo n.º 25
0
        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);
        }
Ejemplo n.º 27
0
        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);
        }
Ejemplo n.º 28
0
        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);
        }
Ejemplo n.º 29
0
        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);
        }
Ejemplo n.º 30
0
        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);
        }