Created
September 28, 2018 14:16
-
-
Save lars-erik/7ed5ee25b7ca3a2f50f02287c566e9dc to your computer and use it in GitHub Desktop.
Fake JSON API in C#
This file contains 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 System.Collections.Generic; | |
using System.Linq; | |
using System.Net.Http; | |
using System.Text; | |
using System.Threading.Tasks; | |
using System.Web.Http; | |
using Microsoft.Owin.Hosting; | |
using Newtonsoft.Json; | |
using Newtonsoft.Json.Linq; | |
using NUnit.Framework; | |
using Owin; | |
namespace JsonTestServer.Tests | |
{ | |
[TestFixture] | |
public class Fake_Json | |
{ | |
[Test] | |
public void From_Root() | |
{ | |
var client = new HttpClient(); | |
var response = client.GetAsync("http://localhost:9000/api/").Result.Content.ReadAsAsync<JObject>().Result; | |
var json = JsonConvert.SerializeObject(response); | |
Console.WriteLine(json); | |
Assert.That(json, Is.EqualTo(@"{""items"":{""a"":{""title"":""The A"",""story"":""The A took a stroll""},""b"":{""title"":""The B"",""story"":""The B took a roll""}},""authors"":{""bob"":{""fullName"":""Bob Bobson""},""mike"":{""fullName"":""Mike Mikeson""}}}")); | |
} | |
[Test] | |
public void From_Items() | |
{ | |
var client = new HttpClient(); | |
var response = client.GetAsync("http://localhost:9000/api/items").Result.Content.ReadAsAsync<JObject>().Result; | |
var json = JsonConvert.SerializeObject(response); | |
Console.WriteLine(json); | |
Assert.That(json, Is.EqualTo(@"{""a"":{""title"":""The A"",""story"":""The A took a stroll""},""b"":{""title"":""The B"",""story"":""The B took a roll""}}")); | |
} | |
[Test] | |
public void From_Item() | |
{ | |
var client = new HttpClient(); | |
var response = client.GetAsync("http://localhost:9000/api/items/a").Result.Content.ReadAsAsync<JObject>().Result; | |
var json = JsonConvert.SerializeObject(response); | |
Console.WriteLine(json); | |
Assert.That(json, Is.EqualTo(@"{""title"":""The A"",""story"":""The A took a stroll""}")); | |
} | |
[Test] | |
public void From_Authors() | |
{ | |
var client = new HttpClient(); | |
var response = client.GetAsync("http://localhost:9000/api/authors").Result.Content.ReadAsAsync<JObject>().Result; | |
var json = JsonConvert.SerializeObject(response); | |
Console.WriteLine(json); | |
Assert.That(json, Is.EqualTo(@"{""bob"":{""fullName"":""Bob Bobson""},""mike"":{""fullName"":""Mike Mikeson""}}")); | |
} | |
private ApiTestHost host; | |
[SetUp] | |
public void Setup() | |
{ | |
host = new ApiTestHost(); | |
host.Start(); | |
} | |
[TearDown] | |
public void TearDown() | |
{ | |
host.Stop(); | |
} | |
} | |
public class CatchAllController : ApiController | |
{ | |
private static string json = @"{ | |
""items"": { | |
""a"": {""title"":""The A"", ""story"": ""The A took a stroll""}, | |
""b"": {""title"":""The B"", ""story"": ""The B took a roll""} | |
}, | |
""authors"": { | |
""bob"": {""fullName"": ""Bob Bobson""}, | |
""mike"": {""fullName"": ""Mike Mikeson""} | |
} | |
}"; | |
private readonly JObject data = JObject.Parse(json); | |
[HttpGet] | |
public object Catch(string catchAll) | |
{ | |
if (catchAll == null) return data; | |
var path = catchAll.Split(new []{'/'}, StringSplitOptions.RemoveEmptyEntries); | |
if (path.Length == 0) return data; | |
JToken current = data; | |
for (var index = 0; index < path.Length; index++) | |
{ | |
current = current[path[index]]; | |
} | |
return current; | |
} | |
} | |
public class ApiTestHost | |
{ | |
private IDisposable host; | |
private string baseAddress; | |
public string BaseAddress => baseAddress; | |
public void Start() | |
{ | |
baseAddress = "http://localhost:9000/"; | |
host = WebApp.Start<Startup>(url: baseAddress); | |
} | |
public void Stop() | |
{ | |
host.Dispose(); | |
} | |
} | |
public class Startup | |
{ | |
public void Configuration(IAppBuilder appBuilder) | |
{ | |
var config = new HttpConfiguration(); | |
config.Routes.MapHttpRoute( | |
"fakeApi", | |
"api/{*catchAll}", | |
new { controller = "CatchAll", Action = "Catch" } | |
); | |
appBuilder.UseWebApi(config); | |
} | |
} | |
} |
This file contains 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
<?xml version="1.0" encoding="utf-8"?> | |
<packages> | |
<package id="Microsoft.AspNet.WebApi" version="5.2.6" targetFramework="net471" /> | |
<package id="Microsoft.AspNet.WebApi.Client" version="5.2.6" targetFramework="net471" /> | |
<package id="Microsoft.AspNet.WebApi.Core" version="5.2.6" targetFramework="net471" /> | |
<package id="Microsoft.AspNet.WebApi.Owin" version="5.2.6" targetFramework="net471" /> | |
<package id="Microsoft.AspNet.WebApi.OwinSelfHost" version="5.2.6" targetFramework="net471" /> | |
<package id="Microsoft.AspNet.WebApi.WebHost" version="5.2.6" targetFramework="net471" /> | |
<package id="Microsoft.Owin" version="2.0.2" targetFramework="net471" /> | |
<package id="Microsoft.Owin.Host.HttpListener" version="2.0.2" targetFramework="net471" /> | |
<package id="Microsoft.Owin.Hosting" version="2.0.2" targetFramework="net471" /> | |
<package id="Newtonsoft.Json" version="11.0.2" targetFramework="net471" /> | |
<package id="NUnit" version="3.10.1" targetFramework="net471" /> | |
<package id="Owin" version="1.0" targetFramework="net471" /> | |
</packages> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment