Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed crazy mode on proxy #137

Merged
merged 1 commit into from
Feb 26, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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