Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Hoverfly configuration by PAC file #766

Merged
merged 12 commits into from
Aug 23, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
31 changes: 30 additions & 1 deletion Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,7 @@
[[constraint]]
branch = "master"
name = "github.com/icrowley/fake"

[[constraint]]
branch = "master"
name = "github.com/jackwakefield/gopac"
1 change: 1 addition & 0 deletions core/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ func getAllHandlers(hoverfly *Hoverfly) []handlers.AdminHandler {
&v2.HoverflyUsageHandler{Hoverfly: hoverfly},
&v2.HoverflyVersionHandler{Hoverfly: hoverfly},
&v2.HoverflyUpstreamProxyHandler{Hoverfly: hoverfly},
&v2.HoverflyPACHandler{Hoverfly: hoverfly},
&v2.SimulationHandler{Hoverfly: hoverfly},
&v2.CacheHandler{Hoverfly: hoverfly},
&v2.LogsHandler{Hoverfly: hoverfly.StoreLogsHook},
Expand Down
68 changes: 68 additions & 0 deletions core/handlers/v2/hoverfly_pac_handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
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.WriteResponse(w, pacFile)
w.Header().Set("Content-Type", "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()
}

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(""))
}
114 changes: 114 additions & 0 deletions core/handlers/v2/hoverfly_pac_handler_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package v2

import (
"bytes"
"io/ioutil"
"net/http"
"testing"

. "github.com/onsi/gomega"
)

type HoverflyPacStub struct {
PACFile []byte
}

func (this HoverflyPacStub) GetPACFile() []byte {
return this.PACFile
}

func (this *HoverflyPacStub) SetPACFile(PACFile []byte) {
this.PACFile = PACFile
}

func (this *HoverflyPacStub) DeletePACFile() {
this.PACFile = nil
}

func Test_HoverflyPACHandler_Get_ReturnsPACfile(t *testing.T) {
RegisterTestingT(t)

stubHoverfly := &HoverflyPacStub{
PACFile: []byte("PACFILE"),
}

unit := HoverflyPACHandler{Hoverfly: stubHoverfly}

request, err := http.NewRequest("GET", "", nil)
Expect(err).To(BeNil())

response := makeRequestOnHandler(unit.Get, request)

Expect(response.Code).To(Equal(http.StatusOK))
Expect(response.Header().Get("Content-Type")).To(Equal("application/x-ns-proxy-autoconfig"))

bodyBytes, err := ioutil.ReadAll(response.Body)
Expect(err).To(BeNil())

Expect(string(bodyBytes)).To(Equal("PACFILE"))
}

func Test_HoverflyPACHandler_Get_Returns404IfNotSet(t *testing.T) {
RegisterTestingT(t)

stubHoverfly := &HoverflyPacStub{}

unit := HoverflyPACHandler{Hoverfly: stubHoverfly}

request, err := http.NewRequest("GET", "", nil)
Expect(err).To(BeNil())

response := makeRequestOnHandler(unit.Get, request)

Expect(response.Code).To(Equal(http.StatusNotFound))

errorViewResponse, err := unmarshalErrorView(response.Body)
Expect(err).To(BeNil())

Expect(errorViewResponse.Error).To(Equal("Not found"))
}

func Test_HoverflyPACHandler_Put_ReturnsPACfile(t *testing.T) {
RegisterTestingT(t)

stubHoverfly := &HoverflyPacStub{}

unit := HoverflyPACHandler{Hoverfly: stubHoverfly}

request, err := http.NewRequest("PUT", "", ioutil.NopCloser(bytes.NewBuffer([]byte("PACFILE"))))
Expect(err).To(BeNil())

response := makeRequestOnHandler(unit.Put, request)

Expect(response.Code).To(Equal(http.StatusOK))
Expect(response.Header().Get("Content-Type")).To(Equal("application/x-ns-proxy-autoconfig"))

bodyBytes, err := ioutil.ReadAll(response.Body)
Expect(err).To(BeNil())

Expect(string(bodyBytes)).To(Equal("PACFILE"))
Expect(string(stubHoverfly.PACFile)).To(Equal("PACFILE"))
}

func Test_HoverflyPACHandler_Delete_DeletesPACfile(t *testing.T) {
RegisterTestingT(t)

stubHoverfly := &HoverflyPacStub{
PACFile: []byte("PACFILE"),
}

unit := HoverflyPACHandler{Hoverfly: stubHoverfly}

request, err := http.NewRequest("DELETE", "", nil)
Expect(err).To(BeNil())

response := makeRequestOnHandler(unit.Delete, request)

Expect(response.Code).To(Equal(http.StatusOK))

bodyBytes, err := ioutil.ReadAll(response.Body)
Expect(err).To(BeNil())

Expect(string(bodyBytes)).To(Equal(""))
Expect(stubHoverfly.PACFile).To(BeNil())
}
26 changes: 0 additions & 26 deletions core/hoverfly.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package hoverfly

import (
"crypto/tls"
"fmt"
"net"
"net/http"
"net/url"
"sync"

log "github.com/Sirupsen/logrus"
Expand Down Expand Up @@ -123,30 +121,6 @@ func GetNewHoverfly(cfg *Configuration, requestCache cache.Cache, authentication
return hoverfly
}

func GetDefaultHoverflyHTTPClient(tlsVerification bool, upstreamProxy string) *http.Client {

var proxyURL func(*http.Request) (*url.URL, error)
if upstreamProxy == "" {
proxyURL = http.ProxyURL(nil)
} else {
u, err := url.Parse(upstreamProxy)
if err != nil {
log.Fatalf("Could not parse upstream proxy: ", err.Error())
}
proxyURL = http.ProxyURL(u)
}

return &http.Client{CheckRedirect: func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
}, Transport: &http.Transport{
Proxy: proxyURL,
TLSClientConfig: &tls.Config{
InsecureSkipVerify: !tlsVerification,
Renegotiation: tls.RenegotiateFreelyAsClient,
},
}}
}

// StartProxy - starts proxy with current configuration, this method is non blocking.
func (hf *Hoverfly) StartProxy() error {

Expand Down
6 changes: 5 additions & 1 deletion core/hoverfly_funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ func (hf *Hoverfly) DoRequest(request *http.Request) (*http.Response, error) {

request.Body = ioutil.NopCloser(bytes.NewReader(requestBody))

resp, err := hf.HTTP.Do(request)
client, err := GetHttpClient(hf, request.Host)
if err != nil {
return nil, err
}
resp, err := client.Do(request)

request.Body = ioutil.NopCloser(bytes.NewReader(requestBody))
if err != nil {
Expand Down
15 changes: 15 additions & 0 deletions core/hoverfly_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -311,3 +311,18 @@ func (this *Hoverfly) AddDiff(requestView v2.SimpleRequestDefinitionView, diffRe
this.responsesDiff[requestView] = append(diffs, diffReport)
}
}

func (this *Hoverfly) GetPACFile() []byte {
return this.Cfg.PACFile
}

func (this *Hoverfly) SetPACFile(pacFile []byte) {
if len(pacFile) == 0 {
pacFile = nil
}
this.Cfg.PACFile = pacFile
}

func (this *Hoverfly) DeletePACFile() {
this.Cfg.PACFile = nil
}
44 changes: 44 additions & 0 deletions core/hoverfly_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -946,3 +946,47 @@ func Test_Hoverfly_AddDiff_DoesntAddDiffReport_NoEntries(t *testing.T) {

Expect(unit.responsesDiff).To(HaveLen(0))
}

func Test_Hoverfly_GetPACFile_GetsPACFile(t *testing.T) {
RegisterTestingT(t)

unit := NewHoverflyWithConfiguration(&Configuration{
PACFile: []byte("PACFILE"),
})

Expect(string(unit.GetPACFile())).To(Equal("PACFILE"))
}

func Test_Hoverfly_SetPACFile_SetsPACFile(t *testing.T) {
RegisterTestingT(t)

unit := NewHoverflyWithConfiguration(&Configuration{})

unit.SetPACFile([]byte("PACFILE"))

Expect(string(unit.Cfg.PACFile)).To(Equal("PACFILE"))
}

func Test_Hoverfly_SetPACFile_SetsPACFileToNilIfEmpty(t *testing.T) {
RegisterTestingT(t)

unit := NewHoverflyWithConfiguration(&Configuration{
PACFile: []byte("PACFILE"),
})

unit.SetPACFile([]byte(""))

Expect(unit.Cfg.PACFile).To(BeNil())
}

func Test_Hoverfly_DeletePACFile(t *testing.T) {
RegisterTestingT(t)

unit := NewHoverflyWithConfiguration(&Configuration{
PACFile: []byte("PACFILE"),
})

unit.DeletePACFile()

Expect(unit.Cfg.PACFile).To(BeNil())
}
Loading