Skip to content

Instantly share code, notes, and snippets.

@ianbattersby
Created September 17, 2013 12:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ianbattersby/6593929 to your computer and use it in GitHub Desktop.
Save ianbattersby/6593929 to your computer and use it in GitHub Desktop.
Example of Simple.Web bootstrapping w/acceptance test. NOTE: The acceptance test assembly does not need to reference anything other than the bootstrapping assembly!
namespace SimpleWebTest.Tests.Acceptance
{
using SimpleWebTest;
using Xunit;
public class GetEndpointTests : IDisposable
{
private const string port = 5000;
private readonly IDbConnection connection;
private readonly IEnumerable<Genre> expectedMovieGenres;
private readonly Guid expectedId;
public GetEndpointTests()
{
// Open connection to our new empty test in-memory database
// Fill database with entities for expectedMovieGenres
}
public void Dispose()
{
if (this.connection != null)
{
this.connection.Close();
this.connection.Dispose();
}
}
[Fact]
public void ShouldReturn200StatusOnSuccess()
{
using (Program.LaunchServer("localhost", this.port))
{
var request = (HttpWebRequest)WebRequest.Create("http://localhost:" + this.port + "/movie/genres");
request.Method = "GET";
request.Accept = "application/json";
using (var response = (HttpWebResponse)request.GetResponse())
{
Assert.NotNull(response);
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
}
}
}
[Fact]
public void ShouldReturn204NoContentWhenNoGenres()
{
using (Program.LaunchServer("localhost", this.port))
{
this.connection.CreateTable<Genre>(true);
var request = (HttpWebRequest)WebRequest.Create("http://localhost:" + this.port + "/movie/genres");
request.Method = "GET";
request.Accept = "application/json";
using (var response = (HttpWebResponse)request.GetResponse())
{
Assert.NotNull(response);
Assert.Equal(HttpStatusCode.NoContent, response.StatusCode);
}
}
}
[Fact]
public void ShouldReturnExpectDataFromEndPoint()
{
using (Program.LaunchServer("localhost", this.port))
{
var request = (HttpWebRequest)WebRequest.Create("http://localhost:" + this.port + "/movie/genres");
request.Method = "GET";
request.Accept = "application/json";
using (var response = (HttpWebResponse)request.GetResponse())
{
var reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
var payload = reader.ReadToEnd();
reader.Close();
const string ExpectedJsonTemplate = "{{\"genreId\":\"{0}\",\"name\":\"{1}\"";
foreach (var entry in this.expectedMovieGenres)
{
var entityJson = string.Format(ExpectedJsonTemplate, entry.Id, entry.Name);
Assert.Contains(entityJson, payload);
}
}
}
}
}
}
namespace SimpleWebTest
{
using System.Collections.Generic;
using System.Linq;
using Simple.Web;
using Simple.Web.Behaviors;
using Simple.Web.Links;
[UriTemplate("/movie/genres")]
[Canonical(typeof(IEnumerable<GenresModel>), Title = "Movie Genres", Type = "application/vnd.mycompany.movie.genre")]
[Root(Rel = "movie-genres", Title = "Movie Genres", Type = "application/vnd.mycompany.movie.genre.list")]
public class GetEndpoint : IGet, IOutput<IEnumerable<GenresModel>>
{
private readonly IMoviesGenresQuery movieGenresQuery;
public GetEndpoint(IMoviesGenresQuery movieGenresQuery)
{
this.movieGenresQuery = movieGenresQuery;
}
public IEnumerable<GenresModel> Output { get; private set; }
public Status Get()
{
this.Output = this.movieGenresQuery.Execute();
return this.Output.Any() ? Status.OK : Status.NoContent;
}
}
}
namespace SimpleWebTest
{
using System;
using System.Configuration;
using System.Diagnostics;
using Microsoft.Owin.Cors;
using Owin;
using Simple.Web.Hosting.Self;
using Simple.Web.OwinSupport;
using Topshelf;
public class Program
{
private static readonly Type[] EnforceReferencesFor =
{
typeof(Simple.Web.JsonNet.JsonMediaTypeHandler),
typeof(Microsoft.Owin.Host.HttpListener.OwinServerFactory),
typeof(Newtonsoft.Json.ConstructorHandling)
};
private static readonly Action<IAppBuilder> Middleware = app =>
{
app.UseCors(CorsOptions.AllowAll);
app.UseErrorPage();
app.UseSimpleWeb();
};
public static IDisposable LaunchServer(string hostname, int port)
{
return new OwinSelfHost(Middleware).Start(hostname ?? "locahost", port);
}
private static void Main(string[] args)
{
string hostname = ConfigurationManager.AppSettings["server"] ?? "localhost";
int port;
if (!int.TryParse(ConfigurationManager.AppSettings["port"], out port))
{
throw new Exception("Couldn't find port setting in appSettings.config.");
}
HostFactory.Run(
ts =>
{
ts.Service<OwinSelfHost>(
s =>
{
IDisposable owinHost = null;
s.ConstructUsing(settings => new OwinSelfHost(Middleware));
s.WhenStarted(
host =>
{
owinHost = host.Start(hostname, port);
Trace.WriteLine(
string.Format("Listening on http://{0}:{1}", hostname, port));
});
s.WhenStopped(
callback =>
{
if (owinHost != null)
{
owinHost.Dispose();
}
});
});
ts.RunAsLocalSystem();
ts.SetDisplayName("[Simple Web] Test");
ts.SetServiceName("simple-web-test");
});
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment