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 9 commits
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
14 changes: 7 additions & 7 deletions core/benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func BenchmarkProcessRequest(b *testing.B) {
Mode: "simulate",
})

simulation := v2.SimulationViewV5{}
simulation := v2.SimulationViewV6{}
_ = json.Unmarshal([]byte(`{
"data": {
"pairs": [{
Expand Down Expand Up @@ -99,13 +99,13 @@ func BenchmarkProcessRequest(b *testing.B) {
}
},
"meta": {
"schemaVersion": "v5",
"schemaVersion": "v6",
"hoverflyVersion": "v0.17.0",
"timeExported": "2017-05-30T14:23:44+01:00"
}
}`), &simulation)

templated := v2.SimulationViewV5{}
templated := v2.SimulationViewV6{}
_ = json.Unmarshal([]byte(`{
"data": {
"pairs": [{
Expand Down Expand Up @@ -150,19 +150,19 @@ func BenchmarkProcessRequest(b *testing.B) {
}
},
"meta": {
"schemaVersion": "v5",
"schemaVersion": "v6",
"hoverflyVersion": "v0.17.0",
"timeExported": "2017-05-30T14:23:44+01:00"
}
}`), &templated)

bytes, _ := ioutil.ReadFile("../testdata/large_response_body.json")
largeResponse := v2.SimulationViewV5{}
largeResponse := v2.SimulationViewV6{}
_ = json.Unmarshal(bytes, &largeResponse)

benchmarks := []struct {
name string
simulation v2.SimulationViewV5
simulation v2.SimulationViewV6
}{
{"Simple simulation", simulation},
{"Templated simulation", templated},
Expand All @@ -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
20 changes: 10 additions & 10 deletions core/handlers/v2/cache_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,23 @@ func (this HoverflyCacheStub) GetCache() (CacheView, error) {
return CacheView{
Cache: []CachedResponseView{
{
MatchingPair: &RequestMatcherResponsePairViewV5{
RequestMatcher: RequestMatcherViewV5{
Destination: []MatcherViewV5{
NewMatcherView(matchers.Exact, "one"),
MatchingPair: &RequestMatcherResponsePairViewV6{
RequestMatcher: RequestMatcherViewV6{
Destination: []MatcherViewV6{
NewMatcherViewV6(matchers.Exact, "one"),
},
},
Response: ResponseDetailsViewV5{},
Response: ResponseDetailsViewV6{},
},
},
{
MatchingPair: &RequestMatcherResponsePairViewV5{
RequestMatcher: RequestMatcherViewV5{
Destination: []MatcherViewV5{
NewMatcherView(matchers.Exact, "two"),
MatchingPair: &RequestMatcherResponsePairViewV6{
RequestMatcher: RequestMatcherViewV6{
Destination: []MatcherViewV6{
NewMatcherViewV6(matchers.Exact, "two"),
},
},
Response: ResponseDetailsViewV5{},
Response: ResponseDetailsViewV6{},
},
},
},
Expand Down
20 changes: 10 additions & 10 deletions core/handlers/v2/journal_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,12 +216,12 @@ func Test_JournalHandler_Post_CallsFilter(t *testing.T) {
unit := JournalHandler{Hoverfly: &stubHoverfly}

journalEntryFilterView := JournalEntryFilterView{
Request: &RequestMatcherViewV5{
Destination: []MatcherViewV5{
NewMatcherView(matchers.Exact, "hoverfly.io"),
Request: &RequestMatcherViewV6{
Destination: []MatcherViewV6{
NewMatcherViewV6(matchers.Exact, "hoverfly.io"),
},
Path: []MatcherViewV5{
NewMatcherView(matchers.Glob, "*"),
Path: []MatcherViewV6{
NewMatcherViewV6(matchers.Glob, "*"),
},
},
}
Expand Down Expand Up @@ -295,12 +295,12 @@ func Test_JournalHandler_Post_JournalError(t *testing.T) {
unit := JournalHandler{Hoverfly: &stubHoverfly}

requestMatcher := JournalEntryFilterView{
Request: &RequestMatcherViewV5{
Destination: []MatcherViewV5{
NewMatcherView(matchers.Exact, "hoverfly.io"),
Request: &RequestMatcherViewV6{
Destination: []MatcherViewV6{
NewMatcherViewV6(matchers.Exact, "hoverfly.io"),
},
Path: []MatcherViewV5{
NewMatcherView(matchers.Glob, "*"),
Path: []MatcherViewV6{
NewMatcherViewV6(matchers.Glob, "*"),
},
},
}
Expand Down
19 changes: 7 additions & 12 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 @@ -16,9 +15,9 @@ import (
)

type HoverflySimulation interface {
GetSimulation() (SimulationViewV5, error)
GetFilteredSimulation(string) (SimulationViewV5, error)
PutSimulation(SimulationViewV5) SimulationImportResult
GetSimulation() (SimulationViewV6, error)
GetFilteredSimulation(string) (SimulationViewV6, error)
PutSimulation(SimulationViewV6, bool) SimulationImportResult
DeleteSimulation()
}

Expand Down Expand Up @@ -60,7 +59,7 @@ func (this *SimulationHandler) Get(w http.ResponseWriter, req *http.Request, nex
urlPattern := req.URL.Query().Get("urlPattern")

var err error
var simulationView SimulationViewV5
var simulationView SimulationViewV6
if urlPattern == "" {
simulationView, err = this.Hoverfly.GetSimulation()
} else {
Expand Down Expand Up @@ -106,7 +105,7 @@ func (this *SimulationHandler) Options(w http.ResponseWriter, r *http.Request, n
}

func (this *SimulationHandler) GetSchema(w http.ResponseWriter, req *http.Request, next http.HandlerFunc) {
bytes, _ := json.Marshal(SimulationViewV5Schema)
bytes, _ := json.Marshal(SimulationViewV6Schema)

handlers.WriteResponse(w, bytes)
}
Expand All @@ -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
Loading