mirrored from https://git.sr.ht/~ancarda/tls-redirector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
http_test.go
108 lines (86 loc) · 2.95 KB
/
http_test.go
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package main
import (
"io"
"math/rand"
"net/http"
"net/http/httptest"
"reflect"
"testing"
"testing/fstest"
"testing/quick"
"time"
"github.com/stretchr/testify/assert"
)
const (
TextPlain = "text/plain"
TextHTML = "text/html; charset=utf-8"
)
func randomString() string {
v, ok := quick.Value(reflect.TypeOf(""),
rand.New(rand.NewSource(time.Now().Unix())))
if !ok {
panic("wasn't able to generate a string")
}
return v.String()
}
func assertionsCommonToAllResponses(t *testing.T, res *http.Response) {
assert.Equal(t, "nosniff", res.Header.Get("X-Content-Type-Options"))
assert.Equal(t, "tls-redirector/2.4", res.Header.Get("Server"))
}
func TestServer_ServeACME_404(t *testing.T) {
rr := httptest.NewRecorder()
r, _ := http.NewRequest(http.MethodGet, "http://nowhere.invalid/.well-known/acme-challenge/z", nil)
app{fstest.MapFS{}, true}.ServeHTTP(rr, r)
res := rr.Result()
assert.Equal(t, http.StatusNotFound, res.StatusCode)
assert.Equal(t, TextHTML, res.Header.Get("Content-Type"))
assertionsCommonToAllResponses(t, res)
body, _ := io.ReadAll(res.Body)
assert.Contains(t, string(body), "File Not Found")
}
func TestServer_ServeACME_HappyPath(t *testing.T) {
mapFS := fstest.MapFS{
"ok": &fstest.MapFile{Data: []byte("12345678")},
}
rr := httptest.NewRecorder()
r, _ := http.NewRequest(http.MethodGet, "http://nowhere.invalid/.well-known/acme-challenge/ok", nil)
app{mapFS, true}.ServeHTTP(rr, r)
res := rr.Result()
assert.Equal(t, http.StatusOK, res.StatusCode)
assert.Equal(t, TextPlain, res.Header.Get("Content-Type"))
assertionsCommonToAllResponses(t, res)
body, _ := io.ReadAll(res.Body)
assert.Equal(t, "12345678", string(body))
}
func TestServer_NoHostHeader_WillError(t *testing.T) {
rr := httptest.NewRecorder()
r, _ := http.NewRequest(http.MethodGet, "", nil)
app{}.ServeHTTP(rr, r)
res := rr.Result()
assert.Equal(t, http.StatusBadRequest, res.StatusCode)
assert.Equal(t, TextHTML, res.Header.Get("Content-Type"))
assertionsCommonToAllResponses(t, res)
body, _ := io.ReadAll(res.Body)
assert.Contains(t, string(body), "header is empty or wasn't sent")
}
func TestServer_IPAddressHostHeader_IsRejected(t *testing.T) {
rr := httptest.NewRecorder()
r, _ := http.NewRequest(http.MethodGet, "http://127.0.0.1/", nil)
app{}.ServeHTTP(rr, r)
res := rr.Result()
assert.Equal(t, http.StatusBadRequest, res.StatusCode)
assert.Equal(t, TextHTML, res.Header.Get("Content-Type"))
assertionsCommonToAllResponses(t, res)
body, _ := io.ReadAll(res.Body)
assert.Contains(t, string(body), "looks like an IP address")
}
func TestServer_HappyPath(t *testing.T) {
rr := httptest.NewRecorder()
r, _ := http.NewRequest(http.MethodGet, "http://nowhere.invalid/", nil)
app{}.ServeHTTP(rr, r)
res := rr.Result()
assert.Equal(t, http.StatusMovedPermanently, res.StatusCode)
assert.Equal(t, TextHTML, res.Header.Get("Content-Type"))
assertionsCommonToAllResponses(t, res)
assert.Equal(t, "https://nowhere.invalid/", res.Header.Get("Location"))
}