public async Task SendIssueReportAsync(IssueReport report)
        {
            var appId = _propertyProvider.GetProperty("locco.github.appId");
            var accessToken = _propertyProvider.GetProperty("locco.github.token");
            var repoOwner = _propertyProvider.GetProperty("locco.github.owner");
            var repoName = _propertyProvider.GetProperty("locco.github.repository");

            // Support default proxy
            var proxy = WebRequest.DefaultWebProxy;
            var httpClient = new HttpClientAdapter(() => HttpMessageHandlerFactory.CreateDefault(proxy));
            var connection = new Connection(new ProductHeaderValue(appId), httpClient);

            if (proxy != null)
            {
                Log.Info(string.Format("Using proxy server '{0}' to connect to github!", proxy.GetProxy(new Uri("https://api.github.com/"))));
            }


            var client = new GitHubClient(connection)
            {
                Credentials = new Credentials(accessToken)
            };


            // Creates a new GitHub Issue
            var newIssue = new NewIssue(report.Title)
            {
                Body = MarkdownReportUtil.CompileBodyMarkdown(report, 220000),
            };
            newIssue.Labels.Add("bot");

            var issue = await client.Issue.Create(repoOwner, repoName, newIssue);
        }
        public async Task OkStatusShouldPassThrough()
        {
            var handler = CreateMockHttpHandler(new HttpResponseMessage(HttpStatusCode.OK));
            var adapter = new HttpClientAdapter(handler);

            var httpRequestMessage = CreateRequest(HttpMethod.Get);

            var response = await adapter.SendAsync(httpRequestMessage, new CancellationToken());

            Assert.Equal(response.StatusCode, HttpStatusCode.OK);
            Assert.Same(response.RequestMessage, httpRequestMessage);
        }
        public static GitHubClient Build()
        {
            var credentialStore = new InMemoryCredentialStore(Helper.Credentials);

            var httpClient = new HttpClientAdapter(Helper.Proxy);

            var connection = new Connection(
                new ProductHeaderValue("GitReleaseManager"),
                GitHubClient.GitHubApiUrl,
                credentialStore,
                httpClient,
                new SimpleJsonSerializer());

            return new GitHubClient(connection);
        }
        public async Task Status303ShouldRedirectChangeMethod()
        {
            var redirectResponse = new HttpResponseMessage(HttpStatusCode.SeeOther);
            redirectResponse.Headers.Location = new Uri("http://example.org/bar");

            var handler = CreateMockHttpHandler(redirectResponse, new HttpResponseMessage(HttpStatusCode.OK));
            var adapter = new HttpClientAdapter(handler);

            var httpRequestMessage = CreateRequest(HttpMethod.Post);

            var response = await adapter.SendAsync(httpRequestMessage, new CancellationToken());

            Assert.Equal(HttpMethod.Get, response.RequestMessage.Method);
            Assert.NotSame(response.RequestMessage, httpRequestMessage);
        }
        public static GitHubClient Create(Uri domain, Credentials credentials)
        {
            // Decorate the HttpClient
            //IHttpClient httpClient = new HttpClientAdapter();
            //httpClient = new OctokitCacheClient(httpClient);
            var client = new HttpClientAdapter(CreateMessageHandler);
            var httpClient = new OctokitNetworkClient(client, Locator.Current.GetService<INetworkActivityService>());

            var connection = new Connection(
                new ProductHeaderValue("CodeHub"),
                domain,
                new InMemoryCredentialStore(credentials),
                httpClient,
                new SimpleJsonSerializer());
            return new GitHubClient(connection);
        }
        public async Task RedirectWithSameHostShouldKeepAuthHeader()
        {
            var redirectResponse = new HttpResponseMessage(HttpStatusCode.Redirect);
            redirectResponse.Headers.Location = new Uri("http://example.org/bar");

            var handler = CreateMockHttpHandler(redirectResponse, new HttpResponseMessage(HttpStatusCode.OK));
            var adapter = new HttpClientAdapter(handler);

            var httpRequestMessage = CreateRequest(HttpMethod.Get);
            httpRequestMessage.Headers.Authorization = new AuthenticationHeaderValue("fooAuth", "aparam");

            var response = await adapter.SendAsync(httpRequestMessage, new CancellationToken());

            Assert.NotSame(response.RequestMessage, httpRequestMessage);
            Assert.Equal("fooAuth", response.RequestMessage.Headers.Authorization.Scheme);
        }
        public async Task CanCancelARequest()
        {
            var httpClient = new HttpClientAdapter(HttpMessageHandlerFactory.CreateDefault);
            var request = new Request
            {
                BaseAddress = new Uri("https://github.global.ssl.fastly.net/", UriKind.Absolute),
                Endpoint = new Uri("/images/icons/emoji/poop.png?v=5", UriKind.RelativeOrAbsolute),
                Method = HttpMethod.Get,
                Timeout = TimeSpan.FromMilliseconds(10)
            };

            var response = httpClient.Send(request, CancellationToken.None);

            await Task.Delay(TimeSpan.FromSeconds(2));

            Assert.True(response.IsCanceled);
        }
        public async Task CanDownloadImage()
        {
            var httpClient = new HttpClientAdapter();
            var request = new Request
            {
                BaseAddress = new Uri("https://github.global.ssl.fastly.net/", UriKind.Absolute),
                Endpoint = new Uri("/images/icons/emoji/poop.png?v=5", UriKind.RelativeOrAbsolute),
                AllowAutoRedirect = true,
                Method = HttpMethod.Get
            };

            var imageBytes = await httpClient.Send<byte[]>(request, CancellationToken.None);

            // Spot check some of dem bytes.
            Assert.Equal(137, imageBytes.BodyAsObject[0]);
            Assert.Equal(80, imageBytes.BodyAsObject[1]);
            Assert.Equal(78, imageBytes.BodyAsObject[2]);
            Assert.Equal(130, imageBytes.BodyAsObject.Last());
        }
        public async Task CanDownloadImage()
        {
            var httpClient = new HttpClientAdapter(HttpMessageHandlerFactory.CreateDefault);
            var request = new Request
            {
                BaseAddress = new Uri("https://github.global.ssl.fastly.net/", UriKind.Absolute),
                Endpoint = new Uri("/images/icons/emoji/poop.png?v=5", UriKind.RelativeOrAbsolute),
                Method = HttpMethod.Get
            };

            var response = await httpClient.Send(request, CancellationToken.None);

            // Spot check some of dem bytes.
            var imageBytes = (byte[])response.Body;
            Assert.Equal(137, imageBytes[0]);
            Assert.Equal(80, imageBytes[1]);
            Assert.Equal(78, imageBytes[2]);
            Assert.Equal(130, imageBytes.Last());
        }
        public async Task Exceed3RedirectsShouldReturn()
        {
            var redirectResponse = new HttpResponseMessage(HttpStatusCode.Found);
            redirectResponse.Headers.Location = new Uri("http://example.org/bar");

            var redirectResponse2 = new HttpResponseMessage(HttpStatusCode.Found);
            redirectResponse2.Headers.Location = new Uri("http://example.org/foo");
            
            var handler = CreateMockHttpHandler(redirectResponse, redirectResponse2);
            var adapter = new HttpClientAdapter(handler);

            var httpRequestMessage = CreateRequest(HttpMethod.Get);

            Assert.ThrowsAsync<InvalidOperationException>(
                () => adapter.SendAsync(httpRequestMessage, new CancellationToken()));
        }
        [InlineData(HttpStatusCode.TemporaryRedirect)]  // 307
        public async Task Status301ShouldRedirectPOSTWithBody(HttpStatusCode statusCode)
        {
            var redirectResponse = new HttpResponseMessage(statusCode);
            redirectResponse.Headers.Location = new Uri("http://example.org/bar");

            var handler = CreateMockHttpHandler(redirectResponse, new HttpResponseMessage(HttpStatusCode.OK));
            var adapter = new HttpClientAdapter(handler);

            var httpRequestMessage = CreateRequest(HttpMethod.Post);
            httpRequestMessage.Content = new StringContent("Hello World");

            var response = await adapter.SendAsync(httpRequestMessage, new CancellationToken());
            
            Assert.Equal(response.RequestMessage.Method, httpRequestMessage.Method);
            Assert.NotSame(response.RequestMessage, httpRequestMessage);
        }