Skip to content

Commit

Permalink
Merge pull request #7 from fernandezja/fix-webaddress-contains-dash-t…
Browse files Browse the repository at this point in the history
…hrow-error

Fix webaddress contains dash throw error
  • Loading branch information
Killeroo authored Oct 29, 2023
2 parents 25529e1 + adc93a0 commit 027de71
Show file tree
Hide file tree
Showing 12 changed files with 281 additions and 10 deletions.
21 changes: 21 additions & 0 deletions HttpPing.Tests/EnvironmentServiceForTestPurpose.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using HttpPing.Interfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HttpPing.Tests
{
internal class EnvironmentServiceForTestPurpose : IEnvironmentService
{
private bool _hasExited = false;

public bool HasExited => _hasExited;

public void Exit(int exitCode)
{
_hasExited = true;
}
}
}
75 changes: 75 additions & 0 deletions HttpPing.Tests/HttpPing.Tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="..\packages\MSTest.TestAdapter.2.2.10\build\net45\MSTest.TestAdapter.props" Condition="Exists('..\packages\MSTest.TestAdapter.2.2.10\build\net45\MSTest.TestAdapter.props')" />
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{6006C2E4-372B-4375-904E-A4E0D67BC2F2}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>HttpPing.Tests</RootNamespace>
<AssemblyName>HttpPing.Tests</AssemblyName>
<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<ProjectTypeGuids>{3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">15.0</VisualStudioVersion>
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
<ReferencePath>$(ProgramFiles)\Common Files\microsoft shared\VSTT\$(VisualStudioVersion)\UITestExtensionPackages</ReferencePath>
<IsCodedUITest>False</IsCodedUITest>
<TestProjectType>UnitTest</TestProjectType>
<NuGetPackageImportStamp>
</NuGetPackageImportStamp>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="Microsoft.VisualStudio.TestPlatform.TestFramework, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\MSTest.TestFramework.2.2.10\lib\net45\Microsoft.VisualStudio.TestPlatform.TestFramework.dll</HintPath>
</Reference>
<Reference Include="Microsoft.VisualStudio.TestPlatform.TestFramework.Extensions, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\MSTest.TestFramework.2.2.10\lib\net45\Microsoft.VisualStudio.TestPlatform.TestFramework.Extensions.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
</ItemGroup>
<ItemGroup>
<Compile Include="EnvironmentServiceForTestPurpose.cs" />
<Compile Include="ProgramTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\HttpPing\HttpPing.csproj">
<Project>{d4d36105-4ac4-4ad6-92dd-5ef9a05af312}</Project>
<Name>HttpPing</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(VSToolsPath)\TeamTest\Microsoft.TestTools.targets" Condition="Exists('$(VSToolsPath)\TeamTest\Microsoft.TestTools.targets')" />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
<ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('..\packages\MSTest.TestAdapter.2.2.10\build\net45\MSTest.TestAdapter.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\MSTest.TestAdapter.2.2.10\build\net45\MSTest.TestAdapter.props'))" />
<Error Condition="!Exists('..\packages\MSTest.TestAdapter.2.2.10\build\net45\MSTest.TestAdapter.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\MSTest.TestAdapter.2.2.10\build\net45\MSTest.TestAdapter.targets'))" />
</Target>
<Import Project="..\packages\MSTest.TestAdapter.2.2.10\build\net45\MSTest.TestAdapter.targets" Condition="Exists('..\packages\MSTest.TestAdapter.2.2.10\build\net45\MSTest.TestAdapter.targets')" />
</Project>
69 changes: 69 additions & 0 deletions HttpPing.Tests/ProgramTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.IO;
using System.Text;

namespace HttpPing.Tests
{
[TestClass]
public class ProgramTests
{
private EnvironmentServiceForTestPurpose _environmentServiceForTestPurpose { get; set; }
private StringBuilder _consoleOutput { get; set; }

[TestInitialize]
public void Init()
{
_environmentServiceForTestPurpose = new EnvironmentServiceForTestPurpose();

_consoleOutput = new StringBuilder();
Console.SetOut(new StringWriter(_consoleOutput));
_consoleOutput.Clear();
}

[TestMethod]
public void When_params_are_empty_should_return_usage_test()
{
var expectedValue = "USAGE: HttpPing web_address [-d] [-t] [-ts] [-https] [-n count] [-i interval] [-l] [-nc] [-r redirectCount] \r\n";

string[] parameters = new string[]{};

web_ping.Program.Main(parameters, _environmentServiceForTestPurpose);


Assert.IsTrue(_consoleOutput.ToString().Equals(expectedValue));
}


[TestMethod]
public void When_params_webaddress_simple_count_1_interval_500ms_should_response_test()
{
var expectedValue = "Sending HTTP requests to [https://www.starwars.com]:\r\nResponse from www.starwars.com: Code=200:OK Size=-1\r\nResponse from www.starwars.com: Code=200:OK Size=-1\r\n";

string[] parameters = new string[] { "https://www.starwars.com",
"-n", "1",
"-i", "500" };

web_ping.Program.Main(parameters, _environmentServiceForTestPurpose);

Assert.IsTrue(_consoleOutput.ToString().Equals(expectedValue));
}


[TestMethod]
public void When_params_webaddress_contains_dash_should_not_return_error_test()
{
//var expectedValue = "Incorrect argument found\r\nUSAGE: HttpPing web_address [-d] [-t] [-ts] [-https] [-n count] [-i interval] [-l] [-nc] [-r redirectCount] \r\n";
var expectedValue = "Sending HTTP requests to [https://www.star-wars.com]:\r\nResponse from www.star-wars.com: Code=200:OK Size=-1\r\nResponse from www.star-wars.com: Code=200:OK Size=-1\r\n";


string[] parameters = new string[] { "https://www.star-wars.com",
"-n", "1",
"-i", "500" };

web_ping.Program.Main(parameters, _environmentServiceForTestPurpose);

Assert.IsTrue(_consoleOutput.ToString().Equals(expectedValue));
}
}
}
20 changes: 20 additions & 0 deletions HttpPing.Tests/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

[assembly: AssemblyTitle("HttpPing.Tests")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("HttpPing.Tests")]
[assembly: AssemblyCopyright("Copyright © 2023")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

[assembly: ComVisible(false)]

[assembly: Guid("6006c2e4-372b-4375-904e-a4e0d67bc2f2")]

// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
5 changes: 5 additions & 0 deletions HttpPing.Tests/packages.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="MSTest.TestAdapter" version="2.2.10" targetFramework="net452" />
<package id="MSTest.TestFramework" version="2.2.10" targetFramework="net452" />
</packages>
13 changes: 11 additions & 2 deletions HttpPing.sln
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 14
VisualStudioVersion = 14.0.25420.1
# Visual Studio Version 17
VisualStudioVersion = 17.8.34219.65
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HttpPing", "HttpPing\HttpPing.csproj", "{D4D36105-4AC4-4AD6-92DD-5EF9A05AF312}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HttpPing.Tests", "HttpPing.Tests\HttpPing.Tests.csproj", "{6006C2E4-372B-4375-904E-A4E0D67BC2F2}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -15,8 +17,15 @@ Global
{D4D36105-4AC4-4AD6-92DD-5EF9A05AF312}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D4D36105-4AC4-4AD6-92DD-5EF9A05AF312}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D4D36105-4AC4-4AD6-92DD-5EF9A05AF312}.Release|Any CPU.Build.0 = Release|Any CPU
{6006C2E4-372B-4375-904E-A4E0D67BC2F2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{6006C2E4-372B-4375-904E-A4E0D67BC2F2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6006C2E4-372B-4375-904E-A4E0D67BC2F2}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6006C2E4-372B-4375-904E-A4E0D67BC2F2}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {93382169-0208-4C2F-B561-E66A44CAC3EB}
EndGlobalSection
EndGlobal
6 changes: 3 additions & 3 deletions HttpPing/App.config
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2"/>
</startup>
</configuration>
</configuration>
16 changes: 16 additions & 0 deletions HttpPing/EnvironmentService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using HttpPing.Interfaces;
using System;
using System.Collections.Generic;

namespace HttpPing
{
internal class EnvironmentService : IEnvironmentService
{
public bool HasExited { get; }

public void Exit(int exitCode)
{
Environment.Exit(exitCode);
}
}
}
3 changes: 3 additions & 0 deletions HttpPing/HttpPing.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<TargetFrameworkProfile />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
Expand Down Expand Up @@ -43,6 +44,8 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="EnvironmentService.cs" />
<Compile Include="Interfaces\IEnvironmentService.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
Expand Down
14 changes: 14 additions & 0 deletions HttpPing/Interfaces/IEnvironmentService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HttpPing.Interfaces
{
internal interface IEnvironmentService
{
bool HasExited { get; }
void Exit(int exitCode);
}
}
44 changes: 39 additions & 5 deletions HttpPing/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
using System.Net;
using System.Threading;
using System.Linq;
using System.Runtime.CompilerServices;
using HttpPing.Interfaces;
using HttpPing;


namespace web_ping
{
Expand All @@ -28,13 +32,24 @@ class Program

private static string resolvedAddress = "";

private static void Main(string[] args)
private static IEnvironmentService EnvironmentService;


/// <summary>
/// This method is the entry point of the program, with the test parameter environment it wrapper exit for test purpose
/// </summary>
/// <param name="args">Array of strings that contains the command-line arguments passed to the program</param>
/// <param name="environmentService">Environment (wrapper for test)</param>
internal static void Main(string[] args, IEnvironmentService environmentService)
{
EnvironmentService = environmentService;

// Arguments check
if (args.Length == 0)
{
Console.WriteLine(Usage);
Environment.Exit(0);
EnvironmentService.Exit(0);
return;
}

// Save console colors
Expand All @@ -47,6 +62,7 @@ private static void Main(string[] args)
for (int count = 0; count < args.Length; count++)
{
string arg = args[count];

switch (arg)
{
case "-?":
Expand Down Expand Up @@ -104,9 +120,13 @@ private static void Main(string[] args)
ForceHttps = true;
break;
default:
if (arg.Contains("-"))
throw new ArgumentException();
if (count > 0)
{
if (arg.Contains("-"))
throw new ArgumentException();
}
break;

}
}
}
Expand All @@ -131,6 +151,8 @@ private static void Main(string[] args)
Exit(e.GetType().ToString());
}

if (EnvironmentService.HasExited) return;

// Find address
string query = "";
if (Uri.IsWellFormedUriString(args.First(), UriKind.RelativeOrAbsolute))
Expand All @@ -140,6 +162,7 @@ private static void Main(string[] args)
else
{
Exit("Could not find URL/Web address");
if (EnvironmentService.HasExited) return;
}

// Modify any exting scheme if we are forcing https
Expand Down Expand Up @@ -171,6 +194,17 @@ private static void Main(string[] args)
// Results?
}


/// <summary>
/// This method is the entry point of the program
/// </summary>
/// <param name="args">Array of strings that contains the command-line arguments passed to the program</param>
internal static void Main(string[] args)
{
Main(args, new EnvironmentService());
}


// SO: https://stackoverflow.com/questions/27108264/c-sharp-how-to-properly-make-a-http-web-get-request
private static void HttpRequestLoop(string query)
{
Expand Down Expand Up @@ -331,7 +365,7 @@ private static void Exit(string message = null)
Error(message);

Console.WriteLine(Usage);
Environment.Exit(1);
EnvironmentService.Exit(1);
}
}
}
Loading

0 comments on commit 027de71

Please sign in to comment.