-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
121 lines (104 loc) · 3.74 KB
/
main_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
package main
import (
"bytes"
"fmt"
"github.com/patrickmn/go-cache"
"github.com/rs/zerolog/log"
"net/http"
"net/http/httptest"
"testing"
"time"
)
var (
XVaultToken = ""
jwt = ""
role = "my-app"
)
func setup() {
ttl = time.Duration(5 * time.Minute)
upstream = "http://localhost:8200"
port = 8201
c = cache.New(ttl, ttl*2)
}
func TestGetProxy(t *testing.T) {
setup()
// Create a new request to simulate a GET request to a specific path
c = cache.New(ttl, ttl*2)
path := fmt.Sprintf("http://localhost:8200/v1/app/%s", "my-catalog/dev/my-app/value1")
req := httptest.NewRequest("GET", path, nil)
// TODO: set your token here, if you want test
req.Header.Set("X-Vault-Token", XVaultToken)
// Create a ResponseRecorder to record the response
rr := httptest.NewRecorder()
// Call the proxyHandler function directly with the simulated request and response recorder
proxyHandler(rr, req)
proxyHandler(rr, req)
proxyHandler(rr, req)
proxyHandler(rr, req)
proxyHandler(rr, req)
// Check the response status code
if status := rr.Code; status != http.StatusOK {
t.Errorf("Handler returned wrong status code: got %v want %v", status, http.StatusOK)
}
}
func TestPostProxy(t *testing.T) {
setup()
jsonPayload := []byte(fmt.Sprintf(`{"role":"%s","jwt":"%s"}`, role, jwt))
// Create a ResponseRecorder to record the response
rr := httptest.NewRecorder()
// Call the proxyHandler function directly with the simulated request and response recorder
proxyHandler(rr, httptest.NewRequest("POST", "http://localhost:8200/v1/data/auth", bytes.NewBuffer(jsonPayload)))
// Check the response status code
if status := rr.Code; status != http.StatusOK {
t.Errorf("Handler returned wrong status code: got %v want %v", status, http.StatusOK)
}
proxyHandler(rr, httptest.NewRequest("POST", "http://localhost:8200/v1/data/auth", bytes.NewBuffer(jsonPayload)))
// Check the response status code
if status := rr.Code; status != http.StatusOK {
t.Errorf("Handler returned wrong status code: got %v want %v", status, http.StatusOK)
}
proxyHandler(rr, httptest.NewRequest("POST", "http://localhost:8200/v1/data/auth", bytes.NewBuffer(jsonPayload)))
// Check the response status code
if status := rr.Code; status != http.StatusOK {
t.Errorf("Handler returned wrong status code: got %v want %v", status, http.StatusOK)
}
proxyHandler(rr, httptest.NewRequest("POST", "http://localhost:8200/v1/data/auth", bytes.NewBuffer(jsonPayload)))
// Check the response status code
if status := rr.Code; status != http.StatusOK {
t.Errorf("Handler returned wrong status code: got %v want %v", status, http.StatusOK)
}
proxyHandler(rr, httptest.NewRequest("POST", "http://localhost:8200/v1/data/auth", bytes.NewBuffer(jsonPayload)))
// Check the response status code
if status := rr.Code; status != http.StatusOK {
t.Errorf("Handler returned wrong status code: got %v want %v", status, http.StatusOK)
}
}
func TestEncryptDecrypt(t *testing.T) {
inputStrings := []string{"hello"}
//inputStrings := []string{"hello", "world", "test", "encryption"}
key := "mysecretkeydfdfdfdfdfdfdfdf"
for _, input := range inputStrings {
// Encrypt the input string
log.Info().
Bytes("Data", []byte(input)).
Msg("Using data")
encrypted, err := encrypt([]byte(input), key)
if err != nil {
t.Errorf("Failed to encrypt string: %s", err)
continue
}
// Decrypt the encrypted string
decrypted, err := decrypt(encrypted, key)
if err != nil {
t.Errorf("Failed to decrypt string: %s", err)
continue
}
log.Info().
Bytes("data", decrypted).
Msg("Decrypted")
// Check that the decrypted string matches the original input string
if string(decrypted) != input {
t.Errorf("Decrypted string does not match input string: %s != %s", decrypted, input)
}
}
}