-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi_test.go
112 lines (101 loc) · 3.72 KB
/
api_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
package envite
import (
"bytes"
"context"
"encoding/json"
"github.com/stretchr/testify/assert"
"io"
"net/http"
"net/http/httptest"
"testing"
"time"
)
func TestAPI(t *testing.T) {
component := &mockComponent{}
env, err := NewEnvironment(
"test-env",
NewComponentGraph().AddLayer(map[string]Component{"component": component}),
)
assert.NoError(t, err)
assert.NotNil(t, env)
call := func(handler http.Handler, request, response any) int {
var reqBody io.Reader
if request != nil {
var data []byte
data, err = json.Marshal(request)
assert.NoError(t, err)
reqBody = bytes.NewBuffer(data)
}
req := httptest.NewRequest(http.MethodGet, "/foo", reqBody)
if request != nil {
req.Header.Set(contentType, applicationJSON)
}
res := httptest.NewRecorder()
handler.ServeHTTP(res, req)
if response != nil {
var data []byte
data, err = io.ReadAll(res.Body)
assert.NoError(t, err)
err = json.Unmarshal(data, response)
assert.NoError(t, err)
}
return res.Code
}
getStatusResponse := GetStatusResponse{}
status := call(getStatusHandler{env: env}, nil, &getStatusResponse)
assert.Equal(t, http.StatusOK, status)
assert.Equal(t, "test-env", getStatusResponse.ID)
assert.Len(t, getStatusResponse.Components, 1)
assert.Len(t, getStatusResponse.Components[0], 1)
assert.Equal(t, "component", getStatusResponse.Components[0][0].ID)
assert.Equal(t, "mock", getStatusResponse.Components[0][0].Type)
status = call(postStartHandler{env: env}, postStartRequest{ComponentID: "invalid"}, nil)
assert.Equal(t, http.StatusInternalServerError, status)
status = call(postStartHandler{env: env}, postStartRequest{ComponentID: "component"}, nil)
assert.Equal(t, http.StatusOK, status)
assert.Equal(t, ComponentStatusRunning, component.status)
status = call(postStopHandler{env: env}, postStopRequest{ComponentID: "invalid"}, nil)
assert.Equal(t, http.StatusInternalServerError, status)
status = call(postStopHandler{env: env}, postStopRequest{ComponentID: "component"}, nil)
assert.Equal(t, http.StatusOK, status)
assert.Equal(t, ComponentStatusStopped, component.status)
status = call(postApplyHandler{env: env}, postApplyRequest{EnabledComponentIDs: []string{"component"}}, nil)
assert.Equal(t, http.StatusOK, status)
assert.Equal(t, ComponentStatusRunning, component.status)
status = call(postApplyHandler{env: env}, postApplyRequest{EnabledComponentIDs: []string{}}, nil)
assert.Equal(t, http.StatusOK, status)
assert.Equal(t, ComponentStatusStopped, component.status)
err = env.StartAll(context.Background())
assert.NoError(t, err)
assert.Equal(t, ComponentStatusRunning, component.status)
assert.False(t, component.cleanupCalled)
status = call(postStopAllHandler{env: env}, postStopAllRequest{Cleanup: true}, nil)
assert.Equal(t, http.StatusOK, status)
assert.True(t, component.cleanupCalled)
req := httptest.NewRequest(http.MethodGet, "/foo", nil)
res := httptest.NewRecorder()
ctx, cancel := context.WithCancel(context.Background())
req = req.WithContext(ctx)
startTime := time.Now()
go func() {
// wait one second and close client connection
time.Sleep(time.Second)
cancel()
}()
outputHandler := getOutputHandler{env: env}
outputHandler.ServeHTTP(res, req)
assert.True(t, time.Since(startTime) >= time.Second)
wHandler := newWebHandler()
req = httptest.NewRequest(http.MethodGet, "/", nil)
res = httptest.NewRecorder()
wHandler.ServeHTTP(res, req)
assert.Equal(t, http.StatusOK, res.Code)
req = httptest.NewRequest(http.MethodGet, "/favicon.ico", nil)
res = httptest.NewRecorder()
wHandler.ServeHTTP(res, req)
assert.Equal(t, http.StatusOK, res.Code)
req = httptest.NewRequest(http.MethodGet, "/", nil)
res = httptest.NewRecorder()
optionsHandler(res, req)
assert.Equal(t, http.StatusOK, res.Code)
}