-
Notifications
You must be signed in to change notification settings - Fork 1
/
handler_test.go
51 lines (39 loc) · 1.23 KB
/
handler_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
package main
import (
"github.com/stretchr/testify/assert"
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
)
func TestRunHandler(t *testing.T) {
Cfg.GitlabToken = "testToken"
Cfg.Timeout = int64(time.Second)
handler := runHandler("env", "/env")
req := httptest.NewRequest("POST", "http://example.com/env", strings.NewReader(`KEY=value`))
req.Header.Set("X-Gitlab-Token", "testToken")
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
w := httptest.NewRecorder()
handler(w, req)
resp := w.Result()
body, _ := io.ReadAll(resp.Body)
assert.Equal(t, http.StatusOK, resp.StatusCode)
assert.Contains(t, string(body), "POST_KEY=value")
}
func TestPostTestHandler(t *testing.T) {
someJson := `{"Some": "json"}`
Cfg.GitlabToken = "testToken"
Cfg.Timeout = int64(time.Second)
handler := runHandler("cat", "/cat")
req := httptest.NewRequest("POST", "http://example.com/cat", strings.NewReader(someJson))
req.Header.Set("X-Gitlab-Token", "testToken")
req.Header.Set("Content-Type", "application/json")
w := httptest.NewRecorder()
handler(w, req)
resp := w.Result()
body, _ := io.ReadAll(resp.Body)
assert.Equal(t, http.StatusOK, resp.StatusCode)
assert.Contains(t, string(body), someJson)
}