Exemplo n.º 1
0
func ConvertToRequestTemplateResponsePair(pairView v1.RequestTemplateResponsePairView) RequestTemplateResponsePair {
	return RequestTemplateResponsePair{
		RequestTemplate: RequestTemplate{
			Path:        pairView.RequestTemplate.Path,
			Method:      pairView.RequestTemplate.Method,
			Destination: pairView.RequestTemplate.Destination,
			Scheme:      pairView.RequestTemplate.Scheme,
			Query:       pairView.RequestTemplate.Query,
			Body:        pairView.RequestTemplate.Body,
			Headers:     pairView.RequestTemplate.Headers,
		},
		Response: models.NewResponseDetailsFromResponse(pairView.Response),
	}
}
Exemplo n.º 2
0
func TestImportImportRequestResponsePairs_CanImportARequestTemplateResponsePair(t *testing.T) {
	RegisterTestingT(t)

	cache := cache.NewInMemoryCache()
	cfg := Configuration{Webserver: false}
	requestMatcher := matching.RequestMatcher{RequestCache: cache, Webserver: &cfg.Webserver}
	hv := Hoverfly{RequestCache: cache, Cfg: &cfg, RequestMatcher: requestMatcher}

	RegisterTestingT(t)

	requestTemplate := v1.RequestDetailsView{
		RequestType: StringToPointer("template"),
		Method:      StringToPointer("GET"),
	}

	responseView := v1.ResponseDetailsView{
		Status:      200,
		Body:        "hello_world",
		EncodedBody: false,
		Headers:     map[string][]string{"Hoverfly": []string{"testing"}},
	}

	templatePair := v1.RequestResponsePairView{
		Response: responseView,
		Request:  requestTemplate,
	}

	hv.ImportRequestResponsePairViews([]interfaces.RequestResponsePair{templatePair})

	Expect(len(hv.RequestMatcher.TemplateStore)).To(Equal(1))

	request := models.NewRequestDetailsFromRequest(requestTemplate)
	responseFromCache, err := hv.RequestMatcher.TemplateStore.GetResponse(request, false)
	Expect(err).To(BeNil())

	response := models.NewResponseDetailsFromResponse(responseView)

	Expect(*responseFromCache).To(Equal(response))
}
Exemplo n.º 3
0
func TestImportImportRequestResponsePairs_CanImportARequestResponsePair_AndRequestTemplateResponsePair(t *testing.T) {
	RegisterTestingT(t)

	cache := cache.NewInMemoryCache()
	cfg := Configuration{Webserver: false}
	requestMatcher := matching.RequestMatcher{RequestCache: cache, Webserver: &cfg.Webserver}
	hv := Hoverfly{RequestCache: cache, Cfg: &cfg, RequestMatcher: requestMatcher}

	RegisterTestingT(t)

	requestTemplate := v1.RequestDetailsView{
		RequestType: StringToPointer("template"),
		Method:      StringToPointer("GET"),
	}

	requestView := v1.RequestDetailsView{
		Method:      StringToPointer("GET"),
		Path:        StringToPointer("/"),
		Destination: StringToPointer("test.com"),
		Scheme:      StringToPointer("http"),
	}

	responseView := v1.ResponseDetailsView{
		Status:      200,
		Body:        "hello_world",
		EncodedBody: false,
		Headers:     map[string][]string{"Hoverfly": []string{"testing"}},
	}

	templatePair := v1.RequestResponsePairView{
		Request:  requestTemplate,
		Response: responseView,
	}

	ordinaryPair := v1.RequestResponsePairView{
		Request:  requestView,
		Response: responseView,
	}

	hv.ImportRequestResponsePairViews([]interfaces.RequestResponsePair{templatePair, ordinaryPair})

	cacheCount, err := hv.RequestCache.RecordsCount()
	Expect(cacheCount).To(Equal(1))
	Expect(err).To(BeNil())

	Expect(len(hv.RequestMatcher.TemplateStore)).To(Equal(1))

	request := models.NewRequestDetailsFromRequest(requestTemplate)
	response := models.NewResponseDetailsFromResponse(responseView)

	pairBytes, err := hv.RequestCache.Get([]byte("76cf08e38439f083de2658b0971df9bf"))
	Expect(err).To(BeNil())

	savedPair, err := models.NewRequestResponsePairFromBytes(pairBytes)
	Expect(err).To(BeNil())

	Expect(savedPair.Response).To(Equal(response))

	responseFromCache, err := hv.RequestMatcher.TemplateStore.GetResponse(request, false)
	Expect(err).To(BeNil())
	Expect(*responseFromCache).To(Equal(response))

}
Exemplo n.º 4
0
// ImportRequestResponsePairViews - a function to save given pairs into the database.
func (hf *Hoverfly) ImportRequestResponsePairViews(pairViews []interfaces.RequestResponsePair) error {
	if len(pairViews) > 0 {
		success := 0
		failed := 0
		for _, pairView := range pairViews {

			if pairView.GetRequest().GetRequestType() != nil && *pairView.GetRequest().GetRequestType() == *StringToPointer("template") {
				responseDetails := models.NewResponseDetailsFromResponse(pairView.GetResponse())

				requestTemplate := matching.RequestTemplate{
					Path:        pairView.GetRequest().GetPath(),
					Method:      pairView.GetRequest().GetMethod(),
					Destination: pairView.GetRequest().GetDestination(),
					Scheme:      pairView.GetRequest().GetScheme(),
					Query:       pairView.GetRequest().GetQuery(),
					Body:        pairView.GetRequest().GetBody(),
					Headers:     pairView.GetRequest().GetHeaders(),
				}

				requestTemplateResponsePair := matching.RequestTemplateResponsePair{
					RequestTemplate: requestTemplate,
					Response:        responseDetails,
				}

				hf.RequestMatcher.TemplateStore = append(hf.RequestMatcher.TemplateStore, requestTemplateResponsePair)
				success++
				continue
			}

			// Convert PayloadView back to Payload for internal storage
			pair := models.NewRequestResponsePairFromRequestResponsePairView(pairView)

			if len(pair.Request.Headers) == 0 {
				pair.Request.Headers = make(map[string][]string)
			}

			if _, present := pair.Request.Headers["Content-Type"]; !present {
				// sniffing content types
				if isJSON(pair.Request.Body) {
					pair.Request.Headers["Content-Type"] = []string{"application/json"}
				} else {
					ct := http.DetectContentType([]byte(pair.Request.Body))
					pair.Request.Headers["Content-Type"] = []string{ct}
				}
			}

			pairBytes, err := pair.Encode()
			if err != nil {
				log.WithFields(log.Fields{
					"error": err.Error(),
				}).Error("Failed to encode payload")
				failed++
			} else {
				// hook
				var en Entry
				en.ActionType = ActionTypeRequestCaptured
				en.Message = "imported"
				en.Time = time.Now()
				en.Data = pairBytes

				if err := hf.Hooks.Fire(ActionTypeRequestCaptured, &en); err != nil {
					log.WithFields(log.Fields{
						"error":      err.Error(),
						"message":    en.Message,
						"actionType": ActionTypeRequestCaptured,
					}).Error("failed to fire hook")
				}

				err := hf.RequestMatcher.SaveRequestResponsePair(&pair)
				if err != nil {
					log.WithFields(log.Fields{
						"error": err.Error(),
					}).Error("Failed to save payload")
				}

				if err == nil {
					success++
				} else {
					failed++
				}
			}
		}
		log.WithFields(log.Fields{
			"total":      len(pairViews),
			"successful": success,
			"failed":     failed,
		}).Info("payloads imported")
		return nil
	}
	return nil
}