Example #1
0
// getRequestFingerprint returns request hash
func GetRequestFingerprint(req *http.Request, requestBody []byte, webserver bool) string {
	var r models.RequestDetails

	r = models.RequestDetails{
		Path:        req.URL.Path,
		Method:      req.Method,
		Destination: req.Host,
		Query:       req.URL.RawQuery,
		Body:        string(requestBody),
		Headers:     req.Header,
	}

	if webserver {
		return r.HashWithoutHost()
	}

	return r.Hash()
}
Example #2
0
// getResponse returns stored response from cache
func (this *RequestMatcher) GetResponse(req *models.RequestDetails) (*models.ResponseDetails, *MatchingError) {

	var key string

	if *this.Webserver {
		key = req.HashWithoutHost()
	} else {
		key = req.Hash()
	}

	pairBytes, err := this.RequestCache.Get([]byte(key))

	if err != nil {
		log.WithFields(log.Fields{
			"key":         key,
			"error":       err.Error(),
			"query":       req.Query,
			"path":        req.Path,
			"destination": req.Destination,
			"method":      req.Method,
		}).Warn("Failed to retrieve response from cache")

		response, err := this.TemplateStore.GetResponse(*req, *this.Webserver)
		if err != nil {
			log.WithFields(log.Fields{
				"key":         key,
				"error":       err.Error(),
				"query":       req.Query,
				"path":        req.Path,
				"destination": req.Destination,
				"method":      req.Method,
			}).Warn("Failed to find matching request template from template store")

			return nil, &MatchingError{
				StatusCode:  412,
				Description: "Could not find recorded request, please record it first!",
			}
		}
		log.WithFields(log.Fields{
			"key":         key,
			"query":       req.Query,
			"path":        req.Path,
			"destination": req.Destination,
			"method":      req.Method,
		}).Info("Found template matching request from template store")
		return response, nil
	}

	// getting cache response
	pair, err := models.NewRequestResponsePairFromBytes(pairBytes)
	if err != nil {
		log.WithFields(log.Fields{
			"error": err.Error(),
			"value": string(pairBytes),
			"key":   key,
		}).Error("Failed to decode payload")
		return nil, &MatchingError{
			StatusCode:  500,
			Description: "Failed to decode payload",
		}
	}

	log.WithFields(log.Fields{
		"key":         key,
		"path":        req.Path,
		"rawQuery":    req.Query,
		"method":      req.Method,
		"destination": req.Destination,
		"status":      pair.Response.Status,
	}).Info("Payload found from cache")

	return &pair.Response, nil
}