Skip to content

Commit

Permalink
Added --pac-file flag to hoverctl start
Browse files Browse the repository at this point in the history
  • Loading branch information
benjih committed Aug 23, 2018
1 parent 72d19ca commit 88ec7f6
Show file tree
Hide file tree
Showing 7 changed files with 161 additions and 0 deletions.
23 changes: 23 additions & 0 deletions functional-tests/hoverctl/start_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,4 +295,27 @@ var _ = Describe("hoverctl `start`", func() {
Expect(output).To(ContainSubstring("Port 8500 was not free"))
})
})

Context("with pac-file", func() {
BeforeEach(func() {
})

It("starts with pac file when defined", func() {
output := functional_tests.Run(hoverctlBinary, "start", "--pac-file", "testdata/test.pac")

Expect(output).To(ContainSubstring("Hoverfly is now running"))

response := functional_tests.DoRequest(sling.New().Get("http://localhost:8888/api/v2/hoverfly/pac"))
Expect(response.StatusCode).To(Equal(200))
responseBody, err := ioutil.ReadAll(response.Body)
Expect(err).To(BeNil())
Expect(string(responseBody)).To(ContainSubstring(`function FindProxyForURL(url, host) {`))
})

It("errors when pac file not found", func() {
output := functional_tests.Run(hoverctlBinary, "start", "--pac-file", "unknown.pac")

Expect(output).To(ContainSubstring("File not found: unknown.pac"))
})
})
})
18 changes: 18 additions & 0 deletions functional-tests/hoverctl/testdata/test.pac
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
function FindProxyForURL(url, host) {
// our local URLs from the domains below example.com don't need a proxy:
if (shExpMatch(host, "*.example.com"))
{
return "DIRECT";
}

// URLs within this network are accessed through
// port 8080 on fastproxy.example.com:
if (isInNet(host, "10.0.0.0", "255.255.248.0"))
{
return "PROXY fastproxy.example.com:8080";
}

// All other requests go through port 8080 of proxy.example.com.
// should that fail to respond, go directly to the WWW:
return "PROXY proxy.example.com:8080; DIRECT";
}
8 changes: 8 additions & 0 deletions hoverctl/cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,13 @@ hoverctl configuration file.
target.UpstreamProxyUrl, _ = cmd.Flags().GetString("upstream-proxy")
target.HttpsOnly, _ = cmd.Flags().GetBool("https-only")

if pacFileLocation, _ := cmd.Flags().GetString("pac-file"); pacFileLocation != "" {

pacFileData, err := configuration.ReadFile(pacFileLocation)
handleIfError(err)
target.PACFile = string(pacFileData)
}

if enableAuth, _ := cmd.Flags().GetBool("auth"); enableAuth {
username, _ := cmd.Flags().GetString("username")
password, _ := cmd.Flags().GetString("password")
Expand Down Expand Up @@ -124,6 +131,7 @@ func init() {
startCmd.Flags().String("key", "", "A path to a key file. Overrides the default Hoverfly TLS key")
startCmd.Flags().Bool("disable-tls", false, "Disables TLS verification")
startCmd.Flags().String("upstream-proxy", "", "A host for which Hoverfly will proxy its requests to")
startCmd.Flags().String("pac-file", "", "Configure upstream proxy by PAC file")
startCmd.Flags().Bool("https-only", false, "Disables insecure HTTP traffic in Hoverfly")
startCmd.Flags().String("listen-on-host", "", "Binds hoverfly listener to a host")

Expand Down
1 change: 1 addition & 0 deletions hoverctl/configuration/target.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type Target struct {
DisableTls bool `yaml:",omitempty"`

UpstreamProxyUrl string `yaml:",omitempty"`
PACFile string `yaml:",omitempty"`
HttpsOnly bool `yaml:",omitempty"`

AuthEnabled bool
Expand Down
5 changes: 5 additions & 0 deletions hoverctl/wrapper/hoverfly.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const (
v2ApiDestination = "/api/v2/hoverfly/destination"
v2ApiState = "/api/v2/state"
v2ApiMiddleware = "/api/v2/hoverfly/middleware"
v2ApiPac = "/api/v2/hoverfly/pac"
v2ApiCache = "/api/v2/cache"
v2ApiLogs = "/api/v2/logs"
v2ApiHoverfly = "/api/v2/hoverfly"
Expand Down Expand Up @@ -240,6 +241,10 @@ func Start(target *configuration.Target) error {
}
}

if target.PACFile != "" {
SetPACFile(*target)
}

return nil
}

Expand Down
21 changes: 21 additions & 0 deletions hoverctl/wrapper/pac.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package wrapper

import (
"github.com/SpectoLabs/hoverfly/hoverctl/configuration"
)

func SetPACFile(target configuration.Target) error {
response, err := doRequest(target, "PUT", v2ApiPac, target.PACFile, nil)
if err != nil {
return err
}

defer response.Body.Close()

err = handleResponseError(response, "Could not set PAC file")
if err != nil {
return err
}

return nil
}
85 changes: 85 additions & 0 deletions hoverctl/wrapper/pac_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package wrapper

import (
"testing"

"github.com/SpectoLabs/hoverfly/core/handlers/v2"
"github.com/SpectoLabs/hoverfly/core/matching/matchers"
. "github.com/onsi/gomega"
)

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

hoverfly.DeleteSimulation()
hoverfly.PutSimulation(v2.SimulationViewV5{
v2.DataViewV5{
RequestResponsePairs: []v2.RequestMatcherResponsePairViewV5{
v2.RequestMatcherResponsePairViewV5{
RequestMatcher: v2.RequestMatcherViewV5{
Method: []v2.MatcherViewV5{
{
Matcher: matchers.Exact,
Value: "PUT",
},
},
Path: []v2.MatcherViewV5{
{
Matcher: matchers.Exact,
Value: "/api/v2/hoverfly/pac",
},
},
},
Response: v2.ResponseDetailsViewV5{
Status: 200,
Body: `PACFILE`,
},
},
},
},
v2.MetaView{
SchemaVersion: "v2",
},
})

err := SetPACFile(target)
Expect(err).To(BeNil())
}

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

hoverfly.DeleteSimulation()
hoverfly.PutSimulation(v2.SimulationViewV5{
v2.DataViewV5{
RequestResponsePairs: []v2.RequestMatcherResponsePairViewV5{
v2.RequestMatcherResponsePairViewV5{
RequestMatcher: v2.RequestMatcherViewV5{
Method: []v2.MatcherViewV5{
{
Matcher: matchers.Exact,
Value: "PUT",
},
},
Path: []v2.MatcherViewV5{
{
Matcher: matchers.Exact,
Value: "/api/v2/hoverfly/pac",
},
},
},
Response: v2.ResponseDetailsViewV5{
Status: 400,
Body: `PACFILE`,
},
},
},
},
v2.MetaView{
SchemaVersion: "v2",
},
})

err := SetPACFile(target)
Expect(err).To(Not(BeNil()))
}

0 comments on commit 88ec7f6

Please sign in to comment.