/
hoverfly_mode_handler.go
63 lines (50 loc) · 1.55 KB
/
hoverfly_mode_handler.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
package v2
import (
"encoding/json"
"net/http"
"github.com/SpectoLabs/hoverfly/core/handlers"
"github.com/codegangsta/negroni"
"github.com/go-zoo/bone"
)
type HoverflyMode interface {
GetMode() ModeView
SetModeWithArguments(ModeView) error
}
type HoverflyModeHandler struct {
Hoverfly HoverflyMode
}
func (this *HoverflyModeHandler) RegisterRoutes(mux *bone.Mux, am *handlers.AuthHandler) {
mux.Get("/api/v2/hoverfly/mode", negroni.New(
negroni.HandlerFunc(am.RequireTokenAuthentication),
negroni.HandlerFunc(this.Get),
))
mux.Put("/api/v2/hoverfly/mode", negroni.New(
negroni.HandlerFunc(am.RequireTokenAuthentication),
negroni.HandlerFunc(this.Put),
))
mux.Options("/api/v2/hoverfly/mode", negroni.New(
negroni.HandlerFunc(this.Options),
))
}
func (this *HoverflyModeHandler) Get(w http.ResponseWriter, req *http.Request, next http.HandlerFunc) {
bytes, _ := json.Marshal(this.Hoverfly.GetMode())
handlers.WriteResponse(w, bytes)
}
func (this *HoverflyModeHandler) Put(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
var modeView ModeView
err := handlers.ReadFromRequest(r, &modeView)
if err != nil {
handlers.WriteErrorResponse(w, err.Error(), 400)
return
}
err = this.Hoverfly.SetModeWithArguments(modeView)
if err != nil {
handlers.WriteErrorResponse(w, err.Error(), http.StatusBadRequest)
return
}
this.Get(w, r, next)
}
func (this *HoverflyModeHandler) Options(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
w.Header().Add("Allow", "OPTIONS, GET, PUT")
handlers.WriteResponse(w, []byte(""))
}