Skip to content

Changes to Status codes #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,6 @@
/SimpleHttpServer.WebApp/obj/Debug
/packages/log4net.2.0.5
/SimpleHttpServer/obj/Debug
/SimpleHttpServer.ViewEngine/obj/Debug
/.vs/config
/SimpleHttpServer.ViewEngine/bin/Debug
3 changes: 1 addition & 2 deletions SimpleHttpServer.App/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ static void Main(string[] args)
return new HttpResponse()
{
ContentAsUTF8 = "Hello from SimpleHttpServer",
ReasonPhrase = "OK",
StatusCode = "200"
HttpStatusCode = HttpStatusCode.Ok
};
}
},
Expand Down
35 changes: 14 additions & 21 deletions SimpleHttpServer.Test/HttpProcessorTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,13 @@ public void HandleClient_GivenGETRequest_Returns200Response()
return new HttpResponse()
{
ContentAsUTF8 = "Hello World",
ReasonPhrase = "OK",
StatusCode = "200"
HttpStatusCode = HttpStatusCode.Ok
};
}
});

httpProcessor.HandleClient(null);
Assert.AreEqual("200", httpProcessor.Response.StatusCode);
Assert.AreEqual(HttpStatusCode.Ok, httpProcessor.Response.HttpStatusCode);
}

[TestMethod]
Expand All @@ -55,7 +54,7 @@ public void HandleClient_GivenGETRequestThrowsException_Returns500Response()
});

httpProcessor.HandleClient(null);
Assert.AreEqual("500", httpProcessor.Response.StatusCode);
Assert.AreEqual(HttpStatusCode.InternalServerError, httpProcessor.Response.HttpStatusCode);
}

[TestMethod]
Expand All @@ -74,14 +73,13 @@ public void HandleClient_GivenGETRequestWhereNotRouted_Returns404Response()
return new HttpResponse()
{
ContentAsUTF8 = "Hello World",
ReasonPhrase = "OK",
StatusCode = "200"
HttpStatusCode = HttpStatusCode.Ok
};
}
});

httpProcessor.HandleClient(null);
Assert.AreEqual("404", httpProcessor.Response.StatusCode);
Assert.AreEqual(HttpStatusCode.NotFound, httpProcessor.Response.HttpStatusCode);
}

[TestMethod]
Expand All @@ -100,16 +98,15 @@ public void HandleClient_GivenGETRequestWithQueryString_Returns200Response()
return new HttpResponse()
{
ContentAsUTF8 = "Hello World",
ReasonPhrase = "OK",
StatusCode = "200"
HttpStatusCode = HttpStatusCode.Ok
};
}
});

httpProcessor.HandleClient(null);


Assert.AreEqual("200", httpProcessor.Response.StatusCode);
Assert.AreEqual(HttpStatusCode.Ok, httpProcessor.Response.HttpStatusCode);
}

[TestMethod]
Expand All @@ -128,15 +125,14 @@ public void Handle_GivenPOSTRequestWhereNotRoutedReturns404Response()
return new HttpResponse()
{
ContentAsUTF8 = "Hello World",
ReasonPhrase = "OK",
StatusCode = "200"
HttpStatusCode = HttpStatusCode.Ok
};
}
});

httpProcessor.HandleClient(null);

Assert.AreEqual("404", httpProcessor.Response.StatusCode);
Assert.AreEqual(HttpStatusCode.NotFound, httpProcessor.Response.HttpStatusCode);
}

[TestMethod]
Expand All @@ -155,15 +151,14 @@ public void Handle_GivenPOSTRequestWhereNotRoutedButGETIsRouted_Returns405Respon
return new HttpResponse()
{
ContentAsUTF8 = "Hello World",
ReasonPhrase = "OK",
StatusCode = "200"
HttpStatusCode = HttpStatusCode.Ok
};
}
});

httpProcessor.HandleClient(null);

Assert.AreEqual("405", httpProcessor.Response.StatusCode);
Assert.AreEqual(HttpStatusCode.MethodNotAllowed, httpProcessor.Response.HttpStatusCode);
}

[TestMethod]
Expand All @@ -173,27 +168,25 @@ public void Handle_GivenPOSTRequest_Returns200Response()
{
Method = "POST",
Url = "/Test/Example",
Content = "Hello World"
ContentAsUTF8 = "Hello World"
}, new Route()
{
UrlRegex = "^/Test/Example$",
Method = "POST",
Callable = (HttpRequest request) =>
{
Assert.AreEqual("Hello World", request.Content);

return new HttpResponse()
{
ContentAsUTF8 = "Hello World",
ReasonPhrase = "OK",
StatusCode = "200"
HttpStatusCode = HttpStatusCode.Ok
};
}
});

httpProcessor.HandleClient(null);

Assert.AreEqual("200", httpProcessor.Response.StatusCode);
Assert.AreEqual(HttpStatusCode.Ok, httpProcessor.Response.HttpStatusCode);
}
}
}
36 changes: 36 additions & 0 deletions SimpleHttpServer.ViewEngine/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("SimpleHttpServer.ViewEngine")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("SimpleHttpServer.ViewEngine")]
[assembly: AssemblyCopyright("Copyright © 2016")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("7c6a12ae-13a2-4320-975a-8493ed08991a")]

// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
55 changes: 55 additions & 0 deletions SimpleHttpServer.ViewEngine/SimpleHttpServer.ViewEngine.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<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>{7C6A12AE-13A2-4320-975A-8493ED08991A}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>SimpleHttpServer.ViewEngine</RootNamespace>
<AssemblyName>SimpleHttpServer.ViewEngine</AssemblyName>
<TargetFrameworkVersion>v4.5.1</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<TargetFrameworkProfile />
</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="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="SimpleViewEngine.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>
20 changes: 20 additions & 0 deletions SimpleHttpServer.ViewEngine/SimpleViewEngine.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace SimpleHttpServer.ViewEngine
{
public class SimpleViewEngine
{
public string Render(string html, Dictionary<string, string> data)
{

html = Regex.Replace(html, "{{(?<name>((?!}).)*)}}", x => data[x.Groups["name"].Value]);

return html;
}
}
}
26 changes: 23 additions & 3 deletions SimpleHttpServer.WebApp/App.config
Original file line number Diff line number Diff line change
@@ -1,6 +1,26 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/>
</startup>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,Log4net"/>
</configSections>
<log4net>
<root>
<level value="DEBUG"/>
<appender-ref ref="LogFileAppender"/>
</root>
<appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender">
<param name="File" value="log-file.txt"/>
<param name="AppendToFile" value="true"/>
<rollingStyle value="Size"/>
<maxSizeRollBackups value="10"/>
<maximumFileSize value="10MB"/>
<staticLogFileName value="true"/>
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline"/>
</layout>
</appender>
</log4net>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.1"/>
</startup>
</configuration>
23 changes: 23 additions & 0 deletions SimpleHttpServer.WebApp/Controllers/Contact.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using SimpleHttpServer.Extensions;
using SimpleHttpServer.Models;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SimpleHttpServer.WebApp.Controllers
{
class Contact
{

public HttpResponse SendEmail(HttpRequest request)
{


return HttpBuilder.MovedPermanently("/");

}
}
}
19 changes: 18 additions & 1 deletion SimpleHttpServer.WebApp/Controllers/Home.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System;
using SimpleHttpServer.Models;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
Expand All @@ -8,5 +10,20 @@ namespace SimpleHttpServer.WebApp.Controllers
{
class Home
{
public HttpResponse Index(HttpRequest request)
{
return new HttpResponse()
{
ContentAsUTF8 = File.ReadAllText("Views/Home/Index.html"),
HttpStatusCode = HttpStatusCode.Ok
};

}

public HttpResponse Error(HttpRequest request)
{
throw new Exception("Can't call this action.");

}
}
}
2 changes: 2 additions & 0 deletions SimpleHttpServer.WebApp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ class Program
{
static void Main(string[] args)
{
log4net.Config.XmlConfigurator.Configure();

HttpServer httpServer = new HttpServer(8080, Routes.GET);

Thread thread = new Thread(new ThreadStart(httpServer.Listen));
Expand Down
Loading