Skip to content
Santekno.com | Level Up Your Engineering Skills
EN
📖 0%
15 Aug 2023 · 3 min read ·Article 32 / 119
Go

02 How to Used HTTP Test in Golang

IH
Ihsan Arif
Writer at Santekno · Backend Engineer

Introduction to HTTP Testing

HTTP Test in Golang has been provided with a special package for creating unit tests for Web features. All of this is in the net/http/httptest package https://golang.org/pkg/net/http/httptest/ . By using this package, we can unit test the web handler that we have created without having to run the web application so that we can immediately focus on the handler function that we want to test.

Methods in HTTP Test

  • httptest.NewRequest is a function used to create http.Request. We can determine the method, url, body that we will send as a unit test simulation. We can also add other additional information to the request we want to send, such as headers, cookies and so on.
  • httptest.NewRecorder() is the function used to create ResponseRecorder which is a help struct for recording the HTTP response from the results of the testing that we do.

How to implement HTTP Test

We are trying to create a handler that we can test, in the previous post we were able to create a HelloHandler which in this session we will add a unit test for that handler. The way to do this is to create a file in the learning-golang-web project and create the http_handler.go file.

go
 1package main
 2
 3import (
 4	"fmt"
 5	"net/http"
 6)
 7
 8func HelloHandler(w http.ResponseWriter, r *http.Request) {
 9	fmt.Fprintln(w, "Hello World")
10}

If you use vscode then just open the file that we have created then hover our cursor over the function that we have created and right click select Go: Generate Unit Tests for function. Then a file http_handler_test.go will be created with contents like this.

go
 1package main
 2
 3import (
 4	"net/http"
 5	"testing"
 6)
 7
 8func TestHelloHandler(t *testing.T) {
 9	type args struct {
10		w http.ResponseWriter
11		r *http.Request
12	}
13	tests := []struct {
14		name string
15		args args
16	}{
17		// TODO: Add test cases.
18	}
19	for _, tt := range tests {
20		t.Run(tt.name, func(t *testing.T) {
21			HelloHandler(tt.args.w, tt.args.r)
22		})
23	}
24}

Then we update the function and we will implement the httptest package to look like this.

go
 1func TestHelloHandler(t *testing.T) {
 2	tests := []struct {
 3		name string
 4		want string
 5	}{
 6		{
 7			name: "mencetak Hello World",
 8			want: "Hello World\n",
 9		},
10	}
11	for _, tt := range tests {
12		t.Run(tt.name, func(t *testing.T) {
13			request := httptest.NewRequest(http.MethodGet, "http://localhost/hello", nil)
14			recorder := httptest.NewRecorder()
15			HelloHandler(recorder, request)
16
17			response := recorder.Result()
18			body, _ := io.ReadAll(response.Body)
19			bodyString := string(body)
20
21			if !reflect.DeepEqual(bodyString, tt.want) {
22				t.Errorf("response = %v, want %v", bodyString, tt.want)
23			}
24		})
25	}
26}

When we run the unit test with the command below

bash
1go test

then the result if it is appropriate will produce something like this

bash
1➜  learn-golang-web git:(main) ✗ go test                                  
2PASS
3ok      github.com/santekno/learn-golang-web  0.984s

Related Articles

💬 Comments