Skip to content
This repository has been archived by the owner on Dec 9, 2020. It is now read-only.

Commit

Permalink
Improved NetworkStatus detection.
Browse files Browse the repository at this point in the history
  • Loading branch information
VahidN committed Jul 1, 2015
1 parent a241172 commit 42ea505
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 7 deletions.
50 changes: 50 additions & 0 deletions GitHubFolderDownloader.Tests/ApiUrlTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using GitHubFolderDownloader.Core;
using GitHubFolderDownloader.ViewModels;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace GitHubFolderDownloader.Tests
{
[TestClass]
public class ApiUrlTests
{
[TestMethod]
public void TestSetApiSegmentsWithUrlsContainingSpaces()
{
var viewModel = new MainWindowViewModel
{
GuiModelData =
{
RepositoryFolderFullUrl =
"https://github.com/EdxStudents/W3C_HTML5.1x/tree/master/About%20W3C%20and%20the%20Web"
}
};

Assert.AreEqual("EdxStudents", viewModel.GuiModelData.RepositoryOwner);
Assert.AreEqual("W3C_HTML5.1x", viewModel.GuiModelData.RepositoryName);
Assert.AreEqual("About W3C and the Web", viewModel.GuiModelData.RepositorySubDir);

var actual = new ApiUrl(viewModel.GuiModelData).GetApiUrl(viewModel.GuiModelData.RepositorySubDir);
Assert.AreEqual("https://api.github.com/repos/EdxStudents/W3C_HTML5.1x/contents/About%20W3C%20and%20the%20Web", actual);
}

[TestMethod]
public void TestSetApiSegmentsWithNormalUrls()
{
var viewModel = new MainWindowViewModel
{
GuiModelData =
{
RepositoryFolderFullUrl =
"https://github.com/VahidN/KendoUI-Samples/tree/master/KendoUI02_MVC/Controllers"
}
};

Assert.AreEqual("VahidN", viewModel.GuiModelData.RepositoryOwner);
Assert.AreEqual("KendoUI-Samples", viewModel.GuiModelData.RepositoryName);
Assert.AreEqual("KendoUI02_MVC/Controllers", viewModel.GuiModelData.RepositorySubDir);

var actual = new ApiUrl(viewModel.GuiModelData).GetApiUrl(viewModel.GuiModelData.RepositorySubDir);
Assert.AreEqual("https://api.github.com/repos/VahidN/KendoUI-Samples/contents/KendoUI02_MVC/Controllers", actual);
}
}
}
2 changes: 1 addition & 1 deletion GitHubFolderDownloader/App.config
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
</appSettings>
<system.net>
<defaultProxy>
<proxy usesystemdefault="False" bypassonlocal="True"/>
<proxy usesystemdefault="True" bypassonlocal="True"/>
</defaultProxy>
<connectionManagement>
<add address="*" maxconnection="100"/>
Expand Down
2 changes: 1 addition & 1 deletion GitHubFolderDownloader/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
xmlns:behaviors="clr-namespace:GitHubFolderDownloader.Behaviors"
xmlns:viewModels="clr-namespace:GitHubFolderDownloader.ViewModels"
xmlns:views="clr-namespace:GitHubFolderDownloader.Views"
Title="GitHub Folder Downloader V1.1"
Title="GitHub Folder Downloader V1.2"
Background="#FF393939" FontFamily="Tahoma"
WindowStartupLocation="CenterScreen"
Height="542" Width="720">
Expand Down
4 changes: 2 additions & 2 deletions GitHubFolderDownloader/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@
)]


[assembly: AssemblyVersion("1.1.0.0")]
[assembly: AssemblyFileVersion("1.1.0.0")]
[assembly: AssemblyVersion("1.2.0.0")]
[assembly: AssemblyFileVersion("1.2.0.0")]
36 changes: 33 additions & 3 deletions GitHubFolderDownloader/Toolkit/NetworkStatus.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.Net.NetworkInformation;
using System.Collections.Generic;
using System.Net;
using System.Net.NetworkInformation;

namespace GitHubFolderDownloader.Toolkit
{
Expand All @@ -9,8 +11,13 @@ public static bool IsConnectedToInternet(int timeoutPerHostMillis = 1000, string
var networkAvailable = NetworkInterface.GetIsNetworkAvailable();
if (!networkAvailable) return false;

var hosts = hostsToPing ?? new[] { "www.google.com" };
var hosts = hostsToPing ?? new[] { "http://www.google.com" };

return canPing(timeoutPerHostMillis, hosts) || canOpenRead(hosts);
}

private static bool canPing(int timeoutPerHostMillis, IEnumerable<string> hosts)
{
using (var ping = new Ping())
{
foreach (var host in hosts)
Expand All @@ -21,10 +28,33 @@ public static bool IsConnectedToInternet(int timeoutPerHostMillis = 1000, string
if (pingReply != null && pingReply.Status == IPStatus.Success)
return true;
}
catch { }
catch
{
}
}
}
return false;
}

private static bool canOpenRead(IEnumerable<string> hosts)
{
foreach (var host in hosts)
{
try
{
using (var webClient = new WebClient())
{
webClient.Headers.Add("user-agent", Downloader.UA);
using (var stream = webClient.OpenRead(host))
{
return true;
}
}
}
catch
{
}
}
return false;
}
}
Expand Down

0 comments on commit 42ea505

Please sign in to comment.