Пример #1
0
        private MockEngineResponse BuildMockEngineResponse(V8ScriptEngine scriptEngine, string scenarioName, MockAction action)
        {
            var mockResponse = action.Response;
            var response     = new MockEngineResponse()
            {
                Success = true
            };

            if (!string.IsNullOrWhiteSpace(mockResponse.Reason))
            {
                response.ReasonPhrase = mockResponse.Reason;
            }
            if (string.IsNullOrWhiteSpace(mockResponse.StatusCode))
            {
                response.StatusCode = HttpStatusCode.OK;
            }
            else
            {
                HttpStatusCode statusCode;
                int            intStatusCode;
                if (Enum.TryParse <HttpStatusCode>(mockResponse.StatusCode, true, out statusCode))
                {
                    response.StatusCode = statusCode;
                }
                else if (int.TryParse(mockResponse.StatusCode, out intStatusCode))
                {
                    var value = Enum.ToObject(typeof(HttpStatusCode), intStatusCode);
                    if (Enum.IsDefined(typeof(HttpStatusCode), value))
                    {
                        response.StatusCode = (HttpStatusCode)value;
                    }
                    else
                    {
                        response.StatusCode   = HttpStatusCode.InternalServerError;
                        response.ReasonPhrase = $"action {action} specified an invalid status code \"{mockResponse.StatusCode}\"";
                        _logProvider.Warning("action {action} specified an invalid status code {statusCode}", action, mockResponse.StatusCode);
                    }
                }
                else
                {
                    response.StatusCode = HttpStatusCode.InternalServerError;
                    _logProvider.Warning("action {action} specified an invalid status code {statusCode}", action, mockResponse.StatusCode);
                }
            }
            return(response);
        }
Пример #2
0
        private MockEngineResponse ExecuteAction(V8ScriptEngine scriptEngine, string scenarioName, MockAction action)
        {
            _logProvider.Information("executing action: {action}", action);
            var response = new MockEngineResponse()
            {
                Success = true
            };

            if (action.Before != null)
            {
                try
                {
                    scriptEngine.Execute(action.Before);
                }
                catch (Exception e)
                {
                    throw new MockEngineException(this.Name, scenarioName, $"error executing Before code: {e.Message}", e);
                }
            }
            if (action.Response != null)
            {
                response = BuildResponse(scriptEngine, scenarioName, action);
                if (response != null)
                {
                    scriptEngine.AddHostObject("response", response);
                }
            }
            if (action.After != null)
            {
                try
                {
                    scriptEngine.Execute(action.After);
                }
                catch (Exception e)
                {
                    throw new MockEngineException(this.Name, scenarioName, $"error executing After code: {e.Message}", e);
                }
            }
            if (action.Log != null)
            {
                _logProvider.Information(ApplyExpressions(scriptEngine, action.Log, false));
            }
            return(response);
        }
Пример #3
0
        public IMockEngineResponse Invoke(string scenarioName, object dynamicProperties = null)
        {
            if (scenarioName == null)
            {
                throw new ArgumentNullException(nameof(scenarioName));
            }

            _logProvider.Verbose("Invoking scenario {scenarioName}", scenarioName);

            var scenario = _scenarioManager.GetScenario(scenarioName);

            if (scenario == null)
            {
                throw new ArgumentOutOfRangeException(nameof(scenarioName), $"scenario \"{scenarioName}\" was not found.");
            }

            using (var scriptEngine = new V8ScriptEngine())
            {
                scriptEngine.AllowReflection = true;

                scriptEngine.Script["scenarioName"] = scenarioName;

                SetInputParameters(scriptEngine, dynamicProperties);

                SetGlobals(scriptEngine, scenario);

                if (scenario.Global != null && !string.IsNullOrWhiteSpace(scenario.Global.Before))
                {
                    try
                    {
                        scriptEngine.Execute(scenario.Global.Before);
                    }
                    catch (Exception e)
                    {
                        throw new MockEngineException(this.Name, scenarioName, $"error executing global Before code: {e.Message}", e);
                    }
                }

                MockEngineResponse response = null;

                foreach (var action in scenario.Actions)
                {
                    try
                    {
                        if (action.When == null)
                        {
                            response = ExecuteAction(scriptEngine, scenarioName, action);
                            break;
                        }
                        else
                        {
                            var caseResult = scriptEngine.Evaluate(action.When);
                            if (caseResult is bool)
                            {
                                if ((bool)caseResult)
                                {
                                    response = ExecuteAction(scriptEngine, scenarioName, action);
                                    break;
                                }
                            }
                            else
                            {
                                _logProvider.Warning("action: {action} case expression did not return a boolean result", action);
                            }
                        }
                    }
                    catch (MockEngineException e)
                    {
                        var message = $"action: {action} exception: {e.Message}";
                        _logProvider.Error(e, "action: {action} exception: {message}", action, e.Message);
                        throw new MockEngineException(this.Name, scenarioName, message, e);
                    }
                    catch (Exception e)
                    {
                        _logProvider.Error(e, "action: {action} exception: {message}", action, e.Message);
                        throw new MockEngineException(this.Name, scenarioName, $"action: {action} exception: {e.Message}", e);
                    }
                }
                if (scenario.Global != null && !string.IsNullOrWhiteSpace(scenario.Global.After))
                {
                    try
                    {
                        scriptEngine.Execute(scenario.Global.After);
                    }
                    catch (Exception e)
                    {
                        throw new MockEngineException(this.Name, scenarioName, $"error executing global After code: {e.Message}", e);
                    }
                }

                if (response != null)
                {
                    return(response);
                }
            }

            var reasonPhrase = $"scenario: \"{scenarioName}\" did not contain any matching actions for this request.";

            _logProvider.Error("scenario: {scenarioName} did not contain any matching actions for this request.", scenarioName);

            return(new MockEngineResponse()
            {
                ReasonPhrase = reasonPhrase
            });
        }