Skip to content

Commit

Permalink
Tests should not depend so strong on a newline symbol
Browse files Browse the repository at this point in the history
  • Loading branch information
dmalikov committed Oct 9, 2014
1 parent ab673d0 commit be92685
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 78 deletions.
18 changes: 8 additions & 10 deletions Anna.Tests/Observables/ObservableFromFileTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ namespace Anna.Tests.Observables
[TestFixture]
public class ObservableFromFileTests
{

[Test]
public void ShouldWork()
{
Expand All @@ -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);
}

Expand All @@ -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);
}

}
}
123 changes: 56 additions & 67 deletions Anna.Tests/ServerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
using Moq;
using NUnit.Framework;
using SharpTestsEx;
using Enumerable = System.Linq.Enumerable;

namespace Anna.Tests
{
Expand All @@ -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");
}
}

Expand All @@ -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!");
}
}

Expand All @@ -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}")
Expand All @@ -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);
Expand All @@ -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());

Expand All @@ -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()
{
Expand All @@ -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);
}
}

Expand All @@ -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");
}
}

Expand All @@ -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");
}
}

Expand All @@ -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<WebException>()
.And.Exception.Response.OfType<HttpWebResponse>().StatusCode.Should().Be.EqualTo(HttpStatusCode.NotFound);
.Should().Throw<WebException>()
.And.Exception.Response.OfType<HttpWebResponse>().StatusCode.Should().Be.EqualTo(HttpStatusCode.NotFound);
}
}

Expand All @@ -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");
}
}

Expand All @@ -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()
{
Expand All @@ -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<Response>(ctx, 201);
mockedResponse.Setup(r => r.WriteStream(It.IsAny<Stream>()))
.Returns(Observable.Throw<Stream>(new InvalidOperationException()));
mockedResponse.Object.Send();
});
.Subscribe(ctx =>
{
var mockedResponse = new Mock<Response>(ctx, 201);
mockedResponse.Setup(r => r.WriteStream(It.IsAny<Stream>()))
.Returns(Observable.Throw<Stream>(new InvalidOperationException()));
mockedResponse.Object.Send();
});

Executing.This(() => Browser.ExecuteGet("http://localhost:1234/"))
.Should().Throw<WebException>();
.Should().Throw<WebException>();
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -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).

Expand Down
Empty file modified Tools/NuGet.exe
100644 → 100755
Empty file.

0 comments on commit be92685

Please sign in to comment.