My new configurable server
Tags: dotnet, productivity, testing
Published: 2019-11-04 20:34:34

I think I actually had a productive thought today, and I started coding and I think I've come to a place where I can start showing off what I've been working on for the last couple of hours.

# What have you been working on?
It's a configurable test server! A little less than exciting, but let me take you through my thinking and why I have created this small tool, which I think is going to help me out a bunch.

## Why did you create this thing?
I don't know about you but I spend a lot of my day working on one thing, and that one thing talks to other things and what I've found is that that I have to run both applications even though I'm only interested in the one. 

In the past when this has become a bit of a hinderance I have spun up a small web application that sends back fake data. The problem with this? Well I need to spin up a fake data service for everything that my thing calls, so I thought to myself that there must be a better way - and their sort of is, you can setup postman "apis" that allows you to configure a contact-based API.

I've been doing a lot of reading about dotnet "global" tools recently, basically, they're small tools that you can run from your command line. So I thought you know what, I'm going to write a dotnet global tool that allows you to spin up a small dotnetcore server and reads a file. In this file, you'll be able to specify a set of URL's and a set of responses and when a URL gets hit the response gets sent back.

It saves me a bunch of hassle in creating small fake data services.

## Let me show you how
If you go to your command shell right now and run this command:
`dotnet tool install --global DotNetSimpleServer`

You will have my tool installed, cool hey?

So how do you use it!? Very simply! If you run: `dotnet-server -fileName:C:\example.txt` for example it will load all of your urls and responses from that file. What does the file look like, let me give you a peak:

```
{
    "sslEnabled" : true,
    "tlsEnabled" : true,
    "interactions": [
        {
            "description": "A GET request to retrieve a thing",
            "request": {
                "method": "get",
                "path": "/things/1234"
            },
            "response": {
                "status": 200,
                "headers": [],
                "body": {
                    "Hello" : "World"
                }
            }
        },
        {
            "description": "A GET request to retrieve a thing",
            "request": {
                "method": "get",
                "path": "/otherThing"
            },
            "response": {
                "status": 200,
                "headers": [],
                "body": {
                    "Hello" : "World2"
                }
            }
        }
    ]
}
```

Here we are defining two endpoints one `/things/1234` which will return the json object `{"Hello": "World"}` and a `/otherThing` endpoint that will return an object that looks like: `{"Hello" : "World2"}`.

I like this tool, I think it's going to save me a bunch of time in the future!  

I will admit the code is a bit rough but ready, for example if you don't specify a fileName then it will just throw an exception and I'm pretty sure if you get the contents of the file wrong something weird will happen. But these are bugs that I will be actively working on in the next couple of days.

If you like what you see, please give the tool a go and see what you think! 

{% github joro550/dotnet-server %}