Skip to content

Commit

Permalink
Fixed crazy mode on proxy (#137)
Browse files Browse the repository at this point in the history
  • Loading branch information
jmartin82 committed Feb 26, 2023
1 parent a1da1ae commit b1335f3
Showing 1 changed file with 14 additions and 16 deletions.
30 changes: 14 additions & 16 deletions internal/server/dispatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
"github.com/jmartin82/mmock/v3/pkg/vars"
)

//Dispatcher is the mock http server
// Dispatcher is the mock http server
type Dispatcher struct {
IP string
Port int
Expand Down Expand Up @@ -68,8 +68,8 @@ func (di Dispatcher) callWebHook(url string, match *match.Transaction) {
log.Printf("WebHook response: %d\n", resp.StatusCode)
}

//ServerHTTP is the mock http server request handler.
//It uses the router to decide the matching mock and translator as adapter between the HTTP impelementation and the mock mock.
// ServerHTTP is the mock http server request handler.
// It uses the router to decide the matching mock and translator as adapter between the HTTP impelementation and the mock mock.
func (di *Dispatcher) ServeHTTP(w http.ResponseWriter, req *http.Request) {
mRequest := di.Translator.BuildRequestDefinitionFromHTTP(req)

Expand Down Expand Up @@ -138,7 +138,7 @@ func getProxyResponse(request *mock.Request, definition *mock.Definition) *mock.
}

func (di *Dispatcher) getMatchingResult(request *mock.Request) (*mock.Definition, *match.Transaction) {
response := &mock.Response{}
var response *mock.Response
mock, result := di.Resolver.Resolve(request)

log.Printf("Definition match found: %s. Name : %s\n", strconv.FormatBool(result.Found), mock.URI)
Expand All @@ -148,21 +148,19 @@ func (di *Dispatcher) getMatchingResult(request *mock.Request) (*mock.Definition
statistics.TrackProxyFeature()
response = getProxyResponse(request, mock)
} else {

di.Evaluator.Eval(request, mock)
if mock.Control.Crazy {
log.Println("Running crazy mode")
mock.Response.StatusCode = di.randomStatusCode(mock.Response.StatusCode)
}

if d := mock.Control.Delay.Duration; d > 0 {
log.Printf("Adding a delay of: %s\n", d)
time.Sleep(d)
}

response = &mock.Response
}

if mock.Control.Crazy {
log.Println("Running crazy mode")
response.StatusCode = di.randomStatusCode(response.StatusCode)
}
if d := mock.Control.Delay.Duration; d > 0 {
log.Printf("Adding a delay of: %s\n", d)
time.Sleep(d)
}

statistics.TrackMockRequest()
} else {
response = &mock.Response
Expand All @@ -174,7 +172,7 @@ func (di *Dispatcher) getMatchingResult(request *mock.Request) (*mock.Definition

}

//Start initialize the HTTP mock server
// Start initialize the HTTP mock server
func (di Dispatcher) Start() {
addr := fmt.Sprintf("%s:%d", di.IP, di.Port)
addrTLS := fmt.Sprintf("%s:%d", di.IP, di.PortTLS)
Expand Down

0 comments on commit b1335f3

Please sign in to comment.