-
Notifications
You must be signed in to change notification settings - Fork 0
Owin Memory Mock
Connell edited this page Feb 20, 2018
·
1 revision
This tutorial runs a self-contained mock API host with temporary in-memory data storage.
Open Visual Studio and create a new Console App (.NET Framework). In our example, we'll call it Restaurant.
- Go to File > New > Project
- From the left pane, select Installed > Visual C# > Windows Classic Desktop
- Select Console App (.NET Framework)
- Enter Restaurant and click OK.
PM> Install-Package Microsoft.Owin.Hosting
PM> Install-Package Microsoft.Owin.Host.HttpListener
PM> Install-Package Firestorm.Owin
PM> Install-Package Firestorm.Fluent.All
- Create a new C# class file called RestaurantApiContext .cs and replace it's contents with the following code.
using System.Collections.Generic;
using Firestorm.Fluent;
namespace Restaurant
{
internal class RestaurantApiContext : ApiContext
{
public RestaurantApiContext()
{
Options.RootConfiguration.AllowWrite = true;
}
public ApiRoot<Dish> Dishes { get; set; }
public ApiRoot<Ingredient> Ingredients { get; set; }
}
internal class Dish
{
public int Id { get; set; }
public decimal Price { get; set; }
public string Name { get; set; }
public ICollection<Ingredient> Ingredients { get; set; }
}
internal class Ingredient
{
public int Id { get; set; }
public string Name { get; set; }
}
}Open Program.cs and replace it's contents with the following code.
using System;
using Firestorm.Endpoints.Web;
using Firestorm.Engine.Defaults;
using Firestorm.Fluent.Start;
using Firestorm.Owin;
using Microsoft.Owin.Hosting;
namespace Restaurant
{
class Program
{
static void Main(string[] args)
{
string baseAddress = "http://localhost:9000/";
using (WebApp.Start(baseAddress, builder => builder.UseFirestorm(new FirestormConfiguration
{
StartResourceFactory = new FluentStartResourceFactory
{
ApiContext = new RestaurantApiContext(),
DataSource = new MemoryDataSource()
}
})))
{
Console.ReadLine();
}
}
}
}That's all you need to do. Run the application now and you've got yourself a nice little test API.
We can create ourselves a new dish.
POST http://localhost:9000/dishes HTTP/1.1
{
"id": 1,
"name": "Awesome dish",
"ingredients": [ { "name": "Bacon" } ]
}
HTTP/1.1 201 Created
Location: /dishes/1Then use PUT to edit the price.
PUT http://localhost:9000/dishes/1 HTTP/1.1
{
"price": 5.60
}
HTTP/1.1 200 OKAnd then get the whole object back.
GET http://localhost:9000/dishes/1 HTTP/1.1
HTTP/1.1 200 OK
{
"id": 1,
"price": 5.60,
"name": "Awesome dish",
"ingredients": [ { "name": "Bacon" } ]
}