-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetadata-endpoint_test.go
97 lines (76 loc) · 2.32 KB
/
metadata-endpoint_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
package main
import (
"bytes"
"net/http"
"net/http/httptest"
"os"
"testing"
"time"
)
func TestFileHandler(t *testing.T) {
handler := fileHandler("testdata/cert.pem")
handler.ServeHTTP(httptest.NewRecorder(), httptest.NewRequest("GET", "/load-balancer/cert.pem", nil))
// Assertions to check the response for cert.pem
recorder := httptest.NewRecorder()
request := httptest.NewRequest("GET", "/load-balancer/cert.pem", nil)
handler = fileHandler("testdata/cert.pem")
handler.ServeHTTP(recorder, request)
// Check status code
if recorder.Code != http.StatusOK {
t.Errorf("Expected status code %d, got %d", http.StatusOK, recorder.Code)
}
// Check response body
expectedData := []byte("test cert")
if !bytes.Equal(recorder.Body.Bytes(), expectedData) {
t.Error("Response body does not match expected content")
}
// Test limiter
for i := 0; i < 15; i++ {
recorder = httptest.NewRecorder()
handler.ServeHTTP(recorder, request)
}
if recorder.Code != http.StatusTooManyRequests {
t.Errorf("Expected status code %d for rate limit exceeded, got %d", http.StatusTooManyRequests, recorder.Code)
}
time.Sleep(1 * time.Second)
// Test non-existent file
recorder = httptest.NewRecorder()
handler = fileHandler("testdata/nonexistent.pem")
handler.ServeHTTP(recorder, request)
if recorder.Code != http.StatusNotFound {
t.Errorf("Expected status code %d for non-existent file, got %d", http.StatusNotFound, recorder.Code)
}
}
func TestMainInternal(t *testing.T) {
// Test with missing VM_INHOST_NAME
os.Setenv("VM_INHOST_NAME", "")
code, err := mainInternal(http.ListenAndServe)
if err == nil {
t.Error("Expected an error, got nil")
}
if code != 1 {
t.Errorf("Expected return code %d, got %d", 1, code)
}
os.Setenv("VM_INHOST_NAME", "test")
os.Setenv("IPV6_ADDRESS", "")
code, err = mainInternal(http.ListenAndServe)
if err == nil {
t.Error("Expected an error, got nil")
}
if code != 1 {
t.Errorf("Expected return code %d, got %d", 1, code)
}
os.Setenv("IPV6_ADDRESS", "test")
code, err = mainInternal(mockListenAndServe)
if err != nil {
t.Error("Expected no error, got", err)
}
if code != 0 {
t.Errorf("Expected return code %d, got %d", 0, code)
}
os.Unsetenv("VM_INHOST_NAME")
os.Unsetenv("IPV6_ADDRESS")
}
func mockListenAndServe(addr string, handler http.Handler) error {
return nil
}