forked from SpectoLabs/hoverfly
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhoverfly_test.go
111 lines (82 loc) · 2.62 KB
/
hoverfly_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
package hoverfly
import (
"bytes"
"io/ioutil"
"net/http"
"os"
"testing"
)
func TestGetNewHoverflyCheckConfig(t *testing.T) {
cfg := InitSettings()
cfg.DatabaseName = "testing2.db"
// getting boltDB
db := GetDB(cfg.DatabaseName)
cache := NewBoltDBCache(db, []byte(RequestsBucketName))
defer cache.CloseDB()
_, dbClient := GetNewHoverfly(cfg, cache)
expect(t, dbClient.Cfg, cfg)
// deleting this database
os.Remove(cfg.DatabaseName)
}
func TestProcessCaptureRequest(t *testing.T) {
server, dbClient := testTools(201, `{'message': 'here'}`)
defer server.Close()
defer dbClient.Cache.DeleteData()
r, err := http.NewRequest("GET", "http://somehost.com", nil)
expect(t, err, nil)
dbClient.Cfg.SetMode("capture")
req, resp := dbClient.processRequest(r)
refute(t, req, nil)
refute(t, resp, nil)
expect(t, resp.StatusCode, 201)
}
func TestProcessVirtualizeRequest(t *testing.T) {
server, dbClient := testTools(201, `{'message': 'here'}`)
defer server.Close()
defer dbClient.Cache.DeleteData()
r, err := http.NewRequest("GET", "http://somehost.com", nil)
expect(t, err, nil)
// capturing
dbClient.Cfg.SetMode("capture")
req, resp := dbClient.processRequest(r)
refute(t, req, nil)
refute(t, resp, nil)
expect(t, resp.StatusCode, 201)
// virtualizing
dbClient.Cfg.SetMode("virtualize")
newReq, newResp := dbClient.processRequest(r)
refute(t, newReq, nil)
refute(t, newResp, nil)
expect(t, newResp.StatusCode, 201)
}
func TestProcessSynthesizeRequest(t *testing.T) {
server, dbClient := testTools(201, `{'message': 'here'}`)
defer server.Close()
defer dbClient.Cache.DeleteData()
// getting reflect middleware
dbClient.Cfg.Middleware = "./examples/middleware/reflect_body/reflect_body.py"
bodyBytes := []byte("request_body_here")
r, err := http.NewRequest("GET", "http://somehost.com", ioutil.NopCloser(bytes.NewBuffer(bodyBytes)))
expect(t, err, nil)
dbClient.Cfg.SetMode("synthesize")
newReq, newResp := dbClient.processRequest(r)
refute(t, newReq, nil)
refute(t, newResp, nil)
expect(t, newResp.StatusCode, 200)
b, err := ioutil.ReadAll(newResp.Body)
expect(t, err, nil)
expect(t, string(b), string(bodyBytes))
}
func TestProcessModifyRequest(t *testing.T) {
server, dbClient := testTools(201, `{'message': 'here'}`)
defer server.Close()
// getting reflect middleware
dbClient.Cfg.Middleware = "./examples/middleware/modify_request/modify_request.py"
r, err := http.NewRequest("POST", "http://somehost.com", nil)
expect(t, err, nil)
dbClient.Cfg.SetMode("modify")
newReq, newResp := dbClient.processRequest(r)
refute(t, newReq, nil)
refute(t, newResp, nil)
expect(t, newResp.StatusCode, 202)
}