Web App Testing – Form Validation Testing

Buat file baru cmd/web/forms_test.go, lalu gunakan kode berikut.

package main

import (
	"net/http"
	"net/http/httptest"
	"net/url"
	"testing"
)

func TestForm_Has(t *testing.T) {
	form := NewForm(nil)

	has := form.Has("whatever")
	if has {
		t.Error("form shows has field when it should not")
	}

	postedData := url.Values{}
	postedData.Add("a", "a")
	form = NewForm(postedData)

	has = form.Has("a")
	if !has {
		t.Error("shows form does not have field when it should")
	}
}

func TestForm_Required(t *testing.T) {
	r := httptest.NewRequest("POST", "/whatever", nil)
	form := NewForm(r.PostForm)

	form.Required("a", "b", "c")

	if form.Valid() {
		t.Error("form shows valid when required fields are missing")
	}

	postedData := url.Values{}
	postedData.Add("a", "a")
	postedData.Add("b", "b")
	postedData.Add("c", "c")

	r, _ = http.NewRequest("POST", "/whatever", nil)
	r.PostForm = postedData

	form = NewForm(r.PostForm)
	form.Required("a", "b", "c")
	if !form.Valid() {
		t.Error("shows post does not have required fields, when it does")
	}
}

func TestForm_Check(t *testing.T) {
	form := NewForm(nil)

	form.Check(false, "password", "password is required")
	if form.Valid() {
		t.Error("Valid() returns false, and it should be true when calling Check()")
	}
}

func TestForm_ErrorGet(t *testing.T) {
	form := NewForm(nil)
	form.Check(false, "password", "password is required")
	s := form.Errors.Get("password")

	if len(s) == 0 {
		t.Error("should have an error returned from Get, but do not")
	}

	s = form.Errors.Get("whatever")
	if len(s) != 0 {
		t.Error("should not have an error, but got one")
	}
}

Berikutnya, jangan lupa untuk menambahkan route untuk /login pada cmd/web/routes_test.go.

package main

import (
	"net/http"
	"strings"
	"testing"

	"github.com/go-chi/chi/v5"
)

func Test_application_routes(t *testing.T) {
	var registered = []struct {
		route  string
		method string
	}{
		{"/", "GET"},
		{"/login", "POST"},
		{"/static/*", "GET"},
	}

	var app application
	mux := app.routes()

	chiRoutes := mux.(chi.Routes)

	for _, route := range registered {
		//check if the route exists
		if !routeExists(route.route, route.method, chiRoutes) {
			t.Errorf("route %s is not registered", route.route)
		}
	}
}

func routeExists(testRoute, testMethod string, chiRoutes chi.Routes) bool {
	found := false

	_ = chi.Walk(chiRoutes, func(method string, route string, handler http.Handler, middleware ...func(http.Handler) http.Handler) error {
		if strings.EqualFold(method, testMethod) && strings.EqualFold(route, testRoute) {
			found = true
		}
		return nil
	})

	return found
}

Jika kita jalankan test dengan perintah berikut, sesuai ekspektasi, test berhasil dijalankan.

$ go test -v  ./...

=== RUN   TestForm_Has
--- PASS: TestForm_Has (0.00s)
=== RUN   TestForm_Required
--- PASS: TestForm_Required (0.00s)
=== RUN   TestForm_Check
--- PASS: TestForm_Check (0.00s)
=== RUN   TestForm_ErrorGet
--- PASS: TestForm_ErrorGet (0.00s)
=== RUN   Test_application_handlers
--- PASS: Test_application_handlers (0.05s)
=== RUN   Test_application_addIPToContext
    middleware_test.go:39: 192.0.2.1
    middleware_test.go:39: unknown
    middleware_test.go:39: 192.163.1.3
    middleware_test.go:39: hello
--- PASS: Test_application_addIPToContext (0.00s)
=== RUN   Test_application_ipFromContext
--- PASS: Test_application_ipFromContext (0.00s)
=== RUN   Test_application_routes
--- PASS: Test_application_routes (0.00s)
PASS
ok      webapp/cmd/web  0.581s

Sampai disini kita sudah mempelajari cara membuat test untuk form validasi pada web app.

Pada modul berikutnya kita akan mengimplementasikan session yang akan mempengaruhi cara kita melakukan testing.

Sharing is caring:

Leave a Comment