public async void MockServerProcessorCancelTokenAfterRequest() { _mockServerProcessor = new MockServerProcessor(new AssertFactory(), new RuleMatcher(), new TemplateContext()); var scenarios = GenerateRandomScenarios(out var httpContext); var input = new MessageProcessorInput(httpContext.Request, scenarios); var Target = _mockServerProcessor; var cancelledTokenSource = new CancellationTokenSource(); Target.Start(); // fill the pipeline with tasks var fillPipeline = new Task(() => { while (true) { _ = Target.Push(input, cancelledTokenSource.Token); } }); fillPipeline.Start(); await Task.Delay(1000); cancelledTokenSource.Cancel(); var Actual = Target.PipelineIsRunning; // should not accept anymore data after it has been cancelled _ = Target.Push(input, cancelledTokenSource.Token); Assert.False(Actual); }
/// <inheritdoc /> public async Task <MockResponse> Push(MessageProcessorInput input, CancellationToken token) { var completionSource = new TaskCompletionSource <ProcessMessagePort>(); token.Register(() => cancellationTokenSource.Cancel()); if (input == null || input.ServerHttpRequest == null || input.ServerHttpRequest.Body == null || input.HeaderDictionary == null || input.QueryDictionary == null || input.Scenarios == null) { var error = "One or more of the Message Processor Inputs is null"; Log.Error("MockServerProcessor Error: {Error}", error); return(new MockResponse { Status = 400, Body = "Something went wrong" }); } var body = string.Empty; using (var reader = new StreamReader(input.ServerHttpRequest.Body)) { body = reader.ReadToEnd(); } Enum.TryParse(input.ServerHttpRequest.Method, true, out HttpMethod verb); var port = new ProcessMessagePort() { Scenarios = input.Scenarios, Path = input.ServerHttpRequest.Path, Verb = verb, Query = input.QueryDictionary, Headers = input.HeaderDictionary, Body = body }; var envelope = new SyncEnvelope(completionSource, port, token); this.startBlock.Post(envelope); port = await completionSource.Task; if (port == null) { var error = "Pipeline port cannot be null"; Log.Error("MockServerProcessor Error: {Error}", error); return(new MockResponse { Status = StatusCodes.Status500InternalServerError, Body = error, Headers = new Dictionary <string, string>() }); } return(port.IsFaulted || port.SelectedResponse == null ? new MockResponse() : port.SelectedResponse); }
public void MockServerProcessorPushWithInvalidVerbTest() { #region TestSetup var httpContext = new DefaultHttpContext(); httpContext.Request.Method = HttpMethods.Options; var input = new MessageProcessorInput(httpContext.Request, new List <Scenario>()); #endregion var Target = this.mockServerProcessor; Target.Start(); var Actual = Target.Push(input, cancellationToken).Result; var Expected = new MockResponse(); Assert.Equal(Expected, Actual); }
public void MockServerProcessorPushWithInvalidInputNullRequestTest() { #region TestSetup var input = new MessageProcessorInput(null, new List <Scenario>()); #endregion var Target = this.mockServerProcessor; Target.Start(); var Actual = Target.Push(input, cancellationToken).Result; var Expected = new MockResponse { Status = StatusCodes.Status400BadRequest, Body = "Something went wrong", Headers = new Dictionary <string, string>() }; Assert.Equal(Expected, Actual); }
public void MockServerProcessorCancelTokenBeforeRequest() { var cancelledTokenSource = new CancellationTokenSource(); cancelledTokenSource.Cancel(); var Target = _mockServerProcessor; Target.Start(); IEnumerable <Scenario> emptyList = new List <Scenario>(); HttpRequest request = new DefaultHttpRequest(new DefaultHttpContext()); var input = new MessageProcessorInput(request, emptyList); var Actual = Target.Push(input, cancelledTokenSource.Token).Result.Status; var Expected = StatusCodes.Status500InternalServerError; Assert.Equal(Actual, Expected); }
public void MockServerProcessorPushWithValidInputNoMatchTest() { #region TestSetup var scenarios = this.fakerScenario.Generate(10); var httpContext = new DefaultHttpContext(); httpContext.Request.Path = "/diff"; httpContext.Request.Method = HttpMethods.Get; var input = new MessageProcessorInput(httpContext.Request, scenarios); #endregion var Target = this.mockServerProcessor; var Expected = new MockResponse(); Target.Start(); var Actual = Target.Push(input, cancellationToken).Result; Assert.Equal(Expected, Actual); }
public async void MockServerProcessorCancelTokenAfterRequestEnsuringInterpipelineEventsAreProcessed() { _mockServerProcessor = new MockServerProcessor(new AssertFactory(), new RuleMatcher(), new TemplateContext()); var Target = _mockServerProcessor; var pipelineCancelTokenSource = new CancellationTokenSource(); var pipelineFillerCancelTokenSource = new CancellationTokenSource(); Target.Start(); // fill the pipeline with tasks as fast as possible (so that they queue) var fillPipeline = Task <List <Task <MockResponse> > > .Factory.StartNew(() => { var itemsPushedIntoPipeline = new List <Task <MockResponse> >(); while (!pipelineFillerCancelTokenSource.Token.IsCancellationRequested) { var scenarios = GenerateRandomScenarios(out var httpContext); // must be valid JSON so it can propagate through the pipeline (and use up more CPU processing // the JSON because it will allow it to simulate real-world conditions) JObject sampleResponse = new JObject { ["test"] = "message" }; httpContext.Request.Body = GenerateStreamFromString(sampleResponse.ToString()); var input = new MessageProcessorInput(httpContext.Request, scenarios); itemsPushedIntoPipeline.Add(Target.Push(input, pipelineCancelTokenSource.Token)); } return(itemsPushedIntoPipeline); }, pipelineFillerCancelTokenSource.Token); // allow the pipeline to be sufficiently full (must be a long time to make the queue catch up) await Task.Delay(10000); pipelineFillerCancelTokenSource.Cancel(); pipelineCancelTokenSource.Cancel(); fillPipeline.Wait(); fillPipeline.Result.ForEach(x => x.Wait()); }
public void MockServerProcessorPushWithInvalidJsonFailure() { #region TestSetup var scenarios = this.fakerScenario.Generate(1); var httpContext = new DefaultHttpContext(); httpContext.Request.Path = scenarios[0].Path; httpContext.Request.Method = scenarios[0].Verb.ToString(); httpContext.Request.Body = new MemoryStream(Encoding.ASCII.GetBytes("invalid \\json")); scenarios[0].RequestMatchRules.HeaderRules.ToList().ForEach(k => httpContext.Request.Headers.Add(k.RuleValue.Key, k.RuleValue.Value)); httpContext.Request.Query = new QueryCollection(scenarios[0].RequestMatchRules.QueryRules.ToDictionary(x => x.RuleValue.Key, x => new StringValues(x.RuleValue.Value))); var input = new MessageProcessorInput(httpContext.Request, scenarios); #endregion var Target = this.mockServerProcessor; Target.Start(); var Actual = Target.Push(input, cancellationToken); Assert.NotNull(Actual.Result.Body); }
public void MockServerProcessorPushWithValidInputTestWithValidPathResponse() { #region TestSetup var scenarios = this.fakerScenario.Generate(10); //Ensures one of the scenarios request match is unique for the test scenarios[0].RequestMatchRules.HeaderRules = scenarios[0].RequestMatchRules.HeaderRules.Select(x => new KeyValuePairRule() { Type = x.Type, RuleValue = new KeyValuePair <string, string>(x.RuleValue.Key, x.RuleValue.Value + "-unique") }).ToList(); scenarios[0].RequestMatchRules.QueryRules = scenarios[0].RequestMatchRules.QueryRules.Select(x => new KeyValuePairRule() { Type = x.Type, RuleValue = new KeyValuePair <string, string>(x.RuleValue.Key, x.RuleValue.Value + "-unique") }).ToList(); scenarios[0].RequestMatchRules.UrlRules = scenarios[0].RequestMatchRules.UrlRules.Select(x => new KeyValuePairRule() { Type = x.Type, RuleValue = new KeyValuePair <string, string>(x.RuleValue.Key, x.RuleValue.Value + "-unique") }).ToList(); var httpContext = new DefaultHttpContext(); httpContext.Request.Path = scenarios[0].Path; httpContext.Request.Method = scenarios[0].Verb.ToString(); httpContext.Request.Body = new MemoryStream(Encoding.ASCII.GetBytes(scenarios[0].RequestMatchRules.BodyRules.ToList()[0].RuleValue.ToString())); scenarios[0].RequestMatchRules.HeaderRules.ToList().ForEach(k => httpContext.Request.Headers.Add(k.RuleValue.Key, k.RuleValue.Value)); httpContext.Request.Query = new QueryCollection(scenarios[0].RequestMatchRules.QueryRules.ToDictionary(x => x.RuleValue.Key, x => new StringValues(x.RuleValue.Value))); var input = new MessageProcessorInput(httpContext.Request, scenarios); #endregion var Target = this.mockServerProcessor; var Expected = scenarios[0].Response; Target.Start(); var Actual = Target.Push(input, cancellationToken).Result; Assert.Equal(Expected, Actual); }