/// <summary> /// Returns a string value depending on the state of a <see cref="StumpsServerInstance"/>. /// </summary> /// <param name="serverInstance">The instance of the Stumps server.</param> /// <param name="running">The message to use if the proxy environment is running.</param> /// <param name="stopped">The message to use if the proxy environment is stopped.</param> /// <param name="recording">The message to use if the proxy environment is recording.</param> /// <returns>A <see cref="String"/> representing the state of the server.</returns> /// <exception cref="ArgumentNullException"><paramref name="serverInstance"/> is <c>null</c>.</exception> public static string StateValue(StumpsServerInstance serverInstance, string running, string stopped, string recording) { serverInstance = serverInstance ?? throw new ArgumentNullException(nameof(serverInstance)); var value = stopped; if (serverInstance.IsRunning && serverInstance.RecordTraffic) { value = recording; } else if (serverInstance.IsRunning && !serverInstance.RecordTraffic) { value = running; } return(value); }
/// <summary> /// Creates a Stump contract from an existing Stump. /// </summary> /// <param name="model">The <see cref="T:Stumps.Web.Models.StumpModel"/> used to create the contract.</param> /// <param name="server">The <see cref="T:Stumps.Server.StumpsServerInstance" /> that contains the Stump.</param> /// <returns>A <see cref="T:Stumps.Server.StumpContract" /> created from an existing Stump.</returns> private StumpContract CreateContractFromStump(StumpModel model, StumpsServerInstance server) { var originalContract = server.FindStump(model.StumpId); var contract = new StumpContract { OriginalRequest = new RecordedRequest(originalContract.OriginalRequest, ContentDecoderHandling.DecodeNotRequired), OriginalResponse = new RecordedResponse(originalContract.OriginalResponse, ContentDecoderHandling.DecodeNotRequired), Response = new RecordedResponse(originalContract.Response, ContentDecoderHandling.DecodeNotRequired), StumpId = model.StumpId, StumpName = model.Name, StumpCategory = "Uncategorized" }; if (model.RequestUrlMatch) { contract.Rules.Add(new RuleContract(new UrlRule(model.RequestUrl))); } if (model.RequestHttpMethodMatch) { contract.Rules.Add(new RuleContract(new HttpMethodRule(model.RequestHttpMethod))); } foreach (var h in model.RequestHeaderMatch) { contract.Rules.Add(new RuleContract(new HeaderRule(h.Name, h.Value))); } switch (model.RequestBodyMatch) { case BodyMatch.ContainsText: contract.Rules.Add(new RuleContract(new BodyContentRule(model.RequestBodyMatchValues))); break; case BodyMatch.ExactMatch: contract.Rules.Add( new RuleContract(new BodyMatchRule(contract.OriginalRequest.BodyLength, contract.OriginalRequest.BodyMd5Hash))); break; case BodyMatch.IsBlank: contract.Rules.Add(new RuleContract(new BodyLengthRule(0, 0))); break; case BodyMatch.IsNotBlank: contract.Rules.Add(new RuleContract(new BodyLengthRule(1, int.MaxValue))); break; } switch (model.ResponseBodySource) { case BodySource.Modified: contract.Response.ClearBody(); contract.Response.AppendToBody(Encoding.UTF8.GetBytes(model.ResponseBodyModification)); break; case BodySource.EmptyBody: contract.Response.ClearBody(); break; case BodySource.Origin: contract.Response.ClearBody(); contract.Response.AppendToBody(originalContract.Response.GetBody()); break; } CopyHeaderModelToDictionary(model.ResponseHeaders, contract.Response.Headers); contract.Response.StatusCode = model.ResponseStatusCode; contract.Response.StatusDescription = model.ResponseStatusDescription; contract.Response.ExamineBody(); return(contract); }