diff --git a/.gitignore b/.gitignore index 5b85ade..9d62ca3 100644 --- a/.gitignore +++ b/.gitignore @@ -1,9 +1,227 @@ -/_ReSharper.uhttpsharp/ -/*.6.0.ReSharper.user -/*.suo -/packages/* -*/bin/* -*/obj/* +## Ignore Visual Studio temporary files, build results, and +## files generated by popular Visual Studio add-ons. + +# User-specific files +*.suo +*.user +*.userosscache +*.sln.docstates + +# User-specific files (MonoDevelop/Xamarin Studio) +*.userprefs + +# Build results +[Dd]ebug/ +[Dd]ebugPublic/ +[Rr]elease/ +[Rr]eleases/ +x64/ +build/ +bld/ +[Bb]in/ +[Oo]bj/ + +# Visual Studio 2015 cache/options directory +*.vs/ + +# Roslyn cache directories +*.ide/ + +# MSTest test Results +[Tt]est[Rr]esult*/ +[Bb]uild[Ll]og.* + +# NUnit +*.VisualState.xml +TestResult.xml + +# Build Results of an ATL Project +[Dd]ebugPS/ +[Rr]eleasePS/ +dlldata.c + +*_i.c +*_p.c +*.ilk +*.meta +*.obj +*.pch +*.pdb +*.pgc +*.pgd +*.rsp +*.sbr +*.tlb +*.tli +*.tlh +*.tmp +*.tmp_proj +*.log +*.vspscc +*.vssscc +.builds +*.pidb +*.log +*.scc + +# Chutzpah Test files +_Chutzpah* + +# Visual C++ cache files +ipch/ +*.aps +*.ncb +*.opensdf +*.sdf +*.cachefile + +# Visual Studio profiler *.psess *.vsp -*.nupkg \ No newline at end of file +*.vspx + +# TFS 2012 Local Workspace +$tf/ + +# Guidance Automation Toolkit +*.gpState + +# ReSharper is a .NET coding add-in +_ReSharper*/ +*.[Rr]e[Ss]harper +*.DotSettings.user + +# JustCode is a .NET coding addin-in +.JustCode + +# TeamCity is a build add-in +_TeamCity* + +# DotCover is a Code Coverage Tool +*.dotCover + +# NCrunch +*.ncrunch* +.*crunch*.local.xml + +# MightyMoose +*.mm.* +AutoTest.Net/ + +# Web workbench (sass) +.sass-cache/ + +# Installshield output folder +[Ee]xpress/ + +# DocProject is a documentation generator add-in +DocProject/buildhelp/ +DocProject/Help/*.HxT +DocProject/Help/*.HxC +DocProject/Help/*.hhc +DocProject/Help/*.hhk +DocProject/Help/*.hhp +DocProject/Help/Html2 +DocProject/Help/html + +# Click-Once directory +publish/ + +# Publish Web Output +*.[Pp]ublish.xml +*.azurePubxml +## TODO: Comment the next line if you want to checkin your +## web deploy settings but do note that will include unencrypted +## passwords +*.pubxml +*.publishproj + +# NuGet Packages Directory +packages/* +*.nupkg + +## TODO: If the tool you use requires repositories.config +## uncomment the next line +#!packages/repositories.config + +# Enable "build/" folder in the NuGet Packages folder since +# NuGet packages use it for MSBuild targets. +# This line needs to be after the ignore of the build folder +# (and the packages folder if the line above has been uncommented) +!packages/build/ + +# PVC Build packages (https://github.com/pvcbuild/pvc) +pvc-packages/* + +# Windows Azure Build Output +csx +*.build.csdef + +# Windows Store app package directory +AppPackages/ + +# Others +sql/ +*.Cache +ClientBin/ +[Ss]tyle[Cc]op.* +~$* +*~ +*.dbmdl +*.dbproj.schemaview +*.[Pp]ublish.xml +*.pfx +*.publishsettings +node_modules/ +bower_components/ + +# RIA/Silverlight projects +Generated_Code/ + +# Backup & report files from converting an old project file to a newer +# Visual Studio version. Backup files are not needed, because we have git ;-) +_UpgradeReport_Files/ +Backup*/ +UpgradeLog*.XML +UpgradeLog*.htm + +# SQL Server files +App_Data/*.mdf +App_Data/*.ldf + +# Business Intelligence projects +*.rdl.data +*.bim.layout +*.bim_*.settings + +# Microsoft Fakes +FakesAssemblies/ + +# Node.js Tools for Visual Studio +.ntvs_analysis.dat + +# Visual Studio 6 build log +*.plg + +# Visual Studio 6 workspace options file +*.opt + +# Paket Package Manager Main exe +.paket/paket.exe + +# ========================= +# Windows detritus +# ========================= + +# Windows image file caches +Thumbs.db +ehthumbs.db + +# Folder config file +Desktop.ini + +# Recycle Bin used on file shares +$RECYCLE.BIN/ + +# Mac crap +.DS_Store diff --git a/README.md b/README.md index 369e655..bfcaa6f 100644 --- a/README.md +++ b/README.md @@ -126,4 +126,4 @@ A sample for usage : * Use it * Open Issues -* Fork and Push requests +* Fork and Push requests \ No newline at end of file diff --git a/uhttpsharp-demo/Controllers/DemoController.cs b/uhttpsharp-demo/Controllers/DemoController.cs index f9a667e..0ce90cd 100644 --- a/uhttpsharp-demo/Controllers/DemoController.cs +++ b/uhttpsharp-demo/Controllers/DemoController.cs @@ -27,9 +27,9 @@ public JsonController(int id) } [HttpMethod(HttpMethods.Post)] - public Task Post([FromBody] Question question) + public IControllerResponse Post([FromBody] Question question) { - return Response.Render(HttpResponseCode.Ok, question); + return Response.Render(HttpResponseCode.Ok, question).Result; } public IPipeline Pipeline { @@ -54,9 +54,9 @@ public Task Post([FromPost("a")] MyRequest request, [FromHe } [Indexer] - public async Task Get(IHttpContext context, int id) + public Task Get(IHttpContext context, int id) { - return new MyController(id); + return Task.FromResult(new MyController(id)); } } public class MyRequest : IValidate @@ -75,7 +75,7 @@ class BaseController : IController [HttpMethod(HttpMethods.Get)] public Task Get() { - return Response.Render(HttpResponseCode.Ok, new {Hello="Base!", Kaki = Enumerable.Range(0, 10000)}); + return Response.Render(HttpResponseCode.Ok, new { Hello = "Base!", Kaki = Enumerable.Range(0, 10000) }); } [HttpMethod(HttpMethods.Post)] @@ -89,16 +89,32 @@ public virtual IPipeline Pipeline get { return new EmptyPipeline(); } } - public IController Derived { + public IController Derived + { get { return new DerivedController(); } } } class DerivedController : BaseController { - protected new Task Get() + [HttpMethod(HttpMethods.Get)] + public new Task Get() { return Response.Render(HttpResponseCode.Ok, new { Hello = "Derived!" }); } + + [Indexer(0)] + public Task Indexer(IHttpContext context, int hey) + { + return Task.FromResult(this); + } + + + [Indexer(1)] + public Task Indexer(IHttpContext context, string hey) + { + return Task.FromResult(this); + } + } } diff --git a/uhttpsharp-demo/Handlers/TimingHandler.cs b/uhttpsharp-demo/Handlers/TimingHandler.cs index 91ab79e..f37504e 100644 --- a/uhttpsharp-demo/Handlers/TimingHandler.cs +++ b/uhttpsharp-demo/Handlers/TimingHandler.cs @@ -2,14 +2,14 @@ using System.Diagnostics; using System.Reflection; using System.Threading.Tasks; -using log4net; using uhttpsharp; +using uhttpsharp.Logging; namespace uhttpsharpdemo.Handlers { public class TimingHandler : IHttpRequestHandler { - private static readonly ILog Logger = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); + private static readonly ILog Logger = LogProvider.GetCurrentClassLogger(); public async Task Handle(IHttpContext context, Func next) { diff --git a/uhttpsharp-demo/Program.cs b/uhttpsharp-demo/Program.cs index b155899..476f14a 100644 --- a/uhttpsharp-demo/Program.cs +++ b/uhttpsharp-demo/Program.cs @@ -18,10 +18,12 @@ using System; using System.Collections.Concurrent; +using System.Dynamic; using System.Globalization; using System.Net; using System.Net.Sockets; using System.Threading.Tasks; +using log4net.Config; using uhttpsharp; using uhttpsharp.Handlers; using uhttpsharp.Handlers.Compression; @@ -37,7 +39,7 @@ internal static class Program { private static void Main() { - log4net.Config.XmlConfigurator.Configure(); + XmlConfigurator.Configure(); //var serverCertificate = X509Certificate.CreateFromCertFile(@"TempCert.cer"); @@ -47,9 +49,14 @@ private static void Main() //httpServer.Use(new ListenerSslDecorator(new TcpListenerAdapter(new TcpListener(IPAddress.Loopback, 443)), serverCertificate)); //httpServer.Use(new SessionHandler(() => DateTime.Now)); + httpServer.Use(new ExceptionHandler()); + httpServer.Use(new SessionHandler(() => new ExpandoObject(), TimeSpan.FromMinutes(20))); + httpServer.Use(new BasicAuthenticationHandler("Hohoho", "username", "password5")); + httpServer.Use(new ControllerHandler(new DerivedController(), new ModelBinder(new ObjectActivator()), new JsonView())); + httpServer.Use(new CompressionHandler(DeflateCompressor.Default, GZipCompressor.Default)); - httpServer.Use(new ControllerHandler(new BaseController(), new JsonModelBinder(), new JsonView())); + httpServer.Use(new ControllerHandler(new BaseController(), new JsonModelBinder(), new JsonView(), StringComparer.OrdinalIgnoreCase)); httpServer.Use(new HttpRouter().With(string.Empty, new IndexHandler()) .With("about", new AboutHandler()) .With("Assets", new AboutHandler()) @@ -126,66 +133,5 @@ public System.Threading.Tasks.Task Handle(IHttpContext context, Func : IHttpRequestHandler - { - private readonly Func _sessionFactory; - - private static readonly Random RandomGenerator = new Random(); - - private class SessionHolder - { - private readonly TSession _session; - private DateTime _lastAccessTime = DateTime.Now; - - public TSession Session - { - get - { - _lastAccessTime = DateTime.Now; - return _session; - } - } - - public DateTime LastAccessTime - { - get - { - return _lastAccessTime; - } - } - - public SessionHolder(TSession session) - { - _session = session; - } - } - - private readonly ConcurrentDictionary _sessions = new ConcurrentDictionary(); - - public SessionHandler(Func sessionFactory) - { - _sessionFactory = sessionFactory; - } - - public System.Threading.Tasks.Task Handle(IHttpContext context, Func next) - { - - string sessId; - if (!context.Cookies.TryGetByName("SESSID", out sessId)) - { - sessId = RandomGenerator.Next().ToString(CultureInfo.InvariantCulture); - context.Cookies.Upsert("SESSID", sessId); - } - - var session = _sessions.GetOrAdd(sessId, CreateSession); - - context.State.Session = session; - - return next(); - } - private TSession CreateSession(string sessionId) - { - return _sessionFactory(); - } - } + } \ No newline at end of file diff --git a/uhttpsharp-demo/app.config b/uhttpsharp-demo/app.config index aeaafa2..bfdad18 100644 --- a/uhttpsharp-demo/app.config +++ b/uhttpsharp-demo/app.config @@ -31,10 +31,14 @@ - - + + + + + + diff --git a/uhttpsharp-demo/packages.config b/uhttpsharp-demo/packages.config index a8e7b37..639150b 100644 --- a/uhttpsharp-demo/packages.config +++ b/uhttpsharp-demo/packages.config @@ -1,5 +1,5 @@  - + \ No newline at end of file diff --git a/uhttpsharp-demo/uhttpsharp.Demo.csproj b/uhttpsharp-demo/uhttpsharp.Demo.csproj index 7563180..fb380e5 100644 --- a/uhttpsharp-demo/uhttpsharp.Demo.csproj +++ b/uhttpsharp-demo/uhttpsharp.Demo.csproj @@ -39,12 +39,13 @@ false - + + False ..\packages\log4net.2.0.3\lib\net40-full\log4net.dll False - ..\packages\Newtonsoft.Json.6.0.3\lib\net45\Newtonsoft.Json.dll + ..\packages\Newtonsoft.Json.6.0.8\lib\net45\Newtonsoft.Json.dll diff --git a/uhttpsharp.Tests/HttpMethodProviderCacheTests.cs b/uhttpsharp.Tests/HttpMethodProviderCacheTests.cs index 85a8f53..4ea0d43 100644 --- a/uhttpsharp.Tests/HttpMethodProviderCacheTests.cs +++ b/uhttpsharp.Tests/HttpMethodProviderCacheTests.cs @@ -1,9 +1,9 @@ -using Moq; +using NSubstitute; using NUnit.Framework; +using Shouldly; namespace uhttpsharp.Tests { - [TestFixture] public class HttpMethodProviderCacheTests { private const string MethodName = "Hello World"; @@ -17,14 +17,14 @@ private static IHttpMethodProvider GetTarget(IHttpMethodProvider child) public void Should_Call_Child_With_Right_Parameters() { // Arrange - var mock = new Mock(); - var target = GetTarget(mock.Object); + var mock = Substitute.For(); + var target = GetTarget(mock); // Act target.Provide(MethodName); // Assert - mock.Verify(m => m.Provide(MethodName), Times.Once); + mock.Received(1).Provide(MethodName); } [Test] @@ -33,24 +33,24 @@ public void Should_Return_Same_Child_Value() // Arrange const HttpMethods expectedMethod = HttpMethods.Post; - var mock = new Mock(); - var target = GetTarget(mock.Object); + var mock = Substitute.For(); + mock.Provide(MethodName).Returns(expectedMethod); + var target = GetTarget(mock); - mock.Setup(m => m.Provide(MethodName)).Returns(expectedMethod); // Act var actual = target.Provide(MethodName); // Assert - Assert.That(actual, Is.EqualTo(expectedMethod)); + actual.ShouldBe(expectedMethod); } [Test] public void Should_Cache_The_Value() { // Arrange - var mock = new Mock(); - var target = GetTarget(mock.Object); + var mock = Substitute.For(); + var target = GetTarget(mock); // Act target.Provide(MethodName); @@ -58,7 +58,7 @@ public void Should_Cache_The_Value() target.Provide(MethodName); // Assert - mock.Verify(m => m.Provide(MethodName), Times.Once); + mock.Received(1).Provide(MethodName); } } diff --git a/uhttpsharp.Tests/HttpMethodProviderTests.cs b/uhttpsharp.Tests/HttpMethodProviderTests.cs index 4589c3a..8a75c45 100644 --- a/uhttpsharp.Tests/HttpMethodProviderTests.cs +++ b/uhttpsharp.Tests/HttpMethodProviderTests.cs @@ -1,10 +1,10 @@ using System; using System.Collections.Generic; +using Shouldly; using NUnit.Framework; namespace uhttpsharp.Tests { - [TestFixture] public class HttpMethodProviderTests { @@ -14,25 +14,35 @@ private static IHttpMethodProvider GetTarget() } - private IEnumerable Methods + public static IEnumerable Methods { get { return Enum.GetNames(typeof(HttpMethods)); } } - - [TestCaseSource("Methods")] - public void Should_Get_Right_Method(string methodName) + + [Test] + [TestCase(HttpMethods.Connect)] + [TestCase(HttpMethods.Delete)] + [TestCase(HttpMethods.Get)] + [TestCase(HttpMethods.Head)] + [TestCase(HttpMethods.Options)] + [TestCase(HttpMethods.Patch)] + [TestCase(HttpMethods.Post)] + [TestCase(HttpMethods.Put)] + [TestCase(HttpMethods.Trace)] + public void Should_Get_Right_Method(HttpMethods method) { // Arrange + var methodName = Enum.GetName(typeof(HttpMethods), method); var target = GetTarget(); // Act var actual = target.Provide(methodName); // Assert - Assert.IsTrue(StringComparer.InvariantCultureIgnoreCase.Equals(actual.ToString(), methodName)); + actual.ToString().ShouldBe(methodName); } } diff --git a/uhttpsharp.Tests/packages.config b/uhttpsharp.Tests/packages.config index c90677c..02801fe 100644 --- a/uhttpsharp.Tests/packages.config +++ b/uhttpsharp.Tests/packages.config @@ -1,5 +1,7 @@  - - + + + + \ No newline at end of file diff --git a/uhttpsharp.Tests/uhttpsharp.Tests.csproj b/uhttpsharp.Tests/uhttpsharp.Tests.csproj index 331fb8e..5fa6850 100644 --- a/uhttpsharp.Tests/uhttpsharp.Tests.csproj +++ b/uhttpsharp.Tests/uhttpsharp.Tests.csproj @@ -1,5 +1,6 @@  + Debug @@ -13,6 +14,7 @@ 512 ..\ true + 543ee8e0 true @@ -32,11 +34,15 @@ 4 - - ..\packages\Moq.4.2.1312.1622\lib\net40\Moq.dll + + ..\packages\NSubstitute.1.8.1.0\lib\net45\NSubstitute.dll - ..\packages\NUnit.2.6.3\lib\nunit.framework.dll + ..\packages\NUnit.2.6.4\lib\nunit.framework.dll + True + + + ..\packages\Shouldly.2.4.0\lib\net40\Shouldly.dll @@ -60,8 +66,17 @@ uhttpsharp + + + + + + This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}. + + +