Skip to content

Commit

Permalink
Fixing Request.Host templating data
Browse files Browse the repository at this point in the history
  • Loading branch information
tommysitu committed Oct 18, 2023
1 parent c977ad7 commit 6b496fb
Show file tree
Hide file tree
Showing 10 changed files with 33 additions and 23 deletions.
4 changes: 0 additions & 4 deletions core/handlers/v2/simulation_views_v1.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ type RequestDetailsView struct {
RequestType *string `json:"requestType,omitempty"`
Path *string `json:"path"`
Method *string `json:"method"`
Host *string `json:"host"`
Destination *string `json:"destination"`
Scheme *string `json:"scheme"`
Query *string `json:"query"`
Expand All @@ -91,9 +90,6 @@ func (this RequestDetailsView) GetPath() *string { return this.Path }
// Gets Method - required for interfaces.RequestMatcher
func (this RequestDetailsView) GetMethod() *string { return this.Method }

// Gets Host - required for interfaces.RequestMatcher
func (this RequestDetailsView) GetHost() *string { return this.Host }

// Gets Destination - required for interfaces.RequestMatcher
func (this RequestDetailsView) GetDestination() *string { return this.Destination }

Expand Down
1 change: 0 additions & 1 deletion core/interfaces/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ type RequestResponsePair interface {
type Request interface {
GetPath() *string
GetMethod() *string
GetHost() *string
GetDestination() *string
GetScheme() *string
GetQuery() *string
Expand Down
2 changes: 0 additions & 2 deletions core/matching/strongest_match_strategy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1396,7 +1396,6 @@ func Test_ShouldReturnMessageForClosestMiss(t *testing.T) {
RequestDetails: models.RequestDetails{
Path: "path",
Method: "method",
Host: "host",
Destination: "destination",
Scheme: "scheme",
Query: map[string][]string{
Expand Down Expand Up @@ -1479,7 +1478,6 @@ The following request was made, but was not matched by Hoverfly:
{
"Path": "path",
"Method": "method",
"Host": "host",
"Destination": "destination",
"Scheme": "scheme",
"Query": {
Expand Down
8 changes: 4 additions & 4 deletions core/middleware/remote_middleware_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func Test_Middleware_executeMiddlewareRemotely_ReturnsErrorIfDoesntGetA200_AndSa
Expect(err.Error()).To(ContainSubstring("Error when communicating with remote middleware: received 404"))
Expect(err.Error()).To(ContainSubstring("URL: " + server.URL))
Expect(err.Error()).To(ContainSubstring("STDIN:"))
Expect(err.Error()).To(ContainSubstring(`{"response":{"status":0,"body":"Normal body","encodedBody":false},"request":{"path":"","method":"","host":"","destination":"","scheme":"","query":"","formData":null,"body":"","headers":null}}`))
Expect(err.Error()).To(ContainSubstring(`{"response":{"status":0,"body":"Normal body","encodedBody":false},"request":{"path":"","method":"","destination":"","scheme":"","query":"","formData":null,"body":"","headers":null}}`))

Expect(newPair).To(Equal(originalPair))
}
Expand Down Expand Up @@ -87,7 +87,7 @@ func Test_Middleware_executeMiddlewareRemotely_ReturnsErrorIfNoRequestResponsePa
Expect(err.Error()).To(ContainSubstring("Error when trying to serialize response from remote middleware"))
Expect(err.Error()).To(ContainSubstring("URL: " + server.URL))
Expect(err.Error()).To(ContainSubstring("STDIN:"))
Expect(err.Error()).To(ContainSubstring(`{"response":{"status":0,"body":"Normal body","encodedBody":false},"request":{"path":"","method":"","host":"","destination":"","scheme":"","query":"","formData":null,"body":"","headers":null}}`))
Expect(err.Error()).To(ContainSubstring(`{"response":{"status":0,"body":"Normal body","encodedBody":false},"request":{"path":"","method":"","destination":"","scheme":"","query":"","formData":null,"body":"","headers":null}}`))

Expect(untouchedPair).To(Equal(originalPair))
}
Expand All @@ -111,7 +111,7 @@ func Test_Middleware_executeMiddlewareRemotely_ReturnsError_WebsiteIsUnreachable
Expect(err.Error()).To(ContainSubstring(`Post "[]somemadeupwebsite": unsupported protocol scheme`))
Expect(err.Error()).To(ContainSubstring("URL: []somemadeupwebsite"))
Expect(err.Error()).To(ContainSubstring("STDIN:"))
Expect(err.Error()).To(ContainSubstring(`{"response":{"status":0,"body":"Normal body","encodedBody":false},"request":{"path":"","method":"","host":"","destination":"","scheme":"","query":"","formData":null,"body":"","headers":null}}`))
Expect(err.Error()).To(ContainSubstring(`{"response":{"status":0,"body":"Normal body","encodedBody":false},"request":{"path":"","method":"","destination":"","scheme":"","query":"","formData":null,"body":"","headers":null}}`))

Expect(untouchedPair).To(Equal(originalPair))

Expand All @@ -123,7 +123,7 @@ func Test_Middleware_executeMiddlewareRemotely_ReturnsError_WebsiteIsUnreachable
Expect(err.Error()).To(MatchRegexp(`Post "http://localhost:4321/spectolabs/hoverfly": dial tcp .+:4321: connect: connection refused`))
Expect(err.Error()).To(ContainSubstring("URL: http://localhost:4321/spectolabs/hoverfly"))
Expect(err.Error()).To(ContainSubstring("STDIN:"))
Expect(err.Error()).To(ContainSubstring(`{"response":{"status":0,"body":"Normal body","encodedBody":false},"request":{"path":"","method":"","host":"","destination":"","scheme":"","query":"","formData":null,"body":"","headers":null}}`))
Expect(err.Error()).To(ContainSubstring(`{"response":{"status":0,"body":"Normal body","encodedBody":false},"request":{"path":"","method":"","destination":"","scheme":"","query":"","formData":null,"body":"","headers":null}}`))

Expect(untouchedPair).To(Equal(originalPair))
}
3 changes: 0 additions & 3 deletions core/middleware/views.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ type RequestDetailsView struct {
RequestType *string `json:"requestType"`
Path *string `json:"path"`
Method *string `json:"method"`
Host *string `json:"host"`
Destination *string `json:"destination"`
Scheme *string `json:"scheme"`
Query *string `json:"query"`
Expand All @@ -36,8 +35,6 @@ func (this RequestDetailsView) GetPath() *string { return this.Path }

func (this RequestDetailsView) GetMethod() *string { return this.Method }

func (this RequestDetailsView) GetHost() *string { return this.Host }

func (this RequestDetailsView) GetDestination() *string { return this.Destination }

func (this RequestDetailsView) GetScheme() *string { return this.Scheme }
Expand Down
4 changes: 0 additions & 4 deletions core/models/payload.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ func NewRequestDetailsFromRequest(data interfaces.Request) RequestDetails {
return RequestDetails{
Path: util.PointerToString(data.GetPath()),
Method: util.PointerToString(data.GetMethod()),
Host: util.PointerToString(data.GetHost()),
Destination: util.PointerToString(data.GetDestination()),
Scheme: util.PointerToString(data.GetScheme()),
Query: query,
Expand All @@ -54,7 +53,6 @@ func NewRequestDetailsFromRequest(data interfaces.Request) RequestDetails {
type RequestDetails struct {
Path string
Method string
Host string
Destination string
Scheme string
Query map[string][]string
Expand Down Expand Up @@ -97,7 +95,6 @@ func NewRequestDetailsFromHttpRequest(req *http.Request) (RequestDetails, error)
requestDetails := RequestDetails{
Path: urlPath,
Method: req.Method,
Host: req.Host,
Destination: strings.ToLower(req.Host),
Scheme: scheme,
Query: req.URL.Query(),
Expand Down Expand Up @@ -128,7 +125,6 @@ func (this *RequestDetails) ConvertToRequestDetailsView() v2.RequestDetailsView
return v2.RequestDetailsView{
Path: &this.Path,
Method: &this.Method,
Host: &this.Host,
Destination: &this.Destination,
Scheme: &this.Scheme,
Query: &queryString,
Expand Down
4 changes: 0 additions & 4 deletions core/models/payload_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,6 @@ func TestRequestResponsePair_ConvertToRequestResponsePairView_WithPlainTextRespo
Request: models.RequestDetails{
Path: "/",
Method: "GET",
Host: "www.foo.com",
Destination: "/",
Scheme: "scheme",
Query: map[string][]string{},
Expand All @@ -160,7 +159,6 @@ func TestRequestResponsePair_ConvertToRequestResponsePairView_WithPlainTextRespo
Request: v2.RequestDetailsView{
Path: StringToPointer("/"),
Method: StringToPointer("GET"),
Host: StringToPointer("www.foo.com"),
Destination: StringToPointer("/"),
Scheme: StringToPointer("scheme"),
Query: StringToPointer(""),
Expand All @@ -181,7 +179,6 @@ func TestRequestResponsePair_ConvertToRequestResponsePairView_WithGzippedRespons
Request: models.RequestDetails{
Path: "/",
Method: "GET",
Host: "www.foo.com",
Destination: "/",
Scheme: "scheme",
Query: map[string][]string{},
Expand All @@ -201,7 +198,6 @@ func TestRequestResponsePair_ConvertToRequestResponsePairView_WithGzippedRespons
Request: v2.RequestDetailsView{
Path: StringToPointer("/"),
Method: StringToPointer("GET"),
Host: StringToPointer("www.foo.com"),
Destination: StringToPointer("/"),
Scheme: StringToPointer("scheme"),
Query: StringToPointer(""),
Expand Down
2 changes: 1 addition & 1 deletion core/templating/templating.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func (t *Templator) NewTemplatingData(requestDetails *models.RequestDetails, lit
FormData: requestDetails.FormData,
body: requestDetails.Body,
Method: requestDetails.Method,
Host: requestDetails.Host,
Host: requestDetails.Destination,
},
Literals: literalMap,
Vars: variableMap,
Expand Down
12 changes: 12 additions & 0 deletions functional-tests/core/ft_templated_response_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,18 @@ var _ = Describe("When I run Hoverfly", func() {
Expect(string(body)).To(Equal("param"))
})

It("Request.Host", func() {
hoverfly.ImportSimulation(testdata.TemplatingRequest)

resp := hoverfly.Proxy(sling.New().Get("http://test-server.com/Request.Host"))
Expect(resp.StatusCode).To(Equal(200))

body, err := io.ReadAll(resp.Body)
Expect(err).To(BeNil())

Expect(string(body)).To(Equal("test-server.com"))
})

It("Gloabl literals and variables", func() {
hoverfly.ImportSimulation(testdata.TemplatingRequest)
resp := hoverfly.Proxy(sling.New().Post("http://test-server.com/global").BodyJSON(map[string]string{
Expand Down
16 changes: 16 additions & 0 deletions functional-tests/testdata/templating_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,22 @@ var TemplatingRequest = `{
"templated": true
}
},
{
"request": {
"path": [
{
"matcher": "exact",
"value": "/Request.Host"
}
]
},
"response": {
"status": 200,
"body": "{{ Request.Host }}",
"encodedBody": false,
"templated": true
}
},
{
"request": {
"path": [
Expand Down

0 comments on commit 6b496fb

Please sign in to comment.