-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_test.go
218 lines (176 loc) · 5.77 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
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package main
import (
"bytes"
"encoding/base64"
"encoding/json"
"net/http"
"net/http/httptest"
"os"
"strings"
"testing"
)
func setUp() {
listenAddr = "0.0.0.0"
listenPort = 8080
authKey = ""
urlSubPath = "/"
}
func TestYaml2JsonFromBody(t *testing.T) {
setUp()
yamlData := `
foo: bar
baz:
- qux
- quux
`
req := httptest.NewRequest("POST", "/", bytes.NewBufferString(yamlData))
req.Header.Set("Content-Type", "application/x-yaml")
rr := httptest.NewRecorder()
httpHandler(rr, req)
if status := rr.Code; status != http.StatusOK {
t.Errorf("Handler returned wrong status code: got %v want %v", status, http.StatusOK)
}
expectedJSON := `{"baz":["qux","quux"],"foo":"bar"}`
if strings.TrimSpace(rr.Body.String()) != expectedJSON {
t.Errorf("Handler returned unexpected body: got %v want %v", rr.Body.String(), expectedJSON)
}
}
func TestYaml2JsonFromURL(t *testing.T) {
setUp()
// Start a test server to serve YAML content
yamlContent := `
foo: bar
numbers:
- 1
- 2
`
testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/x-yaml")
_, _ = w.Write([]byte(yamlContent))
}))
defer testServer.Close()
req := httptest.NewRequest("GET", "/?url="+testServer.URL, nil)
rr := httptest.NewRecorder()
httpHandler(rr, req)
if status := rr.Code; status != http.StatusOK {
t.Errorf("Handler returned wrong status code: got %v want %v", status, http.StatusOK)
}
expectedJSON := `{"foo":"bar","numbers":[1,2]}`
if strings.TrimSpace(rr.Body.String()) != expectedJSON {
t.Errorf("Handler returned unexpected body: got %v want %v", rr.Body.String(), expectedJSON)
}
}
func TestAuthKeyInURL(t *testing.T) {
setUp()
authKey = "secret"
yamlData := `foo: bar`
req := httptest.NewRequest("POST", "/?key=secret", bytes.NewBufferString(yamlData))
req.Header.Set("Content-Type", "application/x-yaml")
rr := httptest.NewRecorder()
httpHandler(rr, req)
if status := rr.Code; status != http.StatusOK {
t.Errorf("Handler returned wrong status code: got %v want %v", status, http.StatusOK)
}
}
func TestHttpBasicAuth(t *testing.T) {
setUp()
authKey = "secret"
yamlData := `foo: bar`
req := httptest.NewRequest("POST", "/", bytes.NewBufferString(yamlData))
req.Header.Set("Content-Type", "application/x-yaml")
// Set Basic Auth header
auth := ":" + authKey
encodedAuth := base64.StdEncoding.EncodeToString([]byte(auth))
req.Header.Set("Authorization", "Basic "+encodedAuth)
rr := httptest.NewRecorder()
httpHandler(rr, req)
if status := rr.Code; status != http.StatusOK {
t.Errorf("Handler returned wrong status code: got %v want %v", status, http.StatusOK)
}
}
func TestUnauthorized(t *testing.T) {
setUp()
authKey = "secret"
yamlData := `foo: bar`
req := httptest.NewRequest("POST", "/", bytes.NewBufferString(yamlData))
req.Header.Set("Content-Type", "application/x-yaml")
rr := httptest.NewRecorder()
httpHandler(rr, req)
if status := rr.Code; status != http.StatusUnauthorized {
t.Errorf("Handler returned wrong status code: got %v want %v", status, http.StatusUnauthorized)
}
var resp map[string]string
_ = json.Unmarshal(rr.Body.Bytes(), &resp)
if resp["error"] != "Unauthorized" {
t.Errorf("Expected Unauthorized error, got %v", resp["error"])
}
}
func TestNoYAMLProvided(t *testing.T) {
setUp()
req := httptest.NewRequest("POST", "/", nil)
rr := httptest.NewRecorder()
httpHandler(rr, req)
if status := rr.Code; status != http.StatusBadRequest {
t.Errorf("Handler returned wrong status code: got %v want %v", status, http.StatusBadRequest)
}
var resp map[string]string
_ = json.Unmarshal(rr.Body.Bytes(), &resp)
if resp["error"] != "No YAML to convert" {
t.Errorf("Expected 'No YAML to convert' error, got %v", resp["error"])
}
}
func TestInvalidYAML(t *testing.T) {
setUp()
invalidYAML := `foo: bar: baz`
req := httptest.NewRequest("POST", "/", bytes.NewBufferString(invalidYAML))
req.Header.Set("Content-Type", "application/x-yaml")
rr := httptest.NewRecorder()
httpHandler(rr, req)
if status := rr.Code; status != http.StatusInternalServerError {
t.Errorf("Handler returned wrong status code: got %v want %v", status, http.StatusInternalServerError)
}
var resp map[string]string
_ = json.Unmarshal(rr.Body.Bytes(), &resp)
if resp["error"] != "Failed to convert YAML to JSON" {
t.Errorf("Expected 'Failed to convert YAML to JSON' error, got %v", resp["error"])
}
}
func TestInvalidURLParameter(t *testing.T) {
setUp()
req := httptest.NewRequest("GET", "/?url=invalid-url", nil)
rr := httptest.NewRecorder()
httpHandler(rr, req)
if status := rr.Code; status != http.StatusBadRequest {
t.Errorf("Handler returned wrong status code: got %v want %v", status, http.StatusBadRequest)
}
var resp map[string]string
_ = json.Unmarshal(rr.Body.Bytes(), &resp)
if resp["error"] != "Failed to fetch YAML from given URL" {
t.Errorf("Expected 'Failed to fetch YAML from given URL' error, got %v", resp["error"])
}
}
func TestEnvironmentVariables(t *testing.T) {
// Set environment variables
_ = os.Setenv("Y2JS_LISTEN_ADDR", "127.0.0.1")
_ = os.Setenv("Y2JS_LISTEN_PORT", "9090")
_ = os.Setenv("Y2JS_AUTH_KEY", "envsecret")
_ = os.Setenv("Y2JS_URL_SUB_PATH", "/convert")
parseConfigs()
if listenAddr != "127.0.0.1" {
t.Errorf("Expected listenAddr to be '127.0.0.1', got %v", listenAddr)
}
if listenPort != 9090 {
t.Errorf("Expected listenPort to be 9090, got %v", listenPort)
}
if authKey != "envsecret" {
t.Errorf("Expected authKey to be 'envsecret', got %v", authKey)
}
if urlSubPath != "/convert" {
t.Errorf("Expected urlSubPath to be '/convert', got %v", urlSubPath)
}
// Clean up environment variables
_ = os.Unsetenv("Y2JS_LISTEN_ADDR")
_ = os.Unsetenv("Y2JS_LISTEN_PORT")
_ = os.Unsetenv("Y2JS_AUTH_KEY")
_ = os.Unsetenv("Y2JS_URL_SUB_PATH")
}