Skip to content

Commit 584490e

Browse files
committed
add start option for hoverctl to enable middleware api
1 parent d876123 commit 584490e

File tree

3 files changed

+34
-3
lines changed

3 files changed

+34
-3
lines changed

functional-tests/hoverctl/start_test.go

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package hoverctl_suite
22

33
import (
4-
"github.com/antonholmquist/jason"
4+
"io"
55
"io/ioutil"
66
"strconv"
7+
"strings"
8+
9+
"github.com/antonholmquist/jason"
710

811
"github.com/SpectoLabs/hoverfly/core/authentication/backends"
912
"github.com/SpectoLabs/hoverfly/functional-tests"
@@ -327,7 +330,21 @@ var _ = Describe("hoverctl `start`", func() {
327330
})
328331
})
329332

330-
Context("with pac-file", func() {
333+
Context("with enable-middleware-api", func() {
334+
It("enables middleware API when flag is provided", func() {
335+
output := functional_tests.Run(hoverctlBinary, "start", "--enable-middleware-api")
336+
Expect(output).To(ContainSubstring("Hoverfly is now running"))
337+
338+
// Attempt to set middleware via Admin API should be allowed (200)
339+
req := sling.New().Put("http://localhost:8888/api/v2/hoverfly/middleware")
340+
// Use a valid echo middleware that reads from STDIN and outputs the JSON payload unchanged
341+
req.Body(strings.NewReader(`{"binary":"ruby", "script":"#!/usr/bin/env ruby\n# encoding: utf-8\nwhile payload = STDIN.gets\nnext unless payload\n\nSTDOUT.puts payload\nend"}`))
342+
res := functional_tests.DoRequest(req)
343+
Expect(res.StatusCode).To(Equal(200))
344+
})
345+
})
346+
347+
Context("with pac-file", func() {
331348
BeforeEach(func() {
332349
})
333350

@@ -338,7 +355,7 @@ var _ = Describe("hoverctl `start`", func() {
338355

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

hoverctl/cmd/start.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ hoverctl configuration file.
7878

7979
target.Simulations, _ = cmd.Flags().GetStringSlice("import")
8080

81+
// Feature flags
82+
target.EnableMiddlewareAPI, _ = cmd.Flags().GetBool("enable-middleware-api")
83+
8184
if pacFileLocation, _ := cmd.Flags().GetString("pac-file"); pacFileLocation != "" {
8285

8386
pacFileData, err := configuration.ReadFile(pacFileLocation)
@@ -199,4 +202,7 @@ func init() {
199202
startCmd.Flags().StringSlice("logs-output", []string{}, "Locations for log output, \"console\"(default) or \"file\"")
200203
startCmd.Flags().String("logs-file", "", "Log file name. Use \"hoverfly-<target name>.log\" if not provided")
201204
startCmd.Flags().String("log-level", "info", "Set log level (panic, fatal, error, warn, info or debug)")
205+
206+
// Feature flags
207+
startCmd.Flags().Bool("enable-middleware-api", false, "Enable the admin API to set middleware (PUT /api/v2/hoverfly/middleware)")
202208
}

hoverctl/configuration/target.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ type Target struct {
3333
ClientAuthenticationClientKey string `yaml:",omitempty"`
3434
ClientAuthenticationCACert string `yaml:",omitempty"`
3535

36+
// Feature flags
37+
EnableMiddlewareAPI bool `yaml:",omitempty"`
38+
3639
AuthEnabled bool
3740
Username string
3841
Password string
@@ -171,5 +174,10 @@ func (this Target) BuildFlags() Flags {
171174
}
172175
}
173176

177+
// Feature flags
178+
if this.EnableMiddlewareAPI {
179+
flags = append(flags, "-enable-middleware-api")
180+
}
181+
174182
return flags
175183
}

0 commit comments

Comments
 (0)