Many programming languages, including Go, have frameworks for performing HTTP end-to-end (e2e) tests. However, not everyone knows that such tests can be performed using the httptest
package built into the Go standard library. In this short article, we will show you how to do it - we will prepare end-to-end tests for a simple endpoint using only the httptest.Server
from the httptest
package.
End-to-end (e2e) testing is a type of software testing that aims to verify the functionality and behavior of an application from start to finish, simulating real-world scenarios.
In the case of an HTTP server, simulating real-world scenarios involves creating a test environment that is equivalent to the production environment, including the database, network and any other external dependencies. Once created, the test environment is used in e2e testing to send real HTTP requests with various parameters, headers and content. The corresponding HTTP responses from the server are verified by the tests to make sure everything works as intended, just as it would on a production environment.
HTTP handler for testing
Before we move on to testing, we need to create the web application that we will test. As the simplest example, we only need a single HTTP handler function such as HTTP Hello World:
hello_world.go
|
|
The only thing this handler does is to write the string “Hello World” in response to the HTTP request.
End-to-end test example
We will use httptest.Server
to perform e2e tests. The httptest.Server
is a structure built-in in Golang’s standard library (in the httptest
package) that allows developers to create HTTP servers for testing purposes. This struct is useful when testing HTTP handlers, routers, and middlewares. It allows to test the operation of the application from start to finish locally, without setting up a separate testing environment on the remote host.
In the constructor, it accepts http.Handler
as an input argument, so you can even use it to connect an entire application hidden behind a request router:
func NewServer(handler http.Handler) *httptest.Server
In our case, the handler will be our simple HelloWorld
function converted using http.HandlerFunc()
.
We will perform end-to-end test in the standard hello_world_test.go
file using the testing
package. However, there is no problem to create a separate testing script working according to your own rules.
Take a look at our sample end-to-end test below:
hello_world_test.go
|
|
- In lines
11
and12
we initialize a newhttptest.Server
with our handler and instruct it to close at the end of the test with the worddefer
. - The server has a custom client configured to make requests to the server, which we get and use from line
13
. - Using the client, we make a request to the server address (line
15
), and then test the response, checking that it has the right status code (line20
) and the right body content (line27
).
So as you can see, testing web applications this way is very easy. All you need to do is to initialize httptest.Server
, and then use it and its client to make HTTP requests, which you can then verify using test functions. The httptest.Server
significantly simplifies the testing of HTTP applications and it is worth knowing this method when working with such applications on a daily basis.