-
Notifications
You must be signed in to change notification settings - Fork 1
/
run_test.go
151 lines (111 loc) · 3.89 KB
/
run_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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
//revive:disable:package-comments
package server_test
import (
"bytes"
"context"
"crypto/tls"
"fmt"
"io"
"net/http"
"os"
"testing"
"git.sonicoriginal.software/server.git/v2"
)
const portEnvKey = "TEST_PORT"
var (
certs []tls.Certificate
expectedResponse = []byte("hello")
)
type testHandler struct {
t *testing.T
}
func (handler *testHandler) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
if written, err := writer.Write(expectedResponse); err != nil {
handler.t.Logf("%v", err)
} else if written != len(expectedResponse) {
handler.t.Log("Could not write all bytes")
}
}
func verifyServerError(t *testing.T, serverErrorChannel chan server.Error, expectedErrorValue error) {
serverError := <-serverErrorChannel
if serverError.Close != nil {
t.Fatalf("Error closing server: %v", serverError.Close.Error())
}
contextError := serverError.Context.Error()
t.Logf("%v\n", contextError)
if contextError != expectedErrorValue.Error() {
t.Fatalf("Server failed unexpectedly: %v", contextError)
}
}
func TestRunCancel(t *testing.T) {
ctx, cancelFunction := context.WithCancel(context.Background())
address, serverErrorChannel := server.Run(ctx, &certs, nil, portEnvKey)
t.Logf("Serving on [%v]\n", address)
cancelFunction()
verifyServerError(t, serverErrorChannel, server.ErrContextCancelled)
}
func TestRunInterrupt(t *testing.T) {
ctx, cancelFunction := context.WithCancel(context.Background())
address, serverErrorChannel := server.Run(ctx, &certs, nil, portEnvKey)
t.Logf("Serving on [%v]\n", address)
pid := os.Getpid()
process, err := os.FindProcess(pid)
if err != nil {
t.Fatalf("Could not get test process: %v", err)
}
// FIXME During debug, only the debug process will get the signal and the
// entire debug process will be interrupted/stopped, rather than the listener
// process
if err = process.Signal(os.Interrupt); err != nil {
t.Fatalf("Error sending interrupt signal: %v", err)
}
verifyServerError(t, serverErrorChannel, server.ErrReceivedInterrupt)
cancelFunction()
}
func TestRunInvalidPort(t *testing.T) {
const invalidPort = "-8000"
t.Setenv(portEnvKey, invalidPort)
ctx, cancelFunction := context.WithCancel(context.Background())
address, serverErrorChannel := server.Run(ctx, &certs, nil, portEnvKey)
t.Logf("Serving on [%v]\n", address)
cancelFunction()
// Slow-clap, go devs. Absolutely marvelous error comparison work! /s
targetError := fmt.Errorf(fmt.Sprintf("listen tcp: address %v: invalid port", invalidPort))
verifyServerError(t, serverErrorChannel, targetError)
}
// TODO Write tests for the TLS HTTP server
func TestTLSServer(t *testing.T) {
t.Skip("Not yet implemented")
}
func TestRoundTrip(t *testing.T) {
const path = ""
h := &testHandler{t}
mux := http.NewServeMux()
t.Logf("Attempting initial handler registration at path [%v]\n", path)
route := server.RegisterHandler(path, h, mux)
t.Logf("Handler registered for route [%v]\n", route)
ctx, cancelFunction := context.WithCancel(context.Background())
address, serverErrorChannel := server.Run(ctx, &certs, mux, portEnvKey)
t.Logf("Serving on [%v]\n", address)
url := fmt.Sprintf("http://%v%v", address, route)
t.Logf("Requesting [%v]\n", url)
response, err := http.DefaultClient.Get(url)
if err != nil {
t.Fatalf("%v\n", err)
}
cancelFunction()
verifyServerError(t, serverErrorChannel, server.ErrContextCancelled)
t.Log("Response:")
t.Logf(" Status code: %v", response.StatusCode)
t.Logf(" Status text: %v", response.Status)
if response.Status != http.StatusText(http.StatusOK) && response.StatusCode != http.StatusOK {
t.Fatalf("Server returned: %v", response.Status)
}
responseBody, err := io.ReadAll(response.Body)
if err != nil {
t.Fatalf("Could not read response: %v", err)
} else if !bytes.Equal(responseBody, expectedResponse) {
t.Fatalf("%v != %v", responseBody, expectedResponse)
}
t.Logf(" Body: %v", string(responseBody))
}