Created
July 13, 2015 22:42
-
-
Save marekstachura/d7200724263a07fd68fa to your computer and use it in GitHub Desktop.
Stub rest service using Nancy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using Nancy; | |
using Nancy.Hosting.Self; | |
using NUnit.Framework; | |
using RestSharp; | |
namespace NancyApp | |
{ | |
public class StubModule : NancyModule | |
{ | |
public StubModule() | |
{ | |
Get["/"] = _ => "{}"; | |
Post["/consumers/dev"] = _ => @"{ | |
""base_uri"": ""http://192.168.50.2:8082/consumers/dev/instances/rest-consumer-1"", | |
""instance_id"": ""rest-consumer-1"" | |
}"; | |
Post["/consumers/not-found/instances/not-found"] = _ => | |
{ | |
var r = (Response)@"{ | |
""error_code"": 404, | |
""message"": ""HTTP 404 Not Found"" | |
}"; | |
r.StatusCode = HttpStatusCode.NotFound; | |
return r; | |
} | |
; | |
Get["/topics/status"] = _ => "status"; | |
} | |
} | |
[TestFixture()] | |
public class NancyTest | |
{ | |
private NancyHost _server; | |
[SetUp] | |
public void SetUp() | |
{ | |
_server = new NancyHost(new Uri("http://localhost:8282")); | |
_server.Start(); | |
} | |
[TearDown] | |
public void TearDown() | |
{ | |
_server.Stop(); | |
} | |
[Test()] | |
public void Get_To_Base_Should_Return_Empty_Content() | |
{ | |
var client = new RestClient("http://localhost:8282"); | |
var request = new RestRequest("/"); | |
var response = client.Execute(request); | |
var content = response.Content; | |
Assert.That(response.Content, Is.StringContaining("{}")); | |
} | |
[Test()] | |
public void Create_Consumer_Should_Return_200() | |
{ | |
var client = new RestClient("http://localhost:8282"); | |
var request = new RestRequest("/consumers/dev", Method.POST); | |
var response = client.Execute(request); | |
var content = response.Content; | |
Assert.That(response.StatusCode, Is.EqualTo(System.Net.HttpStatusCode.OK)); | |
Assert.That(response.Content, Is.StringContaining("rest-consumer-1")); | |
} | |
[Test()] | |
public void Create_Consumer_Should_Return_404_For_Not_Existing_Instance() | |
{ | |
var client = new RestClient("http://localhost:8282"); | |
var request = new RestRequest("/consumers/not-found/instances/not-found", Method.POST); | |
var response = client.Execute(request); | |
var content = response.Content; | |
Assert.That(response.StatusCode, Is.EqualTo(System.Net.HttpStatusCode.NotFound)); | |
Assert.That(response.Content, Is.StringContaining("HTTP 404 Not Found")); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment