Skip to content

Owin Memory Mock

Connell edited this page Feb 20, 2018 · 1 revision

Self-hosted OWIN In-Memory Mock API with Firestorm Fluent API

This tutorial runs a self-contained mock API host with temporary in-memory data storage.

Start new C# Console Application

Open Visual Studio and create a new Console App (.NET Framework). In our example, we'll call it Restaurant.

  1. Go to File > New > Project
  2. From the left pane, select Installed > Visual C# > Windows Classic Desktop
  3. Select Console App (.NET Framework)
  4. Enter Restaurant and click OK.

Install Firestorm packages

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 your context and models

  1. 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; }
    }
}

Start the REST API in your Program.cs

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();
            }
        }
    }
}

Run your application

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/1

Then use PUT to edit the price.

PUT http://localhost:9000/dishes/1 HTTP/1.1
{
   "price": 5.60
}

HTTP/1.1 200 OK

And 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" } ]
}
Clone this wiki locally