-
Notifications
You must be signed in to change notification settings - Fork 14
/
chaos_handler_test.go
58 lines (53 loc) · 1.39 KB
/
chaos_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
52
53
54
55
56
57
58
package nethttplibrary
import (
"github.com/stretchr/testify/assert"
nethttp "net/http"
"net/http/httptest"
"testing"
)
func TestItCreatesANewChaosHandler(t *testing.T) {
handler := NewChaosHandler()
if handler == nil {
t.Error("handler is nil")
}
}
func TestItCreatesANewChaosHandlerWithInvalidOptions(t *testing.T) {
_, err := NewChaosHandlerWithOptions(&ChaosHandlerOptions{
ChaosPercentage: 101,
ChaosStrategy: Random,
})
if err == nil || err.Error() != "ChaosPercentage must be between 0 and 100" {
t.Error("Expected initialization ")
}
}
func TestItCreatesANewChaosHandlerWithOptions(t *testing.T) {
options := &ChaosHandlerOptions{
ChaosPercentage: 100,
ChaosStrategy: Random,
StatusCode: 400,
}
handler, err := NewChaosHandlerWithOptions(options)
if err != nil {
t.Error(err)
}
if handler == nil {
t.Error("handler is nil")
}
testServer := httptest.NewServer(nethttp.HandlerFunc(func(res nethttp.ResponseWriter, req *nethttp.Request) {
res.WriteHeader(200)
_, err := res.Write([]byte("body"))
if err != nil {
t.Error(err)
}
}))
req, err := nethttp.NewRequest(nethttp.MethodGet, testServer.URL, nil)
if err != nil {
t.Error(err)
}
resp, err := handler.Intercept(newNoopPipeline(), 0, req)
if err != nil {
t.Error(err)
}
assert.NotNil(t, resp)
assert.Equal(t, 400, resp.StatusCode)
}