How do you test a server?

216 views
Skip to first unread message

st ov

unread,
Jul 19, 2017, 12:44:01 PM7/19/17
to golang-nuts
When creating a simple server using http.Server and http.ServeMux, how do you test it?

What do you do for unit tests? integration tests?

Do you mock requests to each handler and check the response? How?

How do you test peformance and requests per second?

Shawn Milochik

unread,
Jul 19, 2017, 12:54:30 PM7/19/17
to golang-nuts

There are some tools for that here.

Jérôme LAFORGE

unread,
Jul 19, 2017, 1:07:44 PM7/19/17
to golang-nuts
One of my favorite resource can be found here about testing server
https://github.com/bradfitz/talk-yapc-asia-2015/blob/master/talk.md#testing

st ov

unread,
Jul 20, 2017, 11:59:37 AM7/20/17
to golang-nuts
Thats a great resource, thank you!

Also does the htttptest.Server mock out a server so that you can test clients?
What if I want to test my own server?

Simon Ritchie

unread,
Jul 21, 2017, 4:24:23 AM7/21/17
to golang-nuts
My scaffolding tool generates a web server and some unit and integration tests.  The tests use Peter Gotz' mocking framework pegomock.  If you generate a server with the scaffolder, you can look at the source code and see how it works.

https://github.com/goblimey/scaffolder

By the way, not everybody in the Go community believes in the use of mocking frameworks.  Donovan and Kernighan's The Go Programming Language advocates a different approach.  This is used to test the entire Go infrastructure, so it's clearly effective.  However, I come from the London Java community and we use mocking frameworks a lot over here.

I used pegomock because I couldn't get gomock to work.  I managed to get pegomock working and I found that Peter was much more responsive when I reported a couple of issues with it.

For system testing, you can use the Firefox Selenium addon, which works at the HTTP level and doesn't care what language you used to create the server.  It  records a web session and produces tests in various formats, which you can then edit.  For example, if you record a test that creates an object in a database and it produced one with ID 42, and then you record a test that displays object 42, you can edit the test to replace the ID with a wildcard.

Before I wrote my scaffolder, I experimented by hand-crafting a very simple web server: https://github.com/goblimey/films.
I recorded some Selenium tests for this.  They are in  tests/system/selenium.

Obviously, there is a general point here.  There are lots of tools out there that run penetration, performance and other types of testing on http servers and none of them care what technology you used to create the server.

st ov

unread,
Jul 23, 2017, 7:05:17 PM7/23/17
to golang-nuts
Thanks for the suggestions, but that framework is for mocking a server right? 
What if what I want to test is a server and endpoints I developed?

Brian Wolter

unread,
Jul 23, 2017, 8:26:41 PM7/23/17
to golang-nuts
As others have noted, you can use `net/http/httptest`. I'm not personally a huge fan of that package, though.

As a result, I wrote an HTTP testing tool to run API tests against my servers. Of course, it can be used to test against any HTTP service, but it does happen to be written in Go.

Test cases are straightforward and declarative: you just give it a request to make and the expected response. As a bonus, you can automatically generate documentation of your endpoints from the test cases. (Which is really pretty handy.)

Reply all
Reply to author
Forward
0 new messages