summaryrefslogtreecommitdiff
path: root/workhorse/internal/badgateway/roundtripper_test.go
blob: b59cb8d2c5b31dacdbd7cd8f5660389997966b5e (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
package badgateway

import (
	"errors"
	"io"
	"net/http"
	"testing"

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

type roundtrip502 struct{}

func (roundtrip502) RoundTrip(*http.Request) (*http.Response, error) {
	return nil, errors.New("something went wrong")
}

func TestErrorPage502(t *testing.T) {
	tests := []struct {
		name            string
		devMode         bool
		contentType     string
		responseSnippet string
	}{
		{
			name:            "production mode",
			contentType:     "text/plain",
			responseSnippet: "GitLab is not responding",
		},
		{
			name:            "development mode",
			devMode:         true,
			contentType:     "text/html",
			responseSnippet: "This page will automatically reload",
		},
	}

	for _, tc := range tests {
		t.Run(tc.name, func(t *testing.T) {
			req, err := http.NewRequest("GET", "/", nil)
			require.NoError(t, err, "build request")

			rt := NewRoundTripper(tc.devMode, roundtrip502{})
			response, err := rt.RoundTrip(req)
			require.NoError(t, err, "perform roundtrip")
			defer response.Body.Close()

			body, err := io.ReadAll(response.Body)
			require.NoError(t, err)

			require.Equal(t, tc.contentType, response.Header.Get("content-type"), "content type")
			require.Equal(t, 502, response.StatusCode, "response status")
			require.Contains(t, string(body), tc.responseSnippet)
		})
	}
}