Go Testing Technique: Testing JSON HTTP Requests

Let’s say you have some code that make an HTTP request with some JSON data. What do you test and how? I’m sure you run into something similar.

I tried to keep this example as toy as possible without making it useless.

Don’t worry too much about the exact details about the specific JSON format, URL, etc…what’s important is that your code is making an HTTP request and you want to test it.

I’ll walk you through the following example.


You need to publish an NSQ message in your application-specific format:

This function will:

  1. Wrap the msg string in a JSON string
  2. Make a POST request to a given URL
  3. Return an error if the response is not acceptable

What to test

  1. You want to test that the format of the POSTed data is as expected. This is the example format:

(where %{MSG} is the string passed)

2. You want to test that Publish() is making the right HTTP request.
In this example is a POST %{nsqdUrl}/pub?topic=meaningful-topic.

3. You also want to test that the request was successful and returned the right HTTP status, in this example would be a 200 OK.

Go testing basics

Go provides a testing package which makes writing basic tests very easy.

Let’s assume the Publish() function is part of a nsqpublisher package in the nsqpublisher.go file. By convention your tests will be in a nsqpublisher_test.go file like this:

A few interesting things to note here:

  • You’re in the same package you’re testing. So you have access to everything in the package
  • The test function starts with Test*.
  • The test function receive a *testing.T which you can use to make the test fail by calling Errorf() on it.

And you can run the tests by $ go test

This test will fail because there is no Publish() function yet. Let’s write one.

A first (broken) implementation

Let’s start with a broken implementation of Publish():

With this in place that test will pass: `Publish()` makes an HTTP request, it fails and returns an error.

An HTTP test server

The first test is useful, but how do you test that you’re making a real HTTP request without the need to run the NSQ servers locally? (or whatever server you’re making requests to)

The answer to this question is the Go httptest package. This package makes the creation of a test HTTP server very easy.

Let’s have a look at how by writing a test for when the nsqd server response wasn’t a 200 OK:

OK, this is a bit more complex but very few things happen here:

  • We create a test server by using httptest.NewServer
  • We get the URL of this server by using ts.URL
  • This server will respond with a 503 SERVICE UNAVAILABLE by using w.WriteHeader()
  • The server also has access to the request r which is an *http.Request (not used in this test)
  • This server will be closed when the test will finish because of the defer ts.Close()

If you run this test it will fail: Publish() makes a request to the test server but it doesn’t check the response’s status code. Easy to fix:

Testing the request

Now that we have a test server is easy to test that Publish() makes the right request:

Run the tests and you’ll get something like this:

Wow, this is very useful. We now have a test which is telling us exactly what Publish() is doing wrong. Yes, it makes an HTTP request but to the wrong path, with the wrong verb and it’s not passing the topic param as it should.

How does it work?

There are a few key things to notice here:

In order to make this test pass change Publish() to make a POST request with the right URL:

Tests will pass again. Try to break them by changing the URL or topic.

Testing we’re posting the correct JSON

As it stand, our Publish() function is essentially broken. It doesn’t POST any data. The request body should be JSON and with the expected format.

How do we test it? You probably guessed that we’ll need to read the request body in the test server, unmarshal the JSON string and test it has the right properties.

I will use the go-simplejson package for it. It makes manipulating/traversing JSON a breeze and it has a nice interface:

The key things here are:

  • We build a simplejson’s object from the request body reader: reqJson, err := simplejson.NewFromReader(r.Body)
  • We traverse the JSON’s properties by using simplejson’s GetPath(“key”, “subkey”)
  • We convert these values into an int/string by using MustInt()/MustString()

Run the tests again and they will fail:

Publish() is not posting any data. Let’s sort this out.

Posting the right JSON

To make the tests pass, Publish() will need to POST the JSON in the right format:

NOTE: In this simple example I’m just using fmt.Sprintf() to build the request JSON, but if the JSON is more complex I would prefer to use simplejson.

With this in place, Publish() will now make a POST request to the right URL and with the right body. Tests will pass.


As you’ve seen, it’s very easy to test that your code is making the correct HTTP requests in Go.

Thanks to its httptest package your tests can spawn a simple HTTP server which you can use to inspect these requests.

I hope this is useful and will help you to write better code. Let me know if you’re testing this in different/better ways.

Software Engineer. Son, brother, grandchild, dog lover. I believe in readable, clean code that speak easy to you (with a slightly italian accent)

Software Engineer. Son, brother, grandchild, dog lover. I believe in readable, clean code that speak easy to you (with a slightly italian accent)