コード例 #1
0
func (this HoverflySimulationStub) GetSimulation() (SimulationView, error) {
	pairOne := RequestResponsePairView{
		Request: RequestDetailsView{
			Destination: util.StringToPointer("test.com"),
			Path:        util.StringToPointer("/testing"),
		},
		Response: ResponseDetailsView{
			Body: "test-body",
		},
	}

	return SimulationView{
		DataView{
			RequestResponsePairs: []RequestResponsePairView{pairOne},
			GlobalActions: GlobalActionsView{
				Delays: []v1.ResponseDelayView{
					{
						HttpMethod: "GET",
						Delay:      100,
					},
				},
			},
		},
		MetaView{
			SchemaVersion:   "v1",
			HoverflyVersion: "test",
			TimeExported:    "now",
		},
	}, nil
}
コード例 #2
0
func TestHoverfly_PutSimulation_ImportsTemplates(t *testing.T) {
	RegisterTestingT(t)

	server, unit := testTools(201, `{'message': 'here'}`)
	defer server.Close()

	simulationToImport := v2.SimulationView{
		DataView: v2.DataView{
			RequestResponsePairs: []v2.RequestResponsePairView{pairOneTemplate},
			GlobalActions: v2.GlobalActionsView{
				Delays: []v1.ResponseDelayView{},
			},
		},
		MetaView: v2.MetaView{},
	}

	unit.PutSimulation(simulationToImport)

	importedSimulation, err := unit.GetSimulation()
	Expect(err).To(BeNil())

	Expect(importedSimulation).ToNot(BeNil())

	Expect(importedSimulation.RequestResponsePairs).ToNot(BeNil())
	Expect(importedSimulation.RequestResponsePairs).To(HaveLen(1))

	Expect(importedSimulation.RequestResponsePairs[0].Request.RequestType).To(Equal(util.StringToPointer("template")))
	Expect(importedSimulation.RequestResponsePairs[0].Request.Destination).To(BeNil())
	Expect(importedSimulation.RequestResponsePairs[0].Request.Path).To(Equal(util.StringToPointer("/template")))

	Expect(importedSimulation.RequestResponsePairs[0].Response.Body).To(Equal("template-body"))
}
コード例 #3
0
func TestSimulationHandler_Get_ReturnsSimulation(t *testing.T) {
	RegisterTestingT(t)

	stubHoverfly := &HoverflySimulationStub{}
	unit := SimulationHandler{Hoverfly: stubHoverfly}

	request, err := http.NewRequest("GET", "", nil)
	Expect(err).To(BeNil())

	response := makeRequestOnHandler(unit.Get, request)

	Expect(response.Code).To(Equal(http.StatusOK))

	simulationView, err := unmarshalSimulationView(response.Body)
	Expect(err).To(BeNil())

	Expect(simulationView.DataView.RequestResponsePairs).To(HaveLen(1))

	Expect(simulationView.DataView.RequestResponsePairs[0].Request.Destination).To(Equal(util.StringToPointer("test.com")))
	Expect(simulationView.DataView.RequestResponsePairs[0].Request.Path).To(Equal(util.StringToPointer("/testing")))

	Expect(simulationView.DataView.RequestResponsePairs[0].Response.Body).To(Equal("test-body"))

	Expect(simulationView.DataView.GlobalActions.Delays).To(HaveLen(1))
	Expect(simulationView.DataView.GlobalActions.Delays[0].HttpMethod).To(Equal("GET"))
	Expect(simulationView.DataView.GlobalActions.Delays[0].Delay).To(Equal(100))

	Expect(simulationView.MetaView.SchemaVersion).To(Equal("v1"))
	Expect(simulationView.MetaView.HoverflyVersion).To(Equal("test"))
	Expect(simulationView.MetaView.TimeExported).To(Equal("now"))
}
コード例 #4
0
func TestHoverfly_GetSimulation_ReturnsASingleRequestResponsePairTemplate(t *testing.T) {
	RegisterTestingT(t)

	server, unit := testTools(201, `{'message': 'here'}`)
	defer server.Close()

	unit.RequestMatcher.TemplateStore = append(unit.RequestMatcher.TemplateStore, matching.RequestTemplateResponsePair{
		RequestTemplate: matching.RequestTemplate{
			Destination: util.StringToPointer("test.com"),
		},
		Response: models.ResponseDetails{
			Status: 200,
			Body:   "test-template",
		},
	})

	simulation, err := unit.GetSimulation()
	Expect(err).To(BeNil())

	Expect(simulation.DataView.RequestResponsePairs).To(HaveLen(1))

	Expect(*simulation.DataView.RequestResponsePairs[0].Request.RequestType).To(Equal("template"))
	Expect(*simulation.DataView.RequestResponsePairs[0].Request.Destination).To(Equal("test.com"))
	Expect(simulation.DataView.RequestResponsePairs[0].Request.Path).To(BeNil())
	Expect(simulation.DataView.RequestResponsePairs[0].Request.Method).To(BeNil())
	Expect(simulation.DataView.RequestResponsePairs[0].Request.Query).To(BeNil())
	Expect(simulation.DataView.RequestResponsePairs[0].Request.Scheme).To(BeNil())
	Expect(simulation.DataView.RequestResponsePairs[0].Request.Headers).To(HaveLen(0))

	Expect(simulation.DataView.RequestResponsePairs[0].Response.Status).To(Equal(200))
	Expect(simulation.DataView.RequestResponsePairs[0].Response.EncodedBody).To(BeFalse())
	Expect(simulation.DataView.RequestResponsePairs[0].Response.Body).To(Equal("test-template"))
	Expect(simulation.DataView.RequestResponsePairs[0].Response.Headers).To(HaveLen(0))

	Expect(nil).To(BeNil())
}
コード例 #5
0
import (
	"testing"

	"github.com/SpectoLabs/hoverfly/core/handlers/v1"
	"github.com/SpectoLabs/hoverfly/core/handlers/v2"
	"github.com/SpectoLabs/hoverfly/core/matching"
	"github.com/SpectoLabs/hoverfly/core/models"
	"github.com/SpectoLabs/hoverfly/core/util"
	. "github.com/onsi/gomega"
)

var (
	pairOneRecording = v2.RequestResponsePairView{
		Request: v2.RequestDetailsView{
			RequestType: util.StringToPointer("recording"),
			Destination: util.StringToPointer("test.com"),
			Path:        util.StringToPointer("/testing"),
		},
		Response: v2.ResponseDetailsView{
			Body: "test-body",
		},
	}

	pairOneTemplate = v2.RequestResponsePairView{
		Request: v2.RequestDetailsView{
			RequestType: util.StringToPointer("template"),
			Path:        util.StringToPointer("/template"),
		},
		Response: v2.ResponseDetailsView{
			Body: "template-body",
コード例 #6
0
func TestSimulationHandler_Put_PassesDataIntoHoverfly(t *testing.T) {
	RegisterTestingT(t)

	stubHoverfly := &HoverflySimulationStub{}

	unit := SimulationHandler{Hoverfly: stubHoverfly}

	request, err := http.NewRequest("PUT", "", ioutil.NopCloser(bytes.NewBuffer([]byte(`
	{
		"data": {
			"pairs": [
				{
					"request": {
						"destination": "test.org"
					},
					"response": {
						"status": 200
					}
				}
			],

			"globalActions": {
				"delays": [
					{
						"urlPattern": "test.org",
						"httpMethod": "GET",
						"delay": 200
					}
				]
			}
		},
		"meta": {
			"schemaVersion": "v1"
		}
	}
	`))))
	Expect(err).To(BeNil())

	makeRequestOnHandler(unit.Put, request)

	Expect(stubHoverfly.Simulation).ToNot(BeNil())
	Expect(stubHoverfly.Simulation.RequestResponsePairs).ToNot(BeNil())

	Expect(stubHoverfly.Simulation.RequestResponsePairs[0].Request.Destination).To(Equal(util.StringToPointer("test.org")))
	Expect(stubHoverfly.Simulation.RequestResponsePairs[0].Response.Status).To(Equal(200))

	Expect(stubHoverfly.Simulation.GlobalActions.Delays[0].UrlPattern).To(Equal("test.org"))
	Expect(stubHoverfly.Simulation.GlobalActions.Delays[0].HttpMethod).To(Equal("GET"))
	Expect(stubHoverfly.Simulation.GlobalActions.Delays[0].Delay).To(Equal(200))
}
コード例 #7
0
ファイル: add_handler.go プロジェクト: SpectoLabs/hoverfly
// ManualAddHandler - manually add new request/responses, using a form
func (this *AddHandler) Post(w http.ResponseWriter, req *http.Request, next http.HandlerFunc) {
	err := req.ParseForm()

	if err != nil {
		log.WithFields(log.Fields{
			"error": err.Error(),
		}).Error("Got error while parsing form")
	}

	// request details
	destination := req.PostFormValue("inputDestination")
	method := req.PostFormValue("inputMethod")
	path := req.PostFormValue("inputPath")
	query := req.PostFormValue("inputQuery")
	reqBody := req.PostFormValue("inputRequestBody")

	preq := RequestDetailsView{
		Destination: util.StringToPointer(destination),
		Method:      util.StringToPointer(method),
		Path:        util.StringToPointer(path),
		Query:       util.StringToPointer(query),
		Body:        util.StringToPointer(reqBody),
	}

	// response
	respStatusCode := req.PostFormValue("inputResponseStatusCode")
	respBody := req.PostFormValue("inputResponseBody")
	contentType := req.PostFormValue("inputContentType")

	headers := make(map[string][]string)

	// getting content type
	if contentType == "xml" {
		headers["Content-Type"] = []string{"application/xml"}
	} else if contentType == "json" {
		headers["Content-Type"] = []string{"application/json"}
	} else {
		headers["Content-Type"] = []string{"text/html"}
	}

	sc, _ := strconv.Atoi(respStatusCode)

	presp := ResponseDetailsView{
		Status:  sc,
		Headers: headers,
		Body:    respBody,
	}

	log.WithFields(log.Fields{
		"respBody":    respBody,
		"contentType": contentType,
	}).Info("manually adding request/response")

	p := RequestResponsePairView{Request: preq, Response: presp}

	pairViews := make([]interfaces.RequestResponsePair, 0)

	pairViews = append(pairViews, p)

	err = this.Hoverfly.ImportRequestResponsePairViews(pairViews)

	w.Header().Set("Content-Type", "application/json")
	var response MessageResponse

	if err != nil {
		response.Message = fmt.Sprintf("Got error: %s", err.Error())
		w.WriteHeader(400)

	} else {
		// redirecting to home
		response.Message = "Record added successfuly"
		w.WriteHeader(201)
	}
	b, err := response.Encode()
	if err != nil {
		// failed to read response body
		log.WithFields(log.Fields{
			"error": err.Error(),
		}).Error("Could not encode response body!")
		http.Error(w, "Failed to encode response", 500)
		return
	}
	w.Write(b)

}