From 42ea5059458aa701df7e6778e75e8543f253f526 Mon Sep 17 00:00:00 2001 From: VahidN Date: Wed, 1 Jul 2015 14:56:06 +0430 Subject: [PATCH] Improved NetworkStatus detection. --- GitHubFolderDownloader.Tests/ApiUrlTests.cs | 50 +++++++++++++++++++ GitHubFolderDownloader/App.config | 2 +- GitHubFolderDownloader/MainWindow.xaml | 2 +- .../Properties/AssemblyInfo.cs | 4 +- .../Toolkit/NetworkStatus.cs | 36 +++++++++++-- 5 files changed, 87 insertions(+), 7 deletions(-) create mode 100644 GitHubFolderDownloader.Tests/ApiUrlTests.cs diff --git a/GitHubFolderDownloader.Tests/ApiUrlTests.cs b/GitHubFolderDownloader.Tests/ApiUrlTests.cs new file mode 100644 index 0000000..ddc5e3d --- /dev/null +++ b/GitHubFolderDownloader.Tests/ApiUrlTests.cs @@ -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); + } + } +} \ No newline at end of file diff --git a/GitHubFolderDownloader/App.config b/GitHubFolderDownloader/App.config index 524f8c9..3c157cd 100644 --- a/GitHubFolderDownloader/App.config +++ b/GitHubFolderDownloader/App.config @@ -6,7 +6,7 @@ - + diff --git a/GitHubFolderDownloader/MainWindow.xaml b/GitHubFolderDownloader/MainWindow.xaml index c9db69c..05b0bb4 100644 --- a/GitHubFolderDownloader/MainWindow.xaml +++ b/GitHubFolderDownloader/MainWindow.xaml @@ -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"> diff --git a/GitHubFolderDownloader/Properties/AssemblyInfo.cs b/GitHubFolderDownloader/Properties/AssemblyInfo.cs index 94c01ed..ff6dc13 100644 --- a/GitHubFolderDownloader/Properties/AssemblyInfo.cs +++ b/GitHubFolderDownloader/Properties/AssemblyInfo.cs @@ -22,5 +22,5 @@ )] -[assembly: AssemblyVersion("1.1.0.0")] -[assembly: AssemblyFileVersion("1.1.0.0")] \ No newline at end of file +[assembly: AssemblyVersion("1.2.0.0")] +[assembly: AssemblyFileVersion("1.2.0.0")] \ No newline at end of file diff --git a/GitHubFolderDownloader/Toolkit/NetworkStatus.cs b/GitHubFolderDownloader/Toolkit/NetworkStatus.cs index 21f8239..66a6dd3 100644 --- a/GitHubFolderDownloader/Toolkit/NetworkStatus.cs +++ b/GitHubFolderDownloader/Toolkit/NetworkStatus.cs @@ -1,4 +1,6 @@ -using System.Net.NetworkInformation; +using System.Collections.Generic; +using System.Net; +using System.Net.NetworkInformation; namespace GitHubFolderDownloader.Toolkit { @@ -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 hosts) + { using (var ping = new Ping()) { foreach (var host in hosts) @@ -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 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; } }