This repository has been archived by the owner on Nov 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathmain_test.go
76 lines (61 loc) · 1.76 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
package main
import (
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/acouvreur/tinykv"
)
type ScalerMock struct {
isUp bool
}
func (s ScalerMock) IsUp(name string) bool {
return s.isUp
}
func (ScalerMock) ScaleUp(name string) error { return nil }
func (ScalerMock) ScaleDown(name string) error { return nil }
func TestOndemand_ServeHTTP(t *testing.T) {
testCases := []struct {
desc string
scaler ScalerMock
status string
statusCode int
contentType string
}{
{
desc: "service is starting",
status: "starting",
scaler: ScalerMock{isUp: false},
statusCode: http.StatusAccepted,
contentType: "text/plain",
},
{
desc: "service is started",
status: "started",
scaler: ScalerMock{isUp: true},
statusCode: http.StatusCreated,
contentType: "text/plain",
},
}
for _, test := range testCases {
test := test
t.Run(test.desc, func(t *testing.T) {
t.Logf("IsUp: %t", test.scaler.isUp)
request := httptest.NewRequest("GET", "/?name=whoami&timeout=5m", nil)
responseRecorder := httptest.NewRecorder()
store := tinykv.New[OnDemandRequestState](time.Second * 20)
onDemandHandler := onDemand(test.scaler, store)
onDemandHandler(responseRecorder, request)
body := responseRecorder.Body.String()
if responseRecorder.Code != test.statusCode {
t.Errorf("Want status '%d', got '%d'", test.statusCode, responseRecorder.Code)
}
if responseRecorder.Body.String() != test.status {
t.Errorf("Want body '%s', got '%s'", test.status, body)
}
if responseRecorder.Header().Get("Content-Type") != test.contentType {
t.Errorf("Want content type '%s', got '%s'", test.contentType, responseRecorder.Header().Get("Content-Type"))
}
})
}
}