Skip to content

Commit

Permalink
Add test hash to meta to support tooling on top of api test
Browse files Browse the repository at this point in the history
  • Loading branch information
Stein Fletcher committed Feb 13, 2019
1 parent 4856360 commit 9f39473
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 23 deletions.
22 changes: 22 additions & 0 deletions apitest.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"fmt"
"github.com/steinfletcher/apitest/assert"
"hash/fnv"
"io/ioutil"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -462,6 +463,7 @@ func (r *Response) Report(formatter ...ReportFormatter) {
meta["method"] = capturedInboundReq.Method
meta["name"] = apiTest.name
meta["host"] = apiTest.request.GetHost()
meta["hash"] = createHash(meta)

recorder.AddMeta(meta)

Expand All @@ -472,6 +474,26 @@ func (r *Response) Report(formatter ...ReportFormatter) {
}
}

func createHash(meta map[string]interface{}) string {
path := meta["path"]
method := meta["method"]
name := meta["name"]
host := meta["host"]

prefix := fnv.New32a()
_, err := prefix.Write([]byte(fmt.Sprintf("%s%s%s", host, strings.ToUpper(method.(string)), path)))
if err != nil {
panic(err)
}

suffix := fnv.New32a()
_, err = suffix.Write([]byte(name.(string)))
if err != nil {
panic(err)
}
return fmt.Sprintf("%d_%d", prefix.Sum32(), suffix.Sum32())
}

func (r *Response) execute() {
apiTest := r.apiTest
if len(apiTest.mocks) > 0 {
Expand Down
24 changes: 1 addition & 23 deletions diagram.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"encoding/json"
"errors"
"fmt"
"hash/fnv"
"html/template"
"io"
"io/ioutil"
Expand All @@ -14,7 +13,6 @@ import (
"os"
"path/filepath"
"strconv"
"strings"
"time"
)

Expand Down Expand Up @@ -102,7 +100,7 @@ func (r *SequenceDiagramFormatter) Format(recorder *Recorder) {
panic(err)
}

fileName := createFileName(recorder.Meta)
fileName := fmt.Sprintf("%s.html", recorder.Meta["hash"])
err = r.fs.MkdirAll(r.storagePath, os.ModePerm)
if err != nil {
panic(err)
Expand All @@ -122,26 +120,6 @@ func (r *SequenceDiagramFormatter) Format(recorder *Recorder) {
fmt.Printf("Created sequence diagram (%s): %s\n", fileName, filepath.FromSlash(s))
}

func createFileName(meta map[string]interface{}) string {
path := meta["path"]
method := meta["method"]
name := meta["name"]
host := meta["host"]

prefix := fnv.New32a()
_, err := prefix.Write([]byte(fmt.Sprintf("%s%s%s", host, strings.ToUpper(method.(string)), path)))
if err != nil {
panic(err)
}

suffix := fnv.New32a()
_, err = suffix.Write([]byte(name.(string)))
if err != nil {
panic(err)
}
return fmt.Sprintf("%d_%d.html", prefix.Sum32(), suffix.Sum32())
}

func NewSequenceDiagramFormatter(path ...string) *SequenceDiagramFormatter {
var storagePath string
if len(path) == 0 {
Expand Down

0 comments on commit 9f39473

Please sign in to comment.