From be926852f2b5c4f8bdab20a88876e7c1acc06171 Mon Sep 17 00:00:00 2001 From: Dmitry Malikov Date: Mon, 6 Oct 2014 19:33:38 +0400 Subject: [PATCH] Tests should not depend so strong on a newline symbol --- .../Observables/ObservableFromFileTests.cs | 18 ++- Anna.Tests/ServerTests.cs | 123 ++++++++---------- README.md | 2 +- Tools/NuGet.exe | Bin 4 files changed, 65 insertions(+), 78 deletions(-) mode change 100644 => 100755 Tools/NuGet.exe diff --git a/Anna.Tests/Observables/ObservableFromFileTests.cs b/Anna.Tests/Observables/ObservableFromFileTests.cs index f25b5e3..7b732e6 100644 --- a/Anna.Tests/Observables/ObservableFromFileTests.cs +++ b/Anna.Tests/Observables/ObservableFromFileTests.cs @@ -12,7 +12,6 @@ namespace Anna.Tests.Observables [TestFixture] public class ObservableFromFileTests { - [Test] public void ShouldWork() { @@ -26,10 +25,10 @@ public void ShouldWork() } var array = bytes.SelectMany(b => b) - .Skip(3) // ignore the first 3bytes - .ToArray(); - var result = Encoding.UTF8.GetString(array); - var expected = String.Join(Environment.NewLine, Enumerable.Range(1,9)); + .Skip(3) // ignore the first 3bytes + .ToArray(); + var result = Encoding.UTF8.GetString(array).Replace("\n", " ").Replace("\r\n", " "); + var expected = String.Join(" ", Enumerable.Range(1, 9)); result.Should().Be.EqualTo(expected); } @@ -46,13 +45,12 @@ public void CanReadChunked() } var array = bytes.SelectMany(b => b) - .Skip(3) // ignore the first 3bytes - .ToArray(); + .Skip(3) // ignore the first 3bytes + .ToArray(); - var result = Encoding.UTF8.GetString(array); - var expected = String.Join(Environment.NewLine, Enumerable.Range(1, 9)); + var result = Encoding.UTF8.GetString(array).Replace("\n", " ").Replace("\r\n", " "); + var expected = String.Join(" ", Enumerable.Range(1, 9)); result.Should().Be.EqualTo(expected); } - } } \ No newline at end of file diff --git a/Anna.Tests/ServerTests.cs b/Anna.Tests/ServerTests.cs index 3ec6967..9595c8f 100644 --- a/Anna.Tests/ServerTests.cs +++ b/Anna.Tests/ServerTests.cs @@ -12,7 +12,6 @@ using Moq; using NUnit.Framework; using SharpTestsEx; -using Enumerable = System.Linq.Enumerable; namespace Anna.Tests { @@ -21,14 +20,14 @@ public class ServerTests [Test] public void CanReturnAnString() { - using(var server = new HttpServer("http://*:1234/")) + using (var server = new HttpServer("http://*:1234/")) { server.GET("/") .Subscribe(ctx => ctx.Respond("hello world")); - + Browser.ExecuteGet("http://localhost:1234") - .ReadAllContent() - .Should().Be.EqualTo("hello world"); + .ReadAllContent() + .Should().Be.EqualTo("hello world"); } } @@ -41,8 +40,8 @@ public void CanReturnAStringFromWildcardURL() .Subscribe(ctx => ctx.Respond("hello, " + ctx.Request.UriArguments.Name + "!")); Browser.ExecuteGet("http://localhost:1234/hello/George") - .ReadAllContent() - .Should().Be.EqualTo("hello, George!"); + .ReadAllContent() + .Should().Be.EqualTo("hello, George!"); } } @@ -56,8 +55,8 @@ public void ExampleCode() .Subscribe(ctx => ctx.Respond("Hello, " + ctx.Request.UriArguments.Name + "!")); Browser.ExecuteGet("http://localhost:1234/hello/George") - .ReadAllContent() - .Should().Be.EqualTo("Hello, George!"); + .ReadAllContent() + .Should().Be.EqualTo("Hello, George!"); // use Rx LINQ operators server.POST("/hi/{Name}") @@ -69,12 +68,12 @@ public void ExampleCode() .Subscribe(ctx => ctx.Respond("Hi, Pete!")); Browser.ExecutePost("http://localhost:1234/hi/George") - .ReadAllContent() - .Should().Be.EqualTo("Hi, George!"); + .ReadAllContent() + .Should().Be.EqualTo("Hi, George!"); Browser.ExecutePost("http://localhost:1234/hi/Pete") - .ReadAllContent() - .Should().Be.EqualTo("Hi, Pete!"); + .ReadAllContent() + .Should().Be.EqualTo("Hi, Pete!"); // This becomes a problem: //Browser.ExecutePost("http://localhost:1234/hi/Fran").StatusCode.Should().Be.EqualTo(404); @@ -86,7 +85,7 @@ public void CanReturnBinaryData() { using (var server = new HttpServer("http://*:1234/")) { - var expectedResponse = new byte[] { 0, 1, 2, 3, 4}; + var expectedResponse = new byte[] { 0, 1, 2, 3, 4 }; server.GET("/") .Subscribe(ctx => ctx.Response(expectedResponse).Send()); @@ -103,31 +102,26 @@ public void CanDecodeARequestBody() { using (var server = new HttpServer("http://*:1234/")) { - string requestBody = "There's no business like \u0160ovs \u4F01\u696D"; + const string requestBody = "There's no business like \u0160ovs \u4F01\u696D"; server.POST("/") - .Subscribe(ctx => + .Subscribe(ctx => ctx.Request.GetBody().Subscribe(body => { - ctx.Request.GetBody().Subscribe(body => - { - try - { - Console.WriteLine(body); - body.Should().Be.EqualTo(requestBody); - } - finally - { - ctx.Respond("hi"); - } - }); - }); + try + { + body.Should().Be.EqualTo(requestBody); + } + finally + { + ctx.Respond("hi"); + } + })); - //Browser.ExecutePost("http://posttestserver.com/post.php", requestBody) Browser.ExecutePost("http://localhost:1234", requestBody) - .ReadAllContent() - .Should().Contain("hi"); + .ReadAllContent() + .Should().Contain("hi"); } } - + [Test] public void CanReturnAStaticFile() { @@ -137,24 +131,22 @@ public void CanReturnAStaticFile() .Subscribe(ctx => ctx.StaticFileResponse(@"samples\example_1.txt").Send()); Browser.ExecuteGet("http://localhost:1234") - .ReadAllContent() - .Should().Contain(string.Join(Environment.NewLine, Enumerable.Range(1, 9))); + .ReadAllContent().Replace("\r\n", " ").Replace("\n", " ") + .Should().Contain(string.Join(" ", Enumerable.Range(1, 9))); } } - - [Test] public void CanReturnAnStatusCode() { using (var server = new HttpServer("http://*:1234/")) { server.POST("/") - .Subscribe(ctx => ctx.Respond(201)); + .Subscribe(ctx => ctx.Respond(201)); Browser.ExecutePost("http://localhost:1234") - .StatusCode - .Should().Be.EqualTo(HttpStatusCode.Created); + .StatusCode + .Should().Be.EqualTo(HttpStatusCode.Created); } } @@ -167,8 +159,8 @@ public void CanHandleUriArguments() .Subscribe(ctx => ctx.Respond(string.Format("hello {0}", ctx.Request.UriArguments.name))); Browser.ExecuteGet("http://localhost:1234/customer/peter") - .ReadAllContent() - .Should().Be.EqualTo("hello peter"); + .ReadAllContent() + .Should().Be.EqualTo("hello peter"); } } @@ -178,10 +170,10 @@ public void CanHandleQueryStringArguments() using (var server = new HttpServer("http://*:1234/")) { server.GET("customers") - .Subscribe(ctx => ctx.Respond(string.Format("customers where name equals to {0}", ctx.Request.QueryString.Name))); + .Subscribe(ctx => ctx.Respond(string.Format("customers where name equals to {0}", ctx.Request.QueryString.Name))); Browser.ExecuteGet("http://localhost:1234/customers?name=jose") - .ReadAllContent().Should().Be.EqualTo("customers where name equals to jose"); + .ReadAllContent().Should().Be.EqualTo("customers where name equals to jose"); } } @@ -194,8 +186,8 @@ public void WhenRequestingAnUnhandledRoute_ThenReturn404() .Subscribe(ctx => ctx.Respond(string.Format("hello {0}", ctx.Request.UriArguments.name))); Executing.This(() => Browser.ExecuteGet("http://localhost:1234/customersssss/peter")) - .Should().Throw() - .And.Exception.Response.OfType().StatusCode.Should().Be.EqualTo(HttpStatusCode.NotFound); + .Should().Throw() + .And.Exception.Response.OfType().StatusCode.Should().Be.EqualTo(HttpStatusCode.NotFound); } } @@ -208,10 +200,10 @@ public void WhenSubscribingToRaw_ThenIgnoreTheMethod() .Subscribe(ctx => ctx.Respond(string.Format("hello {0}", ctx.Request.UriArguments.name))); Browser.ExecuteGet("http://localhost:1234/customer/peter") - .ReadAllContent().Should().Be.EqualTo("hello peter"); - + .ReadAllContent().Should().Be.EqualTo("hello peter"); + Browser.ExecutePost("http://localhost:1234/customer/peter") - .ReadAllContent().Should().Be.EqualTo("hello peter"); + .ReadAllContent().Should().Be.EqualTo("hello peter"); } } @@ -224,14 +216,13 @@ public void CanSubscribeToRawInARawPath() .Subscribe(ctx => ctx.Respond("hello master of puppets!")); Browser.ExecuteGet("http://localhost:1234/website/a/b/c/peter?thisQueryString=asdasdsa") - .ReadAllContent().Should().Be.EqualTo("hello master of puppets!"); + .ReadAllContent().Should().Be.EqualTo("hello master of puppets!"); Browser.ExecutePost("http://localhost:1234/website/abcdeefasdasds?a=cdef") - .ReadAllContent().Should().Be.EqualTo("hello master of puppets!"); + .ReadAllContent().Should().Be.EqualTo("hello master of puppets!"); } } - [Test] public void UseSameThreadForAllRequests() { @@ -241,36 +232,34 @@ public void UseSameThreadForAllRequests() { server.RAW("") .Subscribe(ctx => - { - bag.Add(Thread.CurrentThread.ManagedThreadId); - ctx.Respond(200); - }); + { + bag.Add(Thread.CurrentThread.ManagedThreadId); + ctx.Respond(200); + }); Parallel.For(1, 1000, i => Browser.ExecuteGet("http://localhost:1234/")); bag.Distinct().Count() .Should("The default scheduler should be Event Loop, and all subscriber run in same thread") .Be.EqualTo(1); - } } - [Test] public void WhenWritingToTheStreamFail_ThenTryToRespond500() { - using(var server = new HttpServer("http://*:1234/", Scheduler.CurrentThread)) + using (var server = new HttpServer("http://*:1234/", Scheduler.CurrentThread)) { server.RAW("") - .Subscribe(ctx => - { - var mockedResponse = new Mock(ctx, 201); - mockedResponse.Setup(r => r.WriteStream(It.IsAny())) - .Returns(Observable.Throw(new InvalidOperationException())); - mockedResponse.Object.Send(); - }); + .Subscribe(ctx => + { + var mockedResponse = new Mock(ctx, 201); + mockedResponse.Setup(r => r.WriteStream(It.IsAny())) + .Returns(Observable.Throw(new InvalidOperationException())); + mockedResponse.Object.Send(); + }); Executing.This(() => Browser.ExecuteGet("http://localhost:1234/")) - .Should().Throw(); + .Should().Throw(); } } } diff --git a/README.md b/README.md index fa36089..74f4f35 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# Anna [![Build status](https://ci.appveyor.com/api/projects/status/mw9xg6c7jamxsq3c?svg=true&branch=master)](https://ci.appveyor.com/project/jfromaniello/anna) [![NuGet](http://img.shields.io/nuget/v/Anna.svg?style=flat-square)](https://www.nuget.org/packages/Anna/) [![NuGet downloads](http://img.shields.io/nuget/dt/Anna.svg?style=flat-square)](https://www.nuget.org/packages/Anna/) +# Anna [![Build status](https://ci.appveyor.com/api/projects/status/mw9xg6c7jamxsq3c/branch/master?svg=true)](https://ci.appveyor.com/project/jfromaniello/anna) [![NuGet](http://img.shields.io/nuget/v/Anna.svg?style=flat-square)](https://www.nuget.org/packages/Anna/) [![NuGet downloads](http://img.shields.io/nuget/dt/Anna.svg?style=flat-square)](https://www.nuget.org/packages/Anna/) Anna is an event-driven HTTP server library built with ReactiveExtensions (Rx). Anna was inspired in [Node.js](http://nodejs.org) and [Nancy](https://github.com/NancyFx/Nancy). diff --git a/Tools/NuGet.exe b/Tools/NuGet.exe old mode 100644 new mode 100755