natemcmaster / dotnet-serve

Simple command-line HTTPS server for the .NET Core CLI

Home Page:https://nuget.org/packages/dotnet-serve/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Add support for referencing SimpleServer as a public type in a nuget package

goblinfactory opened this issue · comments

I'm busy working on some test tools that I need for a project, and it would be super helpful to be able to reference SimpleServer directly in C# from an integration test, and pass in my own configurations.

There are too many ways that Kestrel can be configured for us to expose all the variations as command line properties.

It also makes it easier to control the lifetime of the server from code within end to end integration tests allowing different tests to have different values, e.g. ports, headers, rate limiting etc that would be a nightmare to have to try to pass via command line arguments to a shell command.

The current code for SimpleServer isn't really meant for this usage, but this is open source and you're more than welcome to copy my code (see paragraph 4 in License.txt about how to deal with derivative works.)

That said, I'd be happy to sponsor a class library on nuget.org if you're willing to do some of the legwork. Before you jump into implementing this, can you share a snippet of C# that demonstrates how you would want to use this? Also, I'm not clear on whether you want an API that launches kestrel in a separate process and manages passing in all the command line args, or if you are just trying to launch kestrel in the same test process.

Excellent :D thank you for the quick reply.

My use case
I'm busy re-writing an old load test tool that I wrote in 2015, similar to gattling.io and need dotnet-serve for the integration tests for the tool, so that I can have tests that prove that the tool is reporting the correct metrics. I don't want to use mock httpServers because that overly complicates the unit tests.

So I have acceptance tests similar to the following.

Feature : Should be able to trust test results are accurate

Scenario: Small sample sizes

   given a webserver
   given gunner
   when gunner load test is run with a small sample size for a short duration
   then gunner requests per second reported should be within @acceptableTolerance of what operating system instrumentation reports  

Ideally I want to be able to have a IDisposable test server that will run a test server with given parameters, e.g. response throttling, custom port etc. So that I can run multiple webservers simulating systems under heavy load without actually putting the test server under heavy load.

The key things i need are a guarantee that the SimpleServer class will cleanup any hung processes, and not leave any open sockets or connections that could cause false negatives on other tests. I want the tests to be isolated and configurable. Webservers should be able to easily run on different ports, overriding configurations, e.g. security, authentication, pipelines etc.

All of this is obviously available via Kestrel.Core, but it just seems that there will be so many common usages for testing that it doesnt make sense to re-invent the wheel every time. The only reason I did not write my own is that the certificate management is non trivial and you've done an amazing job with that.

While I can shell out and run dotnet-serve it won't be disposable and is easily left in an unstable state. I'd have to write try catch kill-process code, which is messy and get's in the way of the readability of the tests.

I'm also the maintainer for a C# fluent automation testing library Draki forked from FluentAutomation and that has a requirement to fire up webservers with different configurations and ports, for testing test tooling.

re: That said, I'd be happy to sponsor a class library on nuget.org if you're willing to do some of the legwork.

excellent! Yes I'd love to.

I'll create a poc project forked from dotnet-serve and see what a simple usage could actually look like and get you that snippet. (I wont have time till next week to do that)

This issue has been automatically marked as stale because it has no recent activity. It will be closed if no further activity occurs. Please comment if you believe this should remain open, otherwise it will be closed in 14 days. Thank you for your contributions to this project.

Closing due to inactivity.
If you are looking at this issue in the future and think it should be reopened, please make a commented here and mention natemcmaster so he sees the notification.