Skip to content

Commit da338f9

Browse files
committed
Merge pull request #187 from SpectoLabs/develop
Next release
2 parents 88cbcdf + caef1eb commit da338f9

File tree

26 files changed

+166
-543
lines changed

26 files changed

+166
-543
lines changed

.drone.yml

Lines changed: 0 additions & 4 deletions
This file was deleted.

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,4 @@ static/dist
4040
# IntelliJ
4141
.idea
4242
*.iml
43+
requests.db

admin.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -603,7 +603,7 @@ func (d *Hoverfly) StateHandler(w http.ResponseWriter, r *http.Request, next htt
603603
}
604604

605605
availableModes := map[string]bool{
606-
"virtualize": true,
606+
"simulate": true,
607607
"capture": true,
608608
"modify": true,
609609
"synthesize": true,
@@ -614,7 +614,7 @@ func (d *Hoverfly) StateHandler(w http.ResponseWriter, r *http.Request, next htt
614614
log.WithFields(log.Fields{
615615
"suppliedMode": sr.Mode,
616616
}).Error("Wrong mode found, can't change state")
617-
http.Error(w, "Bad mode supplied, available modes: virtualize, capture, modify, synthesize.", 400)
617+
http.Error(w, "Bad mode supplied, available modes: simulate, capture, modify, synthesize.", 400)
618618
return
619619
}
620620
log.WithFields(log.Fields{

admin_test.go

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ func TestGetState(t *testing.T) {
217217
m := getBoneRouter(*dbClient)
218218

219219
// setting initial mode
220-
dbClient.Cfg.SetMode(VirtualizeMode)
220+
dbClient.Cfg.SetMode(SimulateMode)
221221

222222
req, err := http.NewRequest("GET", "/api/state", nil)
223223
testutil.Expect(t, err, nil)
@@ -232,10 +232,10 @@ func TestGetState(t *testing.T) {
232232
sr := stateRequest{}
233233
err = json.Unmarshal(body, &sr)
234234

235-
testutil.Expect(t, sr.Mode, VirtualizeMode)
235+
testutil.Expect(t, sr.Mode, SimulateMode)
236236
}
237237

238-
func TestSetVirtualizeState(t *testing.T) {
238+
func TestSetSimulateState(t *testing.T) {
239239
server, dbClient := testTools(200, `{'message': 'here'}`)
240240
defer server.Close()
241241
defer dbClient.RequestCache.DeleteData()
@@ -246,7 +246,7 @@ func TestSetVirtualizeState(t *testing.T) {
246246

247247
// preparing to set mode through rest api
248248
var resp stateRequest
249-
resp.Mode = VirtualizeMode
249+
resp.Mode = SimulateMode
250250

251251
bts, err := json.Marshal(&resp)
252252
testutil.Expect(t, err, nil)
@@ -261,7 +261,7 @@ func TestSetVirtualizeState(t *testing.T) {
261261
testutil.Expect(t, rec.Code, http.StatusOK)
262262

263263
// checking mode
264-
testutil.Expect(t, dbClient.Cfg.GetMode(), VirtualizeMode)
264+
testutil.Expect(t, dbClient.Cfg.GetMode(), SimulateMode)
265265
}
266266

267267
func TestSetCaptureState(t *testing.T) {
@@ -270,8 +270,8 @@ func TestSetCaptureState(t *testing.T) {
270270
defer dbClient.RequestCache.DeleteData()
271271
m := getBoneRouter(*dbClient)
272272

273-
// setting mode to virtualize
274-
dbClient.Cfg.SetMode(VirtualizeMode)
273+
// setting mode to simulate
274+
dbClient.Cfg.SetMode(SimulateMode)
275275

276276
// preparing to set mode through rest api
277277
var resp stateRequest
@@ -299,8 +299,8 @@ func TestSetModifyState(t *testing.T) {
299299
defer dbClient.RequestCache.DeleteData()
300300
m := getBoneRouter(*dbClient)
301301

302-
// setting mode to virtualize
303-
dbClient.Cfg.SetMode(VirtualizeMode)
302+
// setting mode to simulate
303+
dbClient.Cfg.SetMode(SimulateMode)
304304

305305
// preparing to set mode through rest api
306306
var resp stateRequest
@@ -328,8 +328,8 @@ func TestSetSynthesizeState(t *testing.T) {
328328
defer dbClient.RequestCache.DeleteData()
329329
m := getBoneRouter(*dbClient)
330330

331-
// setting mode to virtualize
332-
dbClient.Cfg.SetMode(VirtualizeMode)
331+
// setting mode to simulate
332+
dbClient.Cfg.SetMode(SimulateMode)
333333

334334
// preparing to set mode through rest api
335335
var resp stateRequest
@@ -357,8 +357,8 @@ func TestSetRandomState(t *testing.T) {
357357
defer dbClient.RequestCache.DeleteData()
358358
m := getBoneRouter(*dbClient)
359359

360-
// setting mode to virtualize
361-
dbClient.Cfg.SetMode(VirtualizeMode)
360+
// setting mode to simulate
361+
dbClient.Cfg.SetMode(SimulateMode)
362362

363363
// preparing to set mode through rest api
364364
var resp stateRequest
@@ -377,7 +377,7 @@ func TestSetRandomState(t *testing.T) {
377377
testutil.Expect(t, rec.Code, http.StatusBadRequest)
378378

379379
// checking mode, should not have changed
380-
testutil.Expect(t, dbClient.Cfg.GetMode(), VirtualizeMode)
380+
testutil.Expect(t, dbClient.Cfg.GetMode(), SimulateMode)
381381
}
382382

383383
func TestSetNoBody(t *testing.T) {
@@ -386,8 +386,8 @@ func TestSetNoBody(t *testing.T) {
386386
defer dbClient.RequestCache.DeleteData()
387387
m := getBoneRouter(*dbClient)
388388

389-
// setting mode to virtualize
390-
dbClient.Cfg.SetMode(VirtualizeMode)
389+
// setting mode to simulate
390+
dbClient.Cfg.SetMode(SimulateMode)
391391

392392
// setting state
393393
req, err := http.NewRequest("POST", "/api/state", nil)
@@ -399,7 +399,7 @@ func TestSetNoBody(t *testing.T) {
399399
testutil.Expect(t, rec.Code, http.StatusBadRequest)
400400

401401
// checking mode, should not have changed
402-
testutil.Expect(t, dbClient.Cfg.GetMode(), VirtualizeMode)
402+
testutil.Expect(t, dbClient.Cfg.GetMode(), SimulateMode)
403403
}
404404

405405
func TestStatsHandler(t *testing.T) {
@@ -419,15 +419,15 @@ func TestStatsHandler(t *testing.T) {
419419
testutil.Expect(t, rec.Code, http.StatusOK)
420420
}
421421

422-
func TestStatsHandlerVirtualizeMetrics(t *testing.T) {
423-
// test metrics, increases virtualize count by 1 and then checks through stats
422+
func TestStatsHandlerSimulateMetrics(t *testing.T) {
423+
// test metrics, increases simulate count by 1 and then checks through stats
424424
// handler whether it is visible through /stats handler
425425
server, dbClient := testTools(200, `{'message': 'here'}`)
426426
defer server.Close()
427427
defer dbClient.RequestCache.DeleteData()
428428
m := getBoneRouter(*dbClient)
429429

430-
dbClient.Counter.Counters[VirtualizeMode].Inc(1)
430+
dbClient.Counter.Counters[SimulateMode].Inc(1)
431431

432432
req, err := http.NewRequest("GET", "/api/stats", nil)
433433

@@ -443,7 +443,7 @@ func TestStatsHandlerVirtualizeMetrics(t *testing.T) {
443443
sr := statsResponse{}
444444
err = json.Unmarshal(body, &sr)
445445

446-
testutil.Expect(t, int(sr.Stats.Counters[VirtualizeMode]), 1)
446+
testutil.Expect(t, int(sr.Stats.Counters[SimulateMode]), 1)
447447
}
448448

449449
func TestStatsHandlerCaptureMetrics(t *testing.T) {

cmd/hoverfly/main.go

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ var (
6969
addUser = flag.String("username", "", "username for new user")
7070
addPassword = flag.String("password", "", "password for new user")
7171
isAdmin = flag.Bool("admin", true, "supply '-admin false' to make this non admin user (defaults to 'true') ")
72-
authDisabled = flag.Bool("no-auth", false, "disabled authentication, currently it is enabled by default")
72+
authEnabled = flag.Bool("auth", false, "enable authentication, currently it is disabled by default")
7373

7474
generateCA = flag.Bool("generate-ca-cert", false, "generate CA certificate and private key for MITM")
7575
certName = flag.String("cert-name", "hoverfly.proxy", "cert name")
@@ -79,7 +79,7 @@ var (
7979

8080
tlsVerification = flag.Bool("tls-verification", true, "turn on/off tls verification for outgoing requests (will not try to verify certificates) - defaults to true")
8181

82-
databasePath = flag.String("db-dir", "", "database location - supply it if you want to provide specific to database (will be created there if it doesn't exist)")
82+
databasePath = flag.String("db-path", "", "database location - supply it to provide specific database location (will be created there if it doesn't exist)")
8383
database = flag.String("db", "boltdb", "Persistance storage to use - 'boltdb' or 'memory' which will not write anything to disk")
8484
)
8585

@@ -198,7 +198,7 @@ func main() {
198198
cfg.Middleware = *middleware
199199

200200
// setting default mode
201-
mode := hv.VirtualizeMode
201+
mode := hv.SimulateMode
202202

203203
if *capture {
204204
mode = hv.CaptureMode
@@ -232,8 +232,8 @@ func main() {
232232
cfg.SetMode(mode)
233233

234234
// disabling authentication if no-auth for auth disabled env variable
235-
if !cfg.AuthEnabled || *authDisabled {
236-
cfg.AuthEnabled = false
235+
if (*authEnabled) {
236+
cfg.AuthEnabled = true
237237
}
238238

239239
// disabling tls verification if flag or env variable is set to 'false' (defaults to true)
@@ -319,6 +319,20 @@ func main() {
319319
}
320320
}
321321

322+
// importing records if environment variable is set
323+
ev := os.Getenv(hv.HoverflyImportRecordsEV)
324+
if ev != "" {
325+
err := hoverfly.Import(ev)
326+
if err != nil {
327+
log.WithFields(log.Fields{
328+
"error": err.Error(),
329+
"import": ev,
330+
}).Fatal("Environment variable for importing was set but failed to import this resource")
331+
} else {
332+
err = hoverfly.MetadataCache.Set([]byte("import_from_env_variable"), []byte(ev))
333+
}
334+
}
335+
322336
// importing stuff
323337
if len(importFlags) > 0 {
324338
for i, v := range importFlags {
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Readthedocs client
22

33
This small Python script can query readthedocs API for 50 (you can change that) projects and then query them all.
4-
The main purpose is to showcase hoverfly and how it can capture and then virtualize HTTP(s) traffic.
4+
The main purpose is to showcase hoverfly and how it can capture and then simulate HTTP(s) traffic.
55

66
More information can be found in this blog post: https://www.specto.io/speeding-up-your-slow-dependencies/

hoverfly.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ import (
1919
"regexp"
2020
)
2121

22-
// VirtualizeMode - default mode when Hoverfly looks for captured requests to respond
23-
const VirtualizeMode = "virtualize"
22+
// SimulateMode - default mode when Hoverfly looks for captured requests to respond
23+
const SimulateMode = "simulate"
2424

2525
// SynthesizeMode - all requests are sent to middleware to create response
2626
const SynthesizeMode = "synthesize"
@@ -50,7 +50,7 @@ func GetNewHoverfly(cfg *Configuration, requestCache, metadataCache cache.Cache,
5050
TLSClientConfig: &tls.Config{InsecureSkipVerify: cfg.TLSVerification},
5151
}},
5252
Cfg: cfg,
53-
Counter: metrics.NewModeCounter([]string{VirtualizeMode, SynthesizeMode, ModifyMode, CaptureMode}),
53+
Counter: metrics.NewModeCounter([]string{SimulateMode, SynthesizeMode, ModifyMode, CaptureMode}),
5454
Hooks: make(ActionTypeHooks),
5555
MIN: GetNewMinifiers(),
5656
}

hoverfly_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ func TestProcessCaptureRequest(t *testing.T) {
6363
testutil.Expect(t, resp.StatusCode, 201)
6464
}
6565

66-
func TestProcessVirtualizeRequest(t *testing.T) {
66+
func TestProcessSimulateRequest(t *testing.T) {
6767
server, dbClient := testTools(201, `{'message': 'here'}`)
6868
defer server.Close()
6969
defer dbClient.RequestCache.DeleteData()
@@ -80,7 +80,7 @@ func TestProcessVirtualizeRequest(t *testing.T) {
8080
testutil.Expect(t, resp.StatusCode, 201)
8181

8282
// virtualizing
83-
dbClient.Cfg.SetMode(VirtualizeMode)
83+
dbClient.Cfg.SetMode(SimulateMode)
8484
newReq, newResp := dbClient.processRequest(r)
8585

8686
testutil.Refute(t, newReq, nil)

metrics/metrics_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"testing"
55
)
66

7-
func TestVirtualizeInc(t *testing.T) {
7+
func TestSimulateInc(t *testing.T) {
88
counter := NewModeCounter([]string{"name"})
99

1010
counter.Count("name")

models.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -536,7 +536,7 @@ func (d *Hoverfly) getResponse(req *http.Request) *http.Response {
536536
"value": string(payloadBts),
537537
"key": key,
538538
}).Error("Failed to decode payload")
539-
return hoverflyError(req, err, "Failed to virtualize", http.StatusInternalServerError)
539+
return hoverflyError(req, err, "Failed to simulate", http.StatusInternalServerError)
540540
}
541541

542542
c := NewConstructor(req, *payload)
@@ -549,7 +549,7 @@ func (d *Hoverfly) getResponse(req *http.Request) *http.Response {
549549

550550
log.WithFields(log.Fields{
551551
"key": key,
552-
"mode": VirtualizeMode,
552+
"mode": SimulateMode,
553553
"middleware": d.Cfg.Middleware,
554554
"path": req.URL.Path,
555555
"rawQuery": req.URL.RawQuery,

0 commit comments

Comments
 (0)