-
Notifications
You must be signed in to change notification settings - Fork 204
/
hoverfly_pac_handler.go
70 lines (57 loc) · 1.84 KB
/
hoverfly_pac_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
64
65
66
67
68
69
70
package v2
import (
"io/ioutil"
"net/http"
"github.com/SpectoLabs/hoverfly/core/handlers"
"github.com/codegangsta/negroni"
"github.com/go-zoo/bone"
)
type HoverflyPAC interface {
GetPACFile() []byte
SetPACFile([]byte)
DeletePACFile()
}
type HoverflyPACHandler struct {
Hoverfly HoverflyPAC
}
func (this *HoverflyPACHandler) RegisterRoutes(mux *bone.Mux, am *handlers.AuthHandler) {
mux.Get("/api/v2/hoverfly/pac", negroni.New(
negroni.HandlerFunc(am.RequireTokenAuthentication),
negroni.HandlerFunc(this.Get),
))
mux.Put("/api/v2/hoverfly/pac", negroni.New(
negroni.HandlerFunc(am.RequireTokenAuthentication),
negroni.HandlerFunc(this.Put),
))
mux.Delete("/api/v2/hoverfly/pac", negroni.New(
negroni.HandlerFunc(am.RequireTokenAuthentication),
negroni.HandlerFunc(this.Delete),
))
mux.Options("/api/v2/hoverfly/pac", negroni.New(
negroni.HandlerFunc(this.Options),
))
}
func (this *HoverflyPACHandler) Get(w http.ResponseWriter, req *http.Request, next http.HandlerFunc) {
pacFile := this.Hoverfly.GetPACFile()
if pacFile == nil {
handlers.WriteErrorResponse(w, "Not found", 404)
}
handlers.WriteResponseWithContentType(w, pacFile, "application/x-ns-proxy-autoconfig")
}
func (this *HoverflyPACHandler) Put(w http.ResponseWriter, req *http.Request, next http.HandlerFunc) {
bodyBytes, err := ioutil.ReadAll(req.Body)
if err != nil {
handlers.WriteErrorResponse(w, err.Error(), 400)
return
}
this.Hoverfly.SetPACFile(bodyBytes)
this.Get(w, req, next)
}
func (this *HoverflyPACHandler) Delete(w http.ResponseWriter, req *http.Request, next http.HandlerFunc) {
this.Hoverfly.DeletePACFile()
handlers.WriteResponse(w, []byte(""))
}
func (this *HoverflyPACHandler) Options(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
w.Header().Add("Allow", "OPTIONS, GET, PUT, DELETE")
handlers.WriteResponse(w, []byte(""))
}