Skip to content
File

Blob: gateway/apex_test.go

go161 lines
1package gateway
2 
3import (
4 "fmt"
5 "io"
6 "net/http"
7 "os"
8 "testing"
9 
10 "github.com/stretchr/testify/require"
11)
12 
13const (
14 testUser = "zzzAdminzzz"
15 testPass = "yyyPasswordzzz"
16)
17 
18func TestH2ApexIndex(t *testing.T) {
19 as := require.New(t)
20 
21 _, tcpPort, mockS, done := setupGateway(t, as, nil)
22 defer done()
23 
24 c := getH2Client("", tcpPort)
25 
26 resp, err := c.Get(fmt.Sprintf("https://%s/", testDomain))
27 as.NoError(err)
28 defer resp.Body.Close()
29 
30 b, err := io.ReadAll(resp.Body)
31 as.NoError(err)
32 
33 as.Contains(string(b), testDomain)
34 as.NotEmpty(resp.Header.Get("alt-svc"))
35 as.Equal("false", resp.Header.Get("http3"))
36 
37 mockS.AssertExpectations(t)
38}
39 
40func TestH3ApexIndex(t *testing.T) {
41 as := require.New(t)
42 
43 udpPort, _, mockS, done := setupGateway(t, as, nil)
44 defer done()
45 
46 c := getH3Client("", udpPort)
47 
48 resp, err := c.Get(fmt.Sprintf("https://%s/", testDomain))
49 as.NoError(err)
50 defer resp.Body.Close()
51 
52 b, err := io.ReadAll(resp.Body)
53 as.NoError(err)
54 
55 as.Contains(string(b), testDomain)
56 as.NotEmpty(resp.Header.Get("alt-svc"))
57 as.Equal("true", resp.Header.Get("http3"))
58 
59 mockS.AssertExpectations(t)
60}
61 
62func TestInternalWithAuth(t *testing.T) {
63 os.Setenv("INTERNAL_USER", testUser)
64 os.Setenv("INTERNAL_PASS", testPass)
65 defer func() {
66 os.Setenv("INTERNAL_USER", "")
67 os.Setenv("INTERNAL_PASS", "")
68 }()
69 
70 as := require.New(t)
71 
72 _, tcpPort, mockS, done := setupGateway(t, as, nil)
73 defer done()
74 
75 c := getH2Client("", tcpPort)
76 
77 req, err := http.NewRequest("GET", fmt.Sprintf("https://%s/_internal/chord/stats", testDomain), nil)
78 as.NoError(err)
79 req.SetBasicAuth(testUser, testPass)
80 
81 resp, err := c.Do(req)
82 as.NoError(err)
83 defer resp.Body.Close()
84 
85 as.Equal(http.StatusOK, resp.StatusCode)
86 as.NotEmpty(resp.Header.Get("alt-svc"))
87 
88 mockS.AssertExpectations(t)
89}
90 
91func TestInternalNoAuth(t *testing.T) {
92 os.Setenv("INTERNAL_USER", testUser)
93 os.Setenv("INTERNAL_PASS", testPass)
94 defer func() {
95 os.Setenv("INTERNAL_USER", "")
96 os.Setenv("INTERNAL_PASS", "")
97 }()
98 
99 as := require.New(t)
100 
101 _, tcpPort, mockS, done := setupGateway(t, as, nil)
102 defer done()
103 
104 c := getH2Client("", tcpPort)
105 
106 req, err := http.NewRequest("GET", fmt.Sprintf("https://%s/_internal/stats", testDomain), nil)
107 as.NoError(err)
108 
109 resp, err := c.Do(req)
110 as.NoError(err)
111 defer resp.Body.Close()
112 
113 as.Equal(http.StatusUnauthorized, resp.StatusCode)
114 as.NotEmpty(resp.Header.Get("alt-svc"))
115 
116 mockS.AssertExpectations(t)
117}
118 
119func TestInternalDisabled(t *testing.T) {
120 as := require.New(t)
121 
122 _, tcpPort, mockS, done := setupGateway(t, as, nil)
123 defer done()
124 
125 c := getH2Client("", tcpPort)
126 
127 req, err := http.NewRequest("GET", fmt.Sprintf("https://%s/_internal/stats", testDomain), nil)
128 as.NoError(err)
129 
130 resp, err := c.Do(req)
131 as.NoError(err)
132 defer resp.Body.Close()
133 
134 as.Equal(http.StatusNotFound, resp.StatusCode)
135 as.NotEmpty(resp.Header.Get("alt-svc"))
136 
137 mockS.AssertExpectations(t)
138}
139 
140func TestLogo(t *testing.T) {
141 as := require.New(t)
142 
143 udpPort, _, mockS, done := setupGateway(t, as, nil)
144 defer done()
145 
146 c := getH3Client("", udpPort)
147 
148 resp, err := c.Get(fmt.Sprintf("https://%s/quic.png", testDomain))
149 as.NoError(err)
150 defer resp.Body.Close()
151 
152 b, err := io.ReadAll(resp.Body)
153 as.NoError(err)
154 
155 as.EqualValues(quicPng, b)
156 as.NotEmpty(resp.Header.Get("alt-svc"))
157 as.Equal("true", resp.Header.Get("http3"))
158 
159 mockS.AssertExpectations(t)
160}