aboutsummaryrefslogtreecommitdiff
path: root/internal/webui/utils_test.go
blob: 1e07aa22ac88c392131a30a33eff417c1a609876 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package webui

import (
	"fmt"
	"net/http"
	"net/http/httptest"
	"net/url"
	"reflect"
	"strings"
	"testing"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

var simpleErrorMessage = fmt.Errorf("")

type httpTestCase struct {
	name   string
	input  httpTestInput
	expect httpTestExpect
}
type httpTestInput struct {
	method string
	path   string
	cookie *http.Cookie
	data   url.Values
}
type httpTestExpect struct {
	code       int
	bodyString string
	location   string
	setsCookie bool
	err        interface{}
}

func runHttpTest(t *testing.T, e *env, h func(e *env, w http.ResponseWriter, r *http.Request) error, tc *httpTestCase) {
	req, err := http.NewRequest(tc.input.method, tc.input.path, nil)
	require.Nil(t, err)
	if tc.input.data != nil {
		req, err = http.NewRequest(tc.input.method, tc.input.path, strings.NewReader(tc.input.data.Encode()))
		req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
	}
	if tc.input.cookie != nil {
		req.AddCookie(tc.input.cookie)
	}
	t.Run(tc.name, func(t *testing.T) {
		rr := httptest.NewRecorder()
		err := h(e, rr, req)
		if tc.expect.err != nil {
			require.Error(t, err)
			assert.Equalf(t, reflect.TypeOf(err), reflect.TypeOf(tc.expect.err), "Invalid error type. Got %s but expected %s", reflect.TypeOf(err), reflect.TypeOf(tc.expect.err))
		} else {
			require.NoError(t, err)
			assert.Equal(t, tc.expect.code, rr.Code)
			if tc.expect.bodyString != "" {
				assert.Contains(t, rr.Body.String(), tc.expect.bodyString)
			}
			if tc.expect.location != "" {
				assert.Contains(t, rr.HeaderMap, "Location")
				assert.Len(t, rr.HeaderMap["Location"], 1)
				assert.Equal(t, rr.HeaderMap["Location"][0], tc.expect.location)
			}
			if tc.expect.setsCookie {
				assert.Contains(t, rr.HeaderMap, "Set-Cookie")
			} else {
				assert.NotContains(t, rr.HeaderMap, "Set-Cookie")
			}
		}
	})
}