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

add bodyFile to response part of pair #893

Merged
merged 10 commits into from
Mar 8, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion core/benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ func BenchmarkProcessRequest(b *testing.B) {

for _, bm := range benchmarks {
hoverfly.DeleteSimulation()
hoverfly.PutSimulation(bm.simulation)
hoverfly.PutSimulation(bm.simulation, false)

b.Run(bm.name, func(b *testing.B) {

Expand Down
12 changes: 12 additions & 0 deletions core/cmd/hoverfly/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ func (i *arrayFlags) Set(value string) error {

var importFlags arrayFlags
var destinationFlags arrayFlags
var responseBodyFilesPath string

const boltBackend = "boltdb"
const inmemoryBackend = "memory"
Expand Down Expand Up @@ -179,9 +180,18 @@ func init() {
func main() {
hoverfly := hv.NewHoverfly()

dir, err := os.Getwd()
if err != nil {
fmt.Println(err)
os.Exit(1)
}

Comment on lines +183 to +188
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this needed? I thought if the root path is empty, the default resolving location will be the cwd?

Copy link
Contributor Author

@ns3777k ns3777k Mar 8, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes 'cause getting the cwd can return an error (e.g. http://man7.org/linux/man-pages/man2/getcwd.2.html).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But I'll double check if we can ignore the error and keep the dir variable to be an empty string. Thus we just delay the error handling and it'll occur when a bodyFile is not able to be read from in case it contains a relative path.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what I meant is:

ioutil.ReadFile(filepath.Join("", pair.Response.GetBodyFile()))

should read the file under the current working directory, right?
so setting the response-body-files-path to "" should be equivalent to setting it to os.Getwd()?

Copy link
Contributor Author

@ns3777k ns3777k Mar 8, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, yes. I used the os.Getwd to show the current dir in --help information, otherwise there will be an empty string there. Should I remove it?

flag.Var(&importFlags, "import", "Import from file or from URL (i.e. '-import my_service.json' or '-import http://mypage.com/service_x.json'")
flag.Var(&destinationFlags, "dest", "Specify which hosts to process (i.e. '-dest fooservice.org -dest barservice.org -dest catservice.org') - other hosts will be ignored will passthrough'")
flag.StringVar(&responseBodyFilesPath, "response-body-files-path", dir, "When a response contains a relative bodyFile, it will be resolved against this path")

flag.Parse()

if *logsFormat == "json" {
log.SetFormatter(&log.JSONFormatter{})
} else {
Expand Down Expand Up @@ -344,6 +354,8 @@ func main() {
cfg.Destination = *destination
}

cfg.ResponsesBodyFilesPath = responseBodyFilesPath

var requestCache cache.FastCache
var tokenCache cache.Cache
var userCache cache.Cache
Expand Down
11 changes: 3 additions & 8 deletions core/handlers/v2/simulation_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ package v2
import (
"encoding/json"
"fmt"
"net/http"

"io/ioutil"
"net/http"

"github.com/SpectoLabs/hoverfly/core/handlers"
"github.com/SpectoLabs/hoverfly/core/util"
Expand All @@ -18,7 +17,7 @@ import (
type HoverflySimulation interface {
GetSimulation() (SimulationViewV6, error)
GetFilteredSimulation(string) (SimulationViewV6, error)
PutSimulation(SimulationViewV6) SimulationImportResult
PutSimulation(SimulationViewV6, bool) SimulationImportResult
DeleteSimulation()
}

Expand Down Expand Up @@ -124,11 +123,7 @@ func (this *SimulationHandler) addSimulation(w http.ResponseWriter, req *http.Re
return err
}

if overrideExisting {
this.Hoverfly.DeleteSimulation()
}

result := this.Hoverfly.PutSimulation(simulationView)
result := this.Hoverfly.PutSimulation(simulationView, overrideExisting)
if result.err != nil {

log.WithFields(log.Fields{
Expand Down
7 changes: 4 additions & 3 deletions core/handlers/v2/simulation_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,9 @@ func (this *HoverflySimulationStub) DeleteSimulation() {
this.Deleted = true
}

func (this *HoverflySimulationStub) PutSimulation(simulation SimulationViewV6) SimulationImportResult {
func (this *HoverflySimulationStub) PutSimulation(simulation SimulationViewV6, overrideExisting bool) SimulationImportResult {
this.Simulation = simulation
this.Deleted = overrideExisting
return SimulationImportResult{}
}

Expand All @@ -83,7 +84,7 @@ func (this HoverflySimulationErrorStub) GetFilteredSimulation(urlPattern string)

func (this *HoverflySimulationErrorStub) DeleteSimulation() {}

func (this *HoverflySimulationErrorStub) PutSimulation(simulation SimulationViewV6) SimulationImportResult {
func (this *HoverflySimulationErrorStub) PutSimulation(simulation SimulationViewV6, overrideExisting bool) SimulationImportResult {
return SimulationImportResult{
err: fmt.Errorf("error"),
}
Expand All @@ -101,7 +102,7 @@ func (this HoverflySimulationWarningStub) GetFilteredSimulation(urlPattern strin

func (this *HoverflySimulationWarningStub) DeleteSimulation() {}

func (this *HoverflySimulationWarningStub) PutSimulation(simulation SimulationViewV6) SimulationImportResult {
func (this *HoverflySimulationWarningStub) PutSimulation(simulation SimulationViewV6, overrideExisting bool) SimulationImportResult {
return SimulationImportResult{
WarningMessages: []SimulationImportWarning{{"This is a warning", "url"}},
}
Expand Down
26 changes: 26 additions & 0 deletions core/hoverfly_funcs.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package hoverfly

import (
v2 "github.com/SpectoLabs/hoverfly/core/handlers/v2"
"github.com/aymerick/raymond"
"io/ioutil"
"net/http"
"path/filepath"
"strings"

"github.com/SpectoLabs/hoverfly/core/errors"
Expand Down Expand Up @@ -114,6 +117,29 @@ func (hf *Hoverfly) GetResponse(requestDetails models.RequestDetails) (*models.R
return &response, nil
}

func (hf *Hoverfly) readResponseBodyFiles(pairs []v2.RequestMatcherResponsePairViewV6) v2.SimulationImportResult {
result := v2.SimulationImportResult{}

for i, pair := range pairs {
if len(pair.Response.GetBody()) > 0 && len(pair.Response.GetBodyFile()) > 0 {
result.AddBodyAndBodyFileWarning(i)
continue
}

if len(pair.Response.GetBody()) == 0 && len(pair.Response.GetBodyFile()) > 0 {
fileContents, err := ioutil.ReadFile(filepath.Join(hf.Cfg.ResponsesBodyFilesPath, pair.Response.GetBodyFile()))
if err != nil {
result.SetError(err)
return result
}

pairs[i].Response.Body = string(fileContents[:])
}
}

return result
}

func (hf *Hoverfly) applyBodyTemplating(requestDetails *models.RequestDetails, response *models.ResponseDetails, cachedResponse *models.CachedResponse) (string, error) {
var template *raymond.Template
if cachedResponse != nil && cachedResponse.ResponseTemplate != nil {
Expand Down
4 changes: 2 additions & 2 deletions core/hoverfly_funcs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ func Test_Hoverfly_GetResponse_WillCacheClosestMiss(t *testing.T) {
v2.MetaView{
SchemaVersion: "v3",
},
})
}, false)

requestDetails := models.RequestDetails{
Destination: "somehost.com",
Expand Down Expand Up @@ -596,7 +596,7 @@ func Test_Hoverfly_GetResponse_TransitioningBetweenStatesWhenSimulating(t *testi
hoverfly.CacheMatcher = matching.CacheMatcher{
RequestCache: cache.NewDefaultLRUCache(),
}
hoverfly.PutSimulation(*v6)
hoverfly.PutSimulation(*v6, false)

hoverfly.SetModeWithArguments(v2.ModeView{Mode: "simulate"})

Expand Down
15 changes: 14 additions & 1 deletion core/hoverfly_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,16 @@ func (hf Hoverfly) GetFilteredSimulation(urlPattern string) (v2.SimulationViewV6
hf.version), nil
}

func (this *Hoverfly) PutSimulation(simulationView v2.SimulationViewV6) v2.SimulationImportResult {
func (this *Hoverfly) PutSimulation(simulationView v2.SimulationViewV6, overrideExisting bool) v2.SimulationImportResult {
ns3777k marked this conversation as resolved.
Show resolved Hide resolved
bodyFilesResult := this.readResponseBodyFiles(simulationView.RequestResponsePairs)
if bodyFilesResult.GetError() != nil {
return bodyFilesResult
}

if overrideExisting {
this.DeleteSimulation()
}

result := this.importRequestResponsePairViews(simulationView.DataViewV6.RequestResponsePairs)
if result.GetError() != nil {
return result
Expand All @@ -299,6 +308,10 @@ func (this *Hoverfly) PutSimulation(simulationView v2.SimulationViewV6) v2.Simul
return result
}

for _, warning := range bodyFilesResult.WarningMessages {
result.WarningMessages = append(result.WarningMessages, warning)
}

return result
}

Expand Down
10 changes: 5 additions & 5 deletions core/hoverfly_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,7 @@ func Test_Hoverfly_PutSimulation_ImportsRecordings(t *testing.T) {
v2.MetaView{},
}

unit.PutSimulation(simulationToImport)
unit.PutSimulation(simulationToImport, false)

importedSimulation, err := unit.GetSimulation()
Expect(err).To(BeNil())
Expand Down Expand Up @@ -563,7 +563,7 @@ func Test_Hoverfly_PutSimulation_ImportsSimulationViews(t *testing.T) {
v2.MetaView{},
}

unit.PutSimulation(simulationToImport)
unit.PutSimulation(simulationToImport, false)

importedSimulation, err := unit.GetSimulation()
Expect(err).To(BeNil())
Expand Down Expand Up @@ -595,7 +595,7 @@ func Test_Hoverfly_PutSimulation_ImportsDelays(t *testing.T) {
v2.MetaView{},
}

err := unit.PutSimulation(simulationToImport)
err := unit.PutSimulation(simulationToImport, false)
Expect(err.GetError()).To(BeNil())

delays := unit.Simulation.ResponseDelays.ConvertToResponseDelayPayloadView()
Expand Down Expand Up @@ -626,7 +626,7 @@ func Test_Hoverfly_PutSimulation_ImportsDelaysWithValidationError(t *testing.T)
v2.MetaView{},
}

err := unit.PutSimulation(simulationToImport)
err := unit.PutSimulation(simulationToImport, false)
Expect(err.GetError()).NotTo(BeNil())

delays := unit.Simulation.ResponseDelays.ConvertToResponseDelayPayloadView()
Expand All @@ -648,7 +648,7 @@ func Test_Hoverfly_PutSimulation_ImportsDelaysLogNormal(t *testing.T) {
v2.MetaView{},
}

err := unit.PutSimulation(simulationToImport)
err := unit.PutSimulation(simulationToImport, false)
Expect(err.GetError()).To(BeNil())

delays := unit.Simulation.ResponseDelaysLogNormal.ConvertToResponseDelayLogNormalPayloadView()
Expand Down
15 changes: 3 additions & 12 deletions core/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func (hf *Hoverfly) ImportFromDisk(path string) error {
return fmt.Errorf("Got error while parsing payloads, error %s", err.Error())
}

return hf.PutSimulation(simulation).GetError()
return hf.PutSimulation(simulation, false).GetError()
}

// ImportFromURL - takes one string value and tries connect to a remote server, then parse response body into
Expand All @@ -129,7 +129,7 @@ func (hf *Hoverfly) ImportFromURL(url string) error {
return fmt.Errorf("Got error while parsing payloads, error %s", err.Error())
}

return hf.PutSimulation(simulation).GetError()
return hf.PutSimulation(simulation, false).GetError()
}

// importRequestResponsePairViews - a function to save given pairs into the database.
Expand All @@ -140,12 +140,7 @@ func (hf *Hoverfly) importRequestResponsePairViews(pairViews []v2.RequestMatcher
success := 0
failed := 0
for i, pairView := range pairViews {

pair, err := models.NewRequestMatcherResponsePairFromView(&pairView)
if err != nil {
importResult.SetError(err)
return importResult
}
pair := models.NewRequestMatcherResponsePairFromView(&pairView)

var isPairAdded bool
if hf.Cfg.NoImportCheck {
Expand All @@ -172,10 +167,6 @@ func (hf *Hoverfly) importRequestResponsePairViews(pairViews []v2.RequestMatcher
importResult.AddContentLengthAndTransferEncodingWarning(i)
}

if len(pairView.Response.BodyFile) > 0 && len(pairView.Response.Body) > 0 {
importResult.AddBodyAndBodyFileWarning(i)
}

if len(pairView.Response.Headers["Content-Length"]) > 0 {
contentLength, err := strconv.Atoi(pairView.Response.Headers["Content-Length"][0])
if err == nil && contentLength != len(pair.Response.Body) {
Expand Down
2 changes: 1 addition & 1 deletion core/middleware/local_middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func (this Middleware) executeMiddlewareLocally(pair models.RequestResponsePair)
}).Debug("payload after modifications")
}
// payload unmarshalled into RequestResponsePair struct, returning it
return models.NewRequestResponsePairFromRequestResponsePairView(newPairView)
return models.NewRequestResponsePairFromRequestResponsePairView(newPairView), nil
}
} else {
log.WithFields(log.Fields{
Expand Down
2 changes: 1 addition & 1 deletion core/middleware/remote_middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,5 +91,5 @@ func (this Middleware) executeMiddlewareRemotely(pair models.RequestResponsePair
Stdout: string(returnedPairViewBytes),
}
}
return models.NewRequestResponsePairFromRequestResponsePairView(newPairView)
return models.NewRequestResponsePairFromRequestResponsePairView(newPairView), nil
}
27 changes: 7 additions & 20 deletions core/models/payload.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,13 @@ func (this *RequestResponsePair) ConvertToRequestResponsePairView() v2.RequestRe
return v2.RequestResponsePairViewV1{Response: this.Response.ConvertToResponseDetailsView(), Request: this.Request.ConvertToRequestDetailsView()}
}

func NewRequestResponsePairFromRequestResponsePairView(pairView interfaces.RequestResponsePair) (RequestResponsePair, error) {
response, err := NewResponseDetailsFromResponse(pairView.GetResponse())
if err != nil {
return RequestResponsePair{}, nil
}
func NewRequestResponsePairFromRequestResponsePairView(pairView interfaces.RequestResponsePair) RequestResponsePair {
response := NewResponseDetailsFromResponse(pairView.GetResponse())
ns3777k marked this conversation as resolved.
Show resolved Hide resolved

return RequestResponsePair{
Response: response,
Request: NewRequestDetailsFromRequest(pairView.GetRequest()),
}, nil
}
}

func NewRequestDetailsFromRequest(data interfaces.Request) RequestDetails {
Expand Down Expand Up @@ -201,21 +198,11 @@ type ResponseDetails struct {
RemovesState []string
}

func NewResponseDetailsFromResponse(data interfaces.Response) (ResponseDetails, error) {
var body string

if len(data.GetBody()) > 0 {
body = data.GetBody()
} else if len(data.GetBodyFile()) > 0 {
fileContents, err := ioutil.ReadFile(data.GetBodyFile())
if err != nil {
return ResponseDetails{}, err
}
body = string(fileContents[:])
}
func NewResponseDetailsFromResponse(data interfaces.Response) ResponseDetails {
body := data.GetBody()

if data.GetEncodedBody() == true {
decoded, _ := base64.StdEncoding.DecodeString(body)
decoded, _ := base64.StdEncoding.DecodeString(data.GetBody())
body = string(decoded)
}

Expand All @@ -227,7 +214,7 @@ func NewResponseDetailsFromResponse(data interfaces.Response) (ResponseDetails,
Templated: data.GetTemplated(),
TransitionsState: data.GetTransitionsState(),
RemovesState: data.GetRemovesState(),
}, nil
}
}

// This function will create a JSON appropriate version of ResponseDetails for the v2 API
Expand Down
6 changes: 2 additions & 4 deletions core/models/payload_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,9 +293,8 @@ func TestRequestResponsePairView_ConvertToRequestResponsePairWithoutEncoding(t *
},
}

requestResponsePair, err := models.NewRequestResponsePairFromRequestResponsePairView(view)
requestResponsePair := models.NewRequestResponsePairFromRequestResponsePairView(view)

Expect(err).To(BeNil())
Expect(requestResponsePair).To(Equal(models.RequestResponsePair{
Request: models.RequestDetails{
Path: "A",
Expand Down Expand Up @@ -335,9 +334,8 @@ func TestRequestResponsePairView_ConvertToRequestResponsePairWithEncoding(t *tes
},
}

pair, err := models.NewRequestResponsePairFromRequestResponsePairView(view)
pair := models.NewRequestResponsePairFromRequestResponsePairView(view)

Expect(err).To(BeNil())
Expect(pair.Response.Body).To(Equal("encoded"))
}

Expand Down
Loading