Skip to content

Commit

Permalink
Minor refactor (#175)
Browse files Browse the repository at this point in the history
  • Loading branch information
jmartin82 authored Apr 25, 2024
1 parent 7b2ae02 commit bbff6d5
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 85 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ This is used to have mmock make an API request after receiving the mocked reques
You can use variable data in response. The variables will be defined as tags like this {{nameVar}}

- URI
- Description
- description

**Request data:** Use them if you want to add request data in your response.

Expand Down
17 changes: 17 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.18
require (
github.com/basgys/goxml2json v1.1.0
github.com/elazarl/go-bindata-assetfs v1.0.0
github.com/fatih/color v1.16.0
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
Expand All @@ -16,6 +17,22 @@ require (
github.com/tidwall/gjson v1.9.3
github.com/twinj/uuid v1.0.0
golang.org/x/net v0.23.0
)

require (
github.com/bitly/go-simplejson v0.5.1 // indirect
github.com/corpix/uarand v0.2.0 // indirect
github.com/labstack/gommon v0.4.2 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/myesui/uuid v1.0.0 // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.0 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasttemplate v1.2.2 // indirect
golang.org/x/crypto v0.21.0 // indirect
golang.org/x/sys v0.18.0 // indirect
golang.org/x/text v0.14.0 // indirect
gopkg.in/stretchr/testify.v1 v1.2.2 // indirect
gopkg.in/yaml.v2 v2.3.0 // indirect
)
59 changes: 6 additions & 53 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
github.com/Masterminds/glide v0.13.2/go.mod h1:STyF5vcenH/rUqTEv+/hBXlSTo7KYwg2oc2f4tzPWic=
github.com/Masterminds/semver v1.4.2/go.mod h1:MB6lktGJrhw8PrUyiEoblNEGEQ+RzHPF078ddwwvV3Y=
github.com/Masterminds/vcs v1.13.0/go.mod h1:N09YCmOQr6RLxC6UNHzuVwAdodYbbnycGHSmwVJjcKA=
github.com/basgys/goxml2json v1.1.0 h1:4ln5i4rseYfXNd86lGEB+Vi652IsIXIvggKM/BhUKVw=
github.com/basgys/goxml2json v1.1.0/go.mod h1:wH7a5Np/Q4QoECFIU8zTQlZwZkrilY0itPfecMw41Dw=
github.com/bitly/go-simplejson v0.5.0 h1:6IH+V8/tVMab511d5bn4M7EwGXZf9Hj6i2xSwkNEM+Y=
github.com/bitly/go-simplejson v0.5.0/go.mod h1:cXHtHw4XUPsvGaxgjIAn8PhEWG9NfngEKAMDJEczWVA=
github.com/codegangsta/cli v1.20.0/go.mod h1:/qJNoX69yVSKu5o4jLyXAENLRyk1uhi7zkbQ3slBdOA=
github.com/corpix/uarand v0.1.1 h1:RMr1TWc9F4n5jiPDzFHtmaUXLKLNUFK0SgCLo4BhX/U=
github.com/corpix/uarand v0.1.1/go.mod h1:SFKZvkcRoLqVRFZ4u25xPmp6m9ktANfbpXZ7SJ0/FNU=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/bitly/go-simplejson v0.5.1 h1:xgwPbetQScXt1gh9BmoJ6j9JMr3TElvuIyjR8pgdoow=
github.com/bitly/go-simplejson v0.5.1/go.mod h1:YOPVLzCfwK14b4Sff3oP1AmGhI9T9Vsg84etUnlyp+Q=
github.com/corpix/uarand v0.2.0 h1:U98xXwud/AVuCpkpgfPF7J5TQgr7R5tqT8VZP5KWbzE=
github.com/corpix/uarand v0.2.0/go.mod h1:/3Z1QIqWkDIhf6XWn/08/uMHoQ8JUoTIKc2iPchBOmM=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
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.16.0 h1:zmkK9Ngbjj+K0yRhTVONQh1p/HknKYSlNT+vZCzyokM=
github.com/fatih/color v1.16.0/go.mod h1:fL2Sau1YI5c0pdGEVCbKQbLXB6edEj1ZgiY4NijnWvE=
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=
Expand All @@ -27,22 +24,17 @@ github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovk
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
github.com/myesui/uuid v1.0.0 h1:xCBmH4l5KuvLYc5L7AS7SZg9/jKdIFubM7OVoLqaQUI=
github.com/myesui/uuid v1.0.0/go.mod h1:2CDfNgU0LR8mIdO8vdWd8i9gWWxLlcoIGGpSNgafq84=
github.com/ngdinhtoan/glide-cleanup v0.2.0/go.mod h1:UQzsmiDOb8YV3nOsCxK/c9zPpCZVNoHScRE3EO9pVMM=
github.com/op/go-logging v0.0.0-20160315200505-970db520ece7 h1:lDH9UUVJtmYCjyT0CI4q8xvlXPxeZ0gYCVvWbmPlp88=
github.com/op/go-logging v0.0.0-20160315200505-970db520ece7/go.mod h1:HzydrMdWErDVzsI23lYNej1Htcns9BCg93Dk0bBINWk=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/radovskyb/watcher v1.0.7 h1:AYePLih6dpmS32vlHfhCeli8127LzkIgwJGcwwe8tUE=
github.com/radovskyb/watcher v1.0.7/go.mod h1:78okwvY5wPdzcb1UYnip1pvrZNIVEIh/Cm+ZuvsUYIg=
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=
github.com/stathat/go v1.0.0/go.mod h1:+9Eg2szqkcOGWv6gfheJmBBsmq9Qf5KDbzy8/aYYR0c=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/tidwall/gjson v1.9.3 h1:hqzS9wAHMO+KVBBkLxYdkEeeFHuqr95GfClRLKlgK0E=
github.com/tidwall/gjson v1.9.3/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
Expand All @@ -56,59 +48,20 @@ github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6Kllzaw
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
github.com/valyala/fasttemplate v1.2.2 h1:lxLXG0uE3Qnshl9QyaK6XJxMXlQZELvChBOCmQD0Loo=
github.com/valyala/fasttemplate v1.2.2/go.mod h1:KHLXt3tVN2HBp8eijSv/kGJopbvo7S+qRAEEKiv+SiQ=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.16.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4=
golang.org/x/crypto v0.17.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4=
golang.org/x/crypto v0.19.0/go.mod h1:Iy9bg/ha4yyC70EfRS8jz+B6ybOBKMaSxLj6P6oBDfU=
golang.org/x/crypto v0.21.0 h1:X31++rzVUdKhX5sWmSOFZxx8UW/ldWx55cbf08iNAMA=
golang.org/x/crypto v0.21.0/go.mod h1:0BP7YvVV9gBbVKyeTG0Gyn+gZm94bibOW5BjDEYAOMs=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg=
golang.org/x/net v0.19.0/go.mod h1:CfAk/cbD4CthTvqiEl8NpboMuiuOYsAr/7NOjZJtv1U=
golang.org/x/net v0.21.0/go.mod h1:bIjVDfnllIU7BJ2DNgfnXvpSvtn8VRwhlsaeUTyUS44=
golang.org/x/net v0.23.0 h1:7EYJ93RZ9vYSZAIb2x3lnuvqO5zneoD6IvWjuhfxjTs=
golang.org/x/net v0.23.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4=
golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
golang.org/x/term v0.8.0/go.mod h1:xPskH00ivmX89bAKVGSKKtLOWNx2+17Eiy94tnKShWo=
golang.org/x/term v0.15.0/go.mod h1:BDl952bC7+uMoWR75FIrCDx79TPU9oHkTZ9yRbYOrX0=
golang.org/x/term v0.17.0/go.mod h1:lLRBjIVuehSbZlaOtGMbcMncT+aqLLLmKrsjNrUguwk=
golang.org/x/term v0.18.0/go.mod h1:ILwASektA3OnRv7amZ1xhE/KTR+u50pbXfZ03+6Nx58=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/stretchr/testify.v1 v1.2.2 h1:yhQC6Uy5CqibAIlk1wlusa/MJ3iAN49/BsR/dCCKz3M=
gopkg.in/stretchr/testify.v1 v1.2.2/go.mod h1:QI5V/q6UbPmuhtm10CaFZxED9NreB8PnFYN9JcR6TxU=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU=
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
34 changes: 18 additions & 16 deletions internal/server/dispatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,6 @@ import (
"crypto/x509"
"encoding/json"
"fmt"
"github.com/jmartin82/mmock/v3/internal/config/logger"
"github.com/jmartin82/mmock/v3/internal/proxy"
"github.com/jmartin82/mmock/v3/internal/statistics"
"github.com/jmartin82/mmock/v3/pkg/match"
"github.com/jmartin82/mmock/v3/pkg/mock"
"github.com/jmartin82/mmock/v3/pkg/vars"
"io/ioutil"
"math/rand"
"net/http"
Expand All @@ -21,6 +15,13 @@ import (
"strconv"
"strings"
"time"

"github.com/jmartin82/mmock/v3/internal/config/logger"
"github.com/jmartin82/mmock/v3/internal/proxy"
"github.com/jmartin82/mmock/v3/internal/statistics"
"github.com/jmartin82/mmock/v3/pkg/match"
"github.com/jmartin82/mmock/v3/pkg/mock"
"github.com/jmartin82/mmock/v3/pkg/vars"
)

var log = logger.Log
Expand Down Expand Up @@ -76,7 +77,7 @@ func (di *Dispatcher) ServeHTTP(w http.ResponseWriter, req *http.Request) {
return
}

log.Infof("\033[35mNew request:\033[0m %s %s\n", req.Method, req.URL.String())
log.Infof("%s %s %s\n", terminal.Info("New request:"), req.Method, req.URL.String())

mock, transaction := di.getMatchingResult(&mRequest)

Expand Down Expand Up @@ -138,16 +139,9 @@ func getProxyResponse(request *mock.Request, definition *mock.Definition) *mock.

func (di *Dispatcher) getMatchingResult(request *mock.Request) (*mock.Definition, *match.Transaction) {
var response *mock.Response
var colour = "\033[31m"
var stop_colour = "\033[0m"
mock, result := di.Resolver.Resolve(request)

if result.Found {
colour = "\033[32m"
}

log.Infof("%sDefinition match found:%s%s. Name : %s\n", colour, stop_colour, strconv.FormatBool(result.Found), mock.URI)

mock, result := di.Resolver.Resolve(request)
colorTerminalOutput(result, mock)

if result.Found {
if len(mock.Control.ProxyBaseURL) > 0 {
Expand Down Expand Up @@ -178,6 +172,14 @@ func (di *Dispatcher) getMatchingResult(request *mock.Request) (*mock.Definition

}

func colorTerminalOutput(result *match.Result, mock *mock.Definition) {
msg := fmt.Sprintf("%s %s\n", terminal.Error("Definition match found:"), strconv.FormatBool(result.Found))
if result.Found {
msg = fmt.Sprintf("%s %s %s %s\n", terminal.Success("Definition match found:"), strconv.FormatBool(result.Found), terminal.Success("Name:"), mock.URI)
}
log.Info(msg)
}

// Start initialize the HTTP mock server
func (di Dispatcher) Start() {
addr := fmt.Sprintf("%s:%d", di.IP, di.Port)
Expand Down
10 changes: 5 additions & 5 deletions internal/server/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package server
import (
"bytes"
"encoding/gob"
"errors"

"github.com/jmartin82/mmock/v3/internal/config"
"github.com/jmartin82/mmock/v3/pkg/match"
"github.com/jmartin82/mmock/v3/pkg/mock"
Expand Down Expand Up @@ -50,20 +50,20 @@ func (rr *Router) Resolve(req *mock.Request) (*mock.Definition, *match.Result) {
mLog.Errors = make([]match.Error, 0, len(mocks))

for _, m := range mocks {
log.Debugf("\033[34mConsidering:\033[0m %s", m.URI)
log.Debugf("%s %s", terminal.Info("Considering:"), m.URI)

r, err := rr.Checker.Match(req, &m, true)
if r {
//we return a copy of it, not the config itself because we will working on it.
md := mock.Definition{}
rr.copy(&m, &md)
mLog.Found = true
mLog.URI = m.URI
log.Debugf("%s %s\n", terminal.Success("Found mock:"), m.URI)
return &md, mLog
}
mLog.Errors = append(mLog.Errors, match.Error{URI: m.URI, Reason: err.Error()})
if !errors.Is(err, match.ErrPathNotMatch) {
log.Infof("\033[31mDiscarding mock:\033[0m %s Reason: %s\n", m.URI, err.Error())
}
log.Debugf("%s %s %s %s\n", terminal.Error("Discarding mock:"), m.URI, terminal.Error("Reason:"), err.Error())
}
return getNotFoundResult(), mLog
}
Expand Down
15 changes: 15 additions & 0 deletions internal/server/terminal.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package server

import "github.com/fatih/color"

type terminalColors struct {
Info func(a ...interface{}) string
Success func(a ...interface{}) string
Error func(a ...interface{}) string
}

var terminal = terminalColors{
Info: color.New(color.FgBlue).SprintFunc(),
Success: color.New(color.Bold, color.FgGreen).SprintFunc(),
Error: color.New(color.Bold, color.FgRed).SprintFunc(),
}
13 changes: 7 additions & 6 deletions pkg/vars/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@ package vars

import (
"fmt"
xj "github.com/basgys/goxml2json"
"github.com/jmartin82/mmock/v3/pkg/mock"
"github.com/jmartin82/mmock/v3/pkg/route"
"github.com/tidwall/gjson"
"net/url"
"os"
"regexp"
"sort"
"strings"

xj "github.com/basgys/goxml2json"
"github.com/jmartin82/mmock/v3/pkg/mock"
"github.com/jmartin82/mmock/v3/pkg/route"
"github.com/tidwall/gjson"
)

type Request struct {
Expand Down Expand Up @@ -53,9 +54,9 @@ func (rp Request) Fill(holders []string) map[string][]string {
s, found = rp.getHeaderParam(tag[15:])
} else if strings.HasPrefix(tag, "env.") {
s, found = os.LookupEnv(tag[4:])
} else if tag == "URI" {
} else if strings.ToUpper(tag) == "URI" {
s, found = rp.Mock.URI, true
} else if tag == "Description" {
} else if strings.ToLower(tag) == "description" {
s, found = rp.Mock.Description, true
}

Expand Down
8 changes: 4 additions & 4 deletions pkg/vars/request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,11 +199,11 @@ func TestDescription(t *testing.T) {
res := mock.Response{}
res.Headers = make(mock.Values)
res.Headers["Content-Type"] = []string{"application/json"}
res.Headers[MOCK_HEADER_NAME] = []string{"{{ Description }}"}
res.Headers[MOCK_HEADER_NAME] = []string{"{{ description }}"}

res.Body = `
{
"Description": "{{ Description }}",
"Description": "{{ description }}",
}
`
var expectedBody = fmt.Sprintf(`
Expand All @@ -219,11 +219,11 @@ func TestDescription(t *testing.T) {
varsProcessor.Eval(&req, &mock)

if mock.Response.Body != expectedBody {
t.Error("failed to replace URI in response body", mock.Response.Body)
t.Error("failed to replace Description in response body", mock.Response.Body)
}

var headerReplaced = slices.Contains(mock.Response.Headers[MOCK_HEADER_NAME], MOCK_DESCRIPTION)
if !headerReplaced {
t.Error("failed to replace URI in response headers", mock.Response.Body)
t.Error("failed to replace Description in response headers", mock.Response.Body)
}
}

0 comments on commit bbff6d5

Please sign in to comment.