Skip to content

Commit

Permalink
inital commit - remaining journal templating function work
Browse files Browse the repository at this point in the history
  • Loading branch information
kapishmalik authored and tommysitu committed Mar 9, 2024
1 parent 3b27e96 commit ec75263
Show file tree
Hide file tree
Showing 16 changed files with 532 additions and 146 deletions.
1 change: 1 addition & 0 deletions core/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ func getAllHandlers(hoverfly *Hoverfly) []handlers.AdminHandler {
&v2.DiffHandler{Hoverfly: hoverfly},
&v2.HoverflyPostServeActionDetailsHandler{Hoverfly: hoverfly},
&v2.HoverflyTemplateDataSourceHandler{Hoverfly: hoverfly},
&v2.HoverflyJournalIndexHandler{Hoverfly: hoverfly},
}

return list
Expand Down
66 changes: 66 additions & 0 deletions core/handlers/v2/hoverfly_journalindex_handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package v2

import (
"encoding/json"
"github.com/SpectoLabs/hoverfly/core/handlers"
"github.com/codegangsta/negroni"
"github.com/go-zoo/bone"
"net/http"
)

type HoverflyJournalIndex interface {
GetAllIndexes() []JournalIndexView
AddJournalIndex(string) error
DeleteJournalIndex(string)
}

type HoverflyJournalIndexHandler struct {
Hoverfly HoverflyJournalIndex
}

func (hoverflyJournalIndexHandler HoverflyJournalIndexHandler) RegisterRoutes(mux *bone.Mux, am *handlers.AuthHandler) {

mux.Get("/api/v2/journal/index", negroni.New(
negroni.HandlerFunc(am.RequireTokenAuthentication),
negroni.HandlerFunc(hoverflyJournalIndexHandler.GetAll),
))

mux.Post("/api/v2/journal/index", negroni.New(
negroni.HandlerFunc(am.RequireTokenAuthentication),
negroni.HandlerFunc(hoverflyJournalIndexHandler.Post),
))

mux.Delete("/api/v2/journal/index/:indexName", negroni.New(
negroni.HandlerFunc(am.RequireTokenAuthentication),
negroni.HandlerFunc(hoverflyJournalIndexHandler.Delete),
))
}

func (hoverflyJournalIndexHandler HoverflyJournalIndexHandler) Post(rw http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
var journalIndexRequestView JournalIndexRequestView
err := handlers.ReadFromRequest(r, &journalIndexRequestView)
if err != nil {
handlers.WriteErrorResponse(rw, err.Error(), 400)
return
}
err = hoverflyJournalIndexHandler.Hoverfly.AddJournalIndex(journalIndexRequestView.Name)
if err != nil {
handlers.WriteErrorResponse(rw, err.Error(), 400)
return
}
hoverflyJournalIndexHandler.GetAll(rw, r, next)
}

func (hoverflyJournalIndexHandler HoverflyJournalIndexHandler) Delete(rw http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
indexName := bone.GetValue(r, "indexName")
hoverflyJournalIndexHandler.Hoverfly.DeleteJournalIndex(indexName)
hoverflyJournalIndexHandler.GetAll(rw, r, next)
}

func (hoverflyJournalIndexHandler HoverflyJournalIndexHandler) GetAll(rw http.ResponseWriter, req *http.Request, next http.HandlerFunc) {

journalIndexViews := hoverflyJournalIndexHandler.Hoverfly.GetAllIndexes()
bytes, _ := json.Marshal(journalIndexViews)

handlers.WriteResponse(rw, bytes)
}
16 changes: 16 additions & 0 deletions core/handlers/v2/views.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ type ClosestMissView struct {

type JournalView struct {
Journal []JournalEntryView `json:"journal"`
Index []JournalIndexView `json:"indexes"`
Offset int `json:"offset"`
Limit int `json:"limit"`
Total int `json:"total"`
Expand All @@ -97,6 +98,7 @@ type JournalEntryView struct {
Mode string `json:"mode"`
TimeStarted string `json:"timeStarted"`
Latency float64 `json:"latency"`
Id string `json:"id"`
}

type JournalEntryFilterView struct {
Expand Down Expand Up @@ -138,3 +140,17 @@ type DiffFilterView struct {
ExcludedHeaders []string `json:"excludedHeaders"`
ExcludedResponseFields []string `json:"excludedResponseFields"`
}

type JournalIndexView struct {
Name string `json:"name"`
Entries []JournalIndexEntryView `json:"entries,omitempty"`
}

type JournalIndexEntryView struct {
Key string `json:"key"`
JournalEntryId string `json:"journalEntryId"`
}

type JournalIndexRequestView struct {
Name string `json:"name"`
}
8 changes: 8 additions & 0 deletions core/hoverfly_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -544,3 +544,11 @@ func (hf *Hoverfly) GetAllDataSources() v2.TemplateDataSourceView {
func (hf *Hoverfly) AddJournalIndex(indexKey string) error {
return hf.Journal.AddIndex(indexKey)
}

func (hf *Hoverfly) DeleteJournalIndex(indexKey string) {
hf.Journal.DeleteIndex(indexKey)
}

func (hf *Hoverfly) GetAllIndexes() []v2.JournalIndexView {
return hf.Journal.GetAllIndexes()
}
39 changes: 39 additions & 0 deletions core/journal/journal.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type JournalEntry struct {
Mode string
TimeStarted time.Time
Latency time.Duration
Id string
}

type Journal struct {
Expand Down Expand Up @@ -65,6 +66,27 @@ func (this *Journal) AddIndex(indexKey string) error {
return nil
}

func (this *Journal) DeleteIndex(indexKey string) {

var indexes []Index
for _, index := range this.Indexes {
if index.Name != indexKey {
indexes = append(indexes, index)
}
}
this.Indexes = indexes
}

func (this *Journal) GetAllIndexes() []v2.JournalIndexView {

var journalIndexViews []v2.JournalIndexView
for _, index := range this.Indexes {

journalIndexViews = append(journalIndexViews, index.getIndexView())
}
return journalIndexViews
}

func (this *Journal) NewEntry(request *http.Request, response *http.Response, mode string, started time.Time) error {
if this.EntryLimit == 0 {
return fmt.Errorf("Journal disabled")
Expand All @@ -91,6 +113,7 @@ func (this *Journal) NewEntry(request *http.Request, response *http.Response, mo
Mode: mode,
TimeStarted: started,
Latency: time.Since(started),
Id: util.RandStringFromTimestamp(15),
}

this.entries = append(this.entries, entry)
Expand Down Expand Up @@ -124,6 +147,7 @@ func (this *Journal) GetEntries(offset int, limit int, from *time.Time, to *time

journalView := v2.JournalView{
Journal: []v2.JournalEntryView{},
Index: []v2.JournalIndexView{},
Offset: offset,
Limit: limit,
Total: 0,
Expand Down Expand Up @@ -183,6 +207,7 @@ func (this *Journal) GetEntries(offset int, limit int, from *time.Time, to *time
}

journalView.Journal = convertJournalEntries(selectedEntries[offset:endIndex])
journalView.Index = convertJournalIndexes(this.Indexes, selectedEntries[offset:endIndex])
journalView.Total = totalElements
return journalView, nil
}
Expand Down Expand Up @@ -251,6 +276,19 @@ func (this *Journal) DeleteEntries() error {
return nil
}

func convertJournalIndexes(indexes []Index, entries []JournalEntry) []v2.JournalIndexView {
filteredJournalEntries := util.NewHashSet()
for _, entry := range entries {
filteredJournalEntries.Add(entry.Id)
}
var journalIndexViews []v2.JournalIndexView
for _, index := range indexes {

journalIndexViews = append(journalIndexViews, index.convertIndex(filteredJournalEntries))
}
return journalIndexViews
}

func convertJournalEntries(entries []JournalEntry) []v2.JournalEntryView {

var journalEntryViews []v2.JournalEntryView
Expand All @@ -262,6 +300,7 @@ func convertJournalEntries(entries []JournalEntry) []v2.JournalEntryView {
Mode: journalEntry.Mode,
TimeStarted: journalEntry.TimeStarted.Format(RFC3339Milli),
Latency: journalEntry.Latency.Seconds() * 1e3,
Id: journalEntry.Id,
})
}

Expand Down
47 changes: 45 additions & 2 deletions core/journal/journal_index.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package journal

import (
v2 "github.com/SpectoLabs/hoverfly/core/handlers/v2"
"github.com/SpectoLabs/hoverfly/core/models"
"github.com/SpectoLabs/hoverfly/core/util"
"github.com/aymerick/raymond"
"strings"
)
Expand All @@ -17,7 +19,8 @@ type Request struct {
Path []string
Scheme string
FormData map[string][]string
body string
Body func(queryType, query string, options *raymond.Options) interface{}
BodyStr string
Method string
}

Expand Down Expand Up @@ -49,7 +52,47 @@ func getRequest(requestDetails *models.RequestDetails) Request {
Header: requestDetails.Headers,
Scheme: requestDetails.Scheme,
FormData: requestDetails.FormData,
body: requestDetails.Body,
BodyStr: requestDetails.Body,
Body: requestBody,
Method: requestDetails.Method,
}
}

func (index Index) convertIndex(filteredJournalEntryIds util.HashSet) v2.JournalIndexView {
var journalIndexEntries []v2.JournalIndexEntryView
for key, journalEntry := range index.Entries {

if filteredJournalEntryIds.Contains(journalEntry.Id) {
journalIndexEntry := v2.JournalIndexEntryView{
Key: key,
JournalEntryId: journalEntry.Id,
}
journalIndexEntries = append(journalIndexEntries, journalIndexEntry)
}
}
return v2.JournalIndexView{
Name: index.Name,
Entries: journalIndexEntries,
}
}

func (index Index) getIndexView() v2.JournalIndexView {
var journalIndexEntries []v2.JournalIndexEntryView
for key, journalEntry := range index.Entries {
journalIndexEntry := v2.JournalIndexEntryView{
Key: key,
JournalEntryId: journalEntry.Id,
}
journalIndexEntries = append(journalIndexEntries, journalIndexEntry)
}
return v2.JournalIndexView{
Name: index.Name,
Entries: journalIndexEntries,
}
}

func requestBody(queryType, query string, options *raymond.Options) interface{} {
toMatch := options.Value("Request").(Request).BodyStr
queryType = strings.ToLower(queryType)
return util.FetchFromRequestBody(queryType, query, toMatch)
}
45 changes: 3 additions & 42 deletions core/matching/matchers/json_path_match.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
package matchers

import (
"bytes"
"encoding/json"
"fmt"

log "github.com/sirupsen/logrus"
"k8s.io/client-go/util/jsonpath"
"github.com/SpectoLabs/hoverfly/core/util"
)

var JsonPath = "jsonpath"
Expand All @@ -17,45 +12,11 @@ func JsonPathMatch(match interface{}, toMatch string) bool {
return false
}

matchString = prepareJsonPathQuery(matchString)
returnedString, err := JsonPathExecution(matchString, toMatch)
matchString = util.PrepareJsonPathQuery(matchString)
returnedString, err := util.JsonPathExecution(matchString, toMatch)
if err != nil || returnedString == matchString {
return false
}

return true
}

func JsonPathExecution(matchString, toMatch string) (string, error) {
jsonPath := jsonpath.New("")

err := jsonPath.Parse(matchString)
if err != nil {
log.Errorf("Failed to parse json path query %s: %s", matchString, err.Error())
return "", err
}

var data interface{}
if err := json.Unmarshal([]byte(toMatch), &data); err != nil {
log.Errorf("Failed to unmarshal body to JSON: %s", err.Error())
return "", err
}

buf := new(bytes.Buffer)

err = jsonPath.Execute(buf, data)
if err != nil {
log.Errorf("err to execute json path match: %s", err.Error())
return "", err
}

return buf.String(), nil
}

func prepareJsonPathQuery(query string) string {
if string(query[0:1]) != "{" && string(query[len(query)-1:]) != "}" {
query = fmt.Sprintf("{%s}", query)
}

return query
}
5 changes: 3 additions & 2 deletions core/matching/matchers/matcher_value_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package matchers

import (
"encoding/json"
"github.com/SpectoLabs/hoverfly/core/util"
"reflect"

log "github.com/sirupsen/logrus"
Expand All @@ -15,7 +16,7 @@ func IdentityValueGenerator(match interface{}, toMatch string) string {

func JsonPathMatcherValueGenerator(match interface{}, toMatch string) string {

matchString := prepareJsonPathQuery(match.(string))
matchString := util.PrepareJsonPathQuery(match.(string))

jsonPath := jsonpath.New("")

Expand Down Expand Up @@ -69,7 +70,7 @@ func getResult(results [][]reflect.Value) string {

func XPathMatchValueGenerator(match interface{}, toMatch string) string {

results, err := XpathExecution(match.(string), toMatch)
results, err := util.XpathExecution(match.(string), toMatch)
if err != nil {
log.Errorf("Failed to generate xpath value: %s", err.Error())
return ""
Expand Down
Loading

0 comments on commit ec75263

Please sign in to comment.