Skip to content

Commit

Permalink
CC generation fixed +small code refactor (#90)
Browse files Browse the repository at this point in the history
* CC generation fixed +small code refactor

* Name refactor
  • Loading branch information
jmartin82 authored Jun 10, 2019
1 parent f3aaa12 commit 0ddccc4
Show file tree
Hide file tree
Showing 24 changed files with 284 additions and 188 deletions.
38 changes: 20 additions & 18 deletions cmd/mmock/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@ import (
"path/filepath"
"strings"

"github.com/jmartin82/mmock/internal/console"
"github.com/jmartin82/mmock/internal/config"
"github.com/jmartin82/mmock/internal/config/parser"
"github.com/jmartin82/mmock/pkg/match"
"github.com/jmartin82/mmock/pkg/match/payload"
"github.com/jmartin82/mmock/internal/console"
"github.com/jmartin82/mmock/internal/server"
"github.com/jmartin82/mmock/internal/statistics"
"github.com/jmartin82/mmock/pkg/mock"
"github.com/jmartin82/mmock/internal/vars"
"github.com/jmartin82/mmock/internal/vars/fakedata"
"github.com/jmartin82/mmock/internal/vars/fake"
"github.com/jmartin82/mmock/pkg/match"
"github.com/jmartin82/mmock/pkg/match/payload"
"github.com/jmartin82/mmock/pkg/mock"
)

//VERSION of the application
Expand Down Expand Up @@ -74,7 +74,7 @@ func getOutboundIP() string {
return localAddr[0:idx]
}

func getMatchSpier(checker match.Matcher, matchStore match.Storer) match.Spier {
func getTransactionSpy(checker match.Matcher, matchStore match.TransactionStorer) *match.Spy {
return match.NewSpy(checker, matchStore)
}

Expand Down Expand Up @@ -112,28 +112,30 @@ func getRouter(mapping config.Mapping, checker match.Matcher) *server.Router {
return router
}

func getVarsProcessor() vars.Evaluator {

return vars.Evaluator{FillerFactory: vars.MockFillerFactory{FakeAdapter: fakedata.FakeAdapter{}}}
func getVarsProcessor() *vars.ResponseMessageEvaluator {
ccg := fake.NewCreditCardGenerator()
fp := fake.NewFakeDataProvider(ccg)
ff := vars.NewFillerFactory(fp)
return vars.NewResponseMessageEvaluator(ff)
}

func startServer(ip string, port, portTLS int, configTLS string, done chan bool, router server.Resolver, mLog chan match.Log, scenario match.ScenearioStorer, varsProcessor vars.Evaluator, spier match.Spier) {
func startServer(ip string, port, portTLS int, configTLS string, done chan struct{}, router server.RequestResolver, mLog chan match.Log, scenario match.ScenearioStorer, varsProcessor vars.Evaluator, spier match.TransactionSpier) {
dispatcher := server.Dispatcher{
IP: ip,
Port: port,
PortTLS: portTLS,
ConfigTLS: configTLS,
Resolver: router,
Translator: mock.HTTP{},
Processor: varsProcessor,
Evaluator: varsProcessor,
Scenario: scenario,
Spier: spier,
Mlog: mLog,
}
dispatcher.Start()
done <- true
done <- struct{}{}
}
func startConsole(ip string, port int, resultsPerPage uint, spy match.Spier, scenario match.ScenearioStorer, mapping config.Mapping, done chan bool, mLog chan match.Log) {
func startConsole(ip string, port int, resultsPerPage uint, spy match.TransactionSpier, scenario match.ScenearioStorer, mapping config.Mapping, done chan struct{}, mLog chan match.Log) {
dispatcher := console.Dispatcher{
IP: ip,
Port: port,
Expand All @@ -144,7 +146,7 @@ func startConsole(ip string, port int, resultsPerPage uint, spy match.Spier, sce
ResultsPerPage: resultsPerPage,
}
dispatcher.Start()
done <- true
done <- struct{}{}
}

func main() {
Expand All @@ -171,15 +173,15 @@ func main() {

//chanels
mLog := make(chan match.Log)
done := make(chan bool)
done := make(chan struct{})

//shared structs
scenario := match.NewScenarioStore()
scenario := match.NewInMemoryScenarioStore()
comparator := payload.NewDefaultComparator()
tester := match.NewTester(comparator, scenario)
matchStore := match.NewStore(tester)
matchStore := match.NewInMemoryTransactionStore(tester)
mapping := getMapping(*cPath)
spy := getMatchSpier(tester, matchStore)
spy := getTransactionSpy(tester, matchStore)
router := getRouter(mapping, tester)
varsProcessor := getVarsProcessor()

Expand Down
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,18 @@ require (
github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869 // indirect
github.com/corpix/uarand v0.0.0 // indirect
github.com/elazarl/go-bindata-assetfs v1.0.0
github.com/fatih/color v1.7.0 // indirect
github.com/ghodss/yaml v1.0.0
github.com/icrowley/fake v0.0.0-20180203215853-4178557ae428
github.com/joeljunstrom/go-luhn v0.0.0-20190413165225-1e071b33b576
github.com/kr/pretty v0.1.0 // indirect
github.com/labstack/echo v3.3.10+incompatible
github.com/labstack/gommon v0.2.8 // indirect
github.com/mattn/go-colorable v0.1.1 // indirect
github.com/mattn/go-isatty v0.0.7 // indirect
github.com/myesui/uuid v1.0.0 // indirect
github.com/radovskyb/watcher v1.0.6
github.com/rakyll/gotest v0.0.0-20180125184505-86f0749cd8cc // indirect
github.com/ryanuber/go-glob v1.0.0
github.com/stathat/go v1.0.0
github.com/stretchr/testify v1.3.0 // indirect
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,14 @@ github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/elazarl/go-bindata-assetfs v1.0.0 h1:G/bYguwHIzWq9ZoyUQqrjTmJbbYn3j3CKKpKinvZLFk=
github.com/elazarl/go-bindata-assetfs v1.0.0/go.mod h1:v+YaWX3bdea5J/mo8dSETolEo7R71Vk1u8bnjau5yw4=
github.com/fatih/color v1.7.0 h1:DkWD4oS2D8LGGgTQ6IvwJJXSL5Vp2ffcQg58nFV38Ys=
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
github.com/ghodss/yaml v1.0.0 h1:wQHKEahhL6wmXdzwWG11gIVCkOv05bNOh+Rxn0yngAk=
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
github.com/icrowley/fake v0.0.0-20180203215853-4178557ae428 h1:Mo9W14pwbO9VfRe+ygqZ8dFbPpoIK1HFrG/zjTuQ+nc=
github.com/icrowley/fake v0.0.0-20180203215853-4178557ae428/go.mod h1:uhpZMVGznybq1itEKXj6RYw9I71qK4kH+OGMjRC4KEo=
github.com/joeljunstrom/go-luhn v0.0.0-20190413165225-1e071b33b576 h1:k82KNEG8vk59eHv/8xwBUh4dSR/t1wPiht4aDJm0SOY=
github.com/joeljunstrom/go-luhn v0.0.0-20190413165225-1e071b33b576/go.mod h1:pE5zuSeg07RZZfWS158WpV7oUWb1++8T2jZ/UklLM3E=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
Expand All @@ -36,6 +40,8 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/radovskyb/watcher v1.0.6 h1:8WIQ9UxEYMZjem1OwU7dVH94DXXk9mAIE1i8eqHD+IY=
github.com/radovskyb/watcher v1.0.6/go.mod h1:78okwvY5wPdzcb1UYnip1pvrZNIVEIh/Cm+ZuvsUYIg=
github.com/rakyll/gotest v0.0.0-20180125184505-86f0749cd8cc h1:hrzpgS8mnUi65ieVrD3TKJMxHP84bzmybMTQIdK/XhM=
github.com/rakyll/gotest v0.0.0-20180125184505-86f0749cd8cc/go.mod h1:iln+RRtJaJ52lKwqrSmNgQYw32Fk16CgChX85eFqBgI=
github.com/ryanuber/go-glob v1.0.0 h1:iQh3xXAumdQ+4Ufa5b25cRpC5TYKlno6hsv6Cb3pkBk=
github.com/ryanuber/go-glob v1.0.0/go.mod h1:807d1WSdnB0XRJzKNil9Om6lcp/3a0v4qIHxIXzX/Yc=
github.com/stathat/go v1.0.0 h1:HFIS5YkyaI6tXu7JXIRRZBLRvYstdNZm034zcCeaybI=
Expand Down
2 changes: 1 addition & 1 deletion internal/console/dispatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ type Dispatcher struct {
IP string
Port int
ResultsPerPage uint
MatchSpy match.Spier
MatchSpy match.TransactionSpier
Scenario match.ScenearioStorer
Mapping config.Mapping
Mlog chan match.Log
Expand Down
9 changes: 5 additions & 4 deletions internal/proxy/proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ import (
"bytes"
"errors"
"fmt"
"github.com/jmartin82/mmock/pkg/mock"
"io"
"net/http"
"testing"

"github.com/jmartin82/mmock/pkg/mock"
)

type MockClient struct {
Expand Down Expand Up @@ -47,7 +48,7 @@ func TestMakeValidRequest(t *testing.T) {
request.Host = "http://mock_host.com"
request.Method = "GET"
request.Path = "/home"
url := "http://example.com"
url := "http://jordi.io"
proxy := Proxy{URL: url, Client: client}
response := proxy.MakeRequest(request)

Expand Down Expand Up @@ -81,8 +82,8 @@ func TestMakeInvalidRequest(t *testing.T) {
request := &mock.Request{}
request.Host = "http://mock_host.com"
request.Method = "GET"
request.Path = "/home"
url := "http://example.com"
request.Path = "/home/OriolMG"
url := "http://jordi.io"
proxy := Proxy{URL: url, Client: client}
response := proxy.MakeRequest(request)
if response.StatusCode != http.StatusInternalServerError {
Expand Down
19 changes: 10 additions & 9 deletions internal/server/dispatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,16 @@ import (
"strconv"
"time"

"github.com/jmartin82/mmock/pkg/match"
"github.com/jmartin82/mmock/internal/proxy"
"github.com/jmartin82/mmock/pkg/match"

"github.com/jmartin82/mmock/internal/statistics"
"github.com/jmartin82/mmock/pkg/mock"
"github.com/jmartin82/mmock/internal/vars"
"net/url"
"regexp"
"strings"

"github.com/jmartin82/mmock/internal/statistics"
"github.com/jmartin82/mmock/internal/vars"
"github.com/jmartin82/mmock/pkg/mock"
)

//Dispatcher is the mock http server
Expand All @@ -31,11 +32,11 @@ type Dispatcher struct {
Port int
PortTLS int
ConfigTLS string
Resolver Resolver
Resolver RequestResolver
Translator mock.MessageTranslator
Processor vars.Evaluator
Evaluator vars.Evaluator
Scenario match.ScenearioStorer
Spier match.Spier
Spier match.TransactionSpier
Mlog chan match.Log
}

Expand Down Expand Up @@ -137,7 +138,7 @@ func (di *Dispatcher) getMatchingResult(request *mock.Request) (*mock.Definition
response = getProxyResponse(request, mock)
} else {

di.Processor.Eval(request, mock)
di.Evaluator.Eval(request, mock)
if mock.Control.Crazy {
log.Println("Running crazy mode")
mock.Response.StatusCode = di.randomStatusCode(mock.Response.StatusCode)
Expand Down Expand Up @@ -183,7 +184,7 @@ func (di Dispatcher) Start() {
err := <-errCh

if err != nil {
log.Fatalf("ListenAndServe: %s" , err.Error())
log.Fatalf("ListenAndServe: %s", err.Error())
}

}
Expand Down
4 changes: 2 additions & 2 deletions internal/server/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (
"github.com/jmartin82/mmock/pkg/match"
)

//Resolver contains the functions to check the http request and return the matching mock.
type Resolver interface {
//RequestResolver contains the functions to check the http request and return the matching mock.
type RequestResolver interface {
Resolve(req *mock.Request) (*mock.Definition, *match.Result)
}

Expand Down
25 changes: 16 additions & 9 deletions internal/vars/evaluator.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,27 @@
package vars

import (
"github.com/jmartin82/mmock/pkg/mock"
"regexp"
"strings"


"github.com/jmartin82/mmock/pkg/mock"
)

var varsRegex = regexp.MustCompile(`\{\{\s*(.+?)\s*\}\}`)

type Evaluator struct {
type Evaluator interface {
Eval(req *mock.Request, m *mock.Definition)
}

type ResponseMessageEvaluator struct {
FillerFactory FillerFactory
}

func (fp Evaluator) Eval(req *mock.Request, m *mock.Definition) {
func NewResponseMessageEvaluator(fp FillerFactory) *ResponseMessageEvaluator {
return &ResponseMessageEvaluator{FillerFactory: fp}
}

func (fp ResponseMessageEvaluator) Eval(req *mock.Request, m *mock.Definition) {
requestFiller := fp.FillerFactory.CreateRequestFiller(req, m)
fakeFiller := fp.FillerFactory.CreateFakeFiller()
streamFiller := fp.FillerFactory.CreateStreamFiller()
Expand All @@ -26,7 +33,7 @@ func (fp Evaluator) Eval(req *mock.Request, m *mock.Definition) {
fp.walkAndFill(m, vars)
}

func (fp Evaluator) walkAndGet(res mock.Response) []string {
func (fp ResponseMessageEvaluator) walkAndGet(res mock.Response) []string {

vars := []string{}
for _, header := range res.Headers {
Expand All @@ -43,7 +50,7 @@ func (fp Evaluator) walkAndGet(res mock.Response) []string {
return vars
}

func (fp Evaluator) walkAndFill(m *mock.Definition, vars map[string][]string) {
func (fp ResponseMessageEvaluator) walkAndFill(m *mock.Definition, vars map[string][]string) {
res := &m.Response
for header, values := range res.Headers {
for i, value := range values {
Expand All @@ -58,7 +65,7 @@ func (fp Evaluator) walkAndFill(m *mock.Definition, vars map[string][]string) {
res.Body = fp.replaceVars(res.Body, vars)
}

func (fp Evaluator) replaceVars(input string, vars map[string][]string) string {
func (fp ResponseMessageEvaluator) replaceVars(input string, vars map[string][]string) string {
return varsRegex.ReplaceAllStringFunc(input, func(value string) string {
varName := strings.Trim(value, "{} ")
// replace the strings
Expand All @@ -72,7 +79,7 @@ func (fp Evaluator) replaceVars(input string, vars map[string][]string) string {
})
}

func (fp Evaluator) extractVars(input string, vars *[]string) {
func (fp ResponseMessageEvaluator) extractVars(input string, vars *[]string) {
if m := varsRegex.FindAllString(input, -1); m != nil {
for _, v := range m {
varName := strings.Trim(v, "{} ")
Expand All @@ -81,7 +88,7 @@ func (fp Evaluator) extractVars(input string, vars *[]string) {
}
}

func (fp Evaluator) mergeVars(org map[string][]string, vals map[string][]string) {
func (fp ResponseMessageEvaluator) mergeVars(org map[string][]string, vals map[string][]string) {
for k, v := range vals {
org[k] = v
}
Expand Down
11 changes: 6 additions & 5 deletions internal/vars/evaluator_test.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package vars

import (
"github.com/jmartin82/mmock/pkg/mock"
"testing"

"github.com/jmartin82/mmock/pkg/mock"

"strconv"

"strings"


)

//DummyDataFaker is used in tests
Expand Down Expand Up @@ -178,8 +177,10 @@ func (ddf DummyDataFaker) Hex(n int) string {
return strings.Repeat("0", n)
}

func getProcessor() Evaluator {
return Evaluator{FillerFactory: MockFillerFactory{FakeAdapter: NewDummyDataFaker("AleixMG")}}
func getProcessor() ResponseMessageEvaluator {
dfp := NewDummyDataFaker("AleixMG")
ff := NewFillerFactory(dfp)
return ResponseMessageEvaluator{FillerFactory: ff}
}

func TestReplaceTags(t *testing.T) {
Expand Down
7 changes: 5 additions & 2 deletions internal/vars/fake.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package vars

import (
"errors"
"github.com/jmartin82/mmock/internal/vars/fakedata"
"github.com/jmartin82/mmock/internal/vars/fake"
"log"
"reflect"
"regexp"
Expand All @@ -11,11 +11,14 @@ import (

)


var errMissingParameterValue = errors.New("The requested method needs input parameters which are not supplied!")



//Fake parses the data looking for fake data tags or request data tags
type Fake struct {
Fake fakedata.DataFaker
Fake fake.Generator
}

func (fv Fake) call(data reflect.Value, name string) (string, error) {
Expand Down
Loading

0 comments on commit 0ddccc4

Please sign in to comment.