A class that represents the persisted form for a T:Stumps.Stump.
Пример #1
0
        /// <summary>
        ///     Creates a Stump contract from a Stump data entity.
        /// </summary>
        /// <param name="serverId">The unique identifier for the server.</param>
        /// <param name="entity">The <see cref="T:Stumps.Server.Data.StumpEntity"/> used to create the contract.</param>
        /// <param name="dataAccess">The data access provider used by the instance.</param>
        /// <returns>
        ///     A <see cref="T:Stumps.Server.StumpContract"/> created from the specified <paramref name="entity"/>.
        /// </returns>
        public static StumpContract CreateContractFromEntity(string serverId, StumpEntity entity, IDataAccess dataAccess)
        {
            var contract = new StumpContract
            {
                OriginalRequest = new RecordedRequest(new HttpRequestEntityReader(serverId, entity.OriginalRequest, dataAccess), ContentDecoderHandling.DecodeNotRequired),
                OriginalResponse = new RecordedResponse(new HttpResponseEntityReader(serverId, entity.OriginalResponse, dataAccess), ContentDecoderHandling.DecodeNotRequired),
                Response = new RecordedResponse(new HttpResponseEntityReader(serverId, entity.Response, dataAccess), ContentDecoderHandling.DecodeNotRequired),
                ResponseDelay = entity.ResponseDelay,
                Rules = new RuleContractCollection(),
                StumpCategory = entity.StumpName,
                StumpId = entity.StumpId,
                StumpName = entity.StumpName,
                TerminateConnection = entity.TerminateConnection
            };

            foreach (var ruleEntity in entity.Rules)
            {
                var rule = new RuleContract
                {
                    RuleName = ruleEntity.RuleName
                };

                foreach (var value in ruleEntity.Settings)
                {
                    var setting = new RuleSetting
                    {
                        Name = value.Name,
                        Value = value.Value
                    };
                    rule.AppendRuleSetting(setting);
                }

                contract.Rules.Add(rule);
            }

            return contract;
        }
Пример #2
0
        public DataAccessTests()
        {
            _sampleProxyServer = new ServerEntity
            {
                AutoStart            = false,
                RemoteServerHostName = SampleHostName,
                Port     = 500,
                ServerId = "ABCD",
                UseSsl   = false
            };

            var originalRequest = new HttpRequestEntity()
            {
                HttpMethod       = "GET",
                BodyResourceName = null,
                Headers          = new List <NameValuePairEntity>(),
                ProtocolVersion  = "1.1",
                RawUrl           = "/"
            };

            var originalResponse = new HttpResponseEntity()
            {
                BodyResourceName = null,
                Headers          = new List <NameValuePairEntity>()
                {
                    new NameValuePairEntity
                    {
                        Name  = "Content-Type",
                        Value = "text/plain"
                    }
                },
                RedirectAddress   = null,
                StatusCode        = 200,
                StatusDescription = "OK"
            };

            var response = new HttpResponseEntity()
            {
                BodyResourceName = null,
                Headers          = new List <NameValuePairEntity>()
                {
                    new NameValuePairEntity
                    {
                        Name  = "Content-Type",
                        Value = "text/plain"
                    }
                },
                RedirectAddress   = null,
                StatusCode        = 200,
                StatusDescription = "OK"
            };

            _sampleStump = new StumpEntity
            {
                OriginalRequest  = originalRequest,
                OriginalResponse = originalResponse,
                Response         = response,
                Rules            = new List <RuleEntity>(),
                StumpCategory    = "Uncategorized",
                StumpId          = "ABCD",
                StumpName        = "MyStump"
            };

            _sampleBytes = new byte[]
            {
                1, 2, 3
            };
        }
Пример #3
0
        public DataAccessTests()
        {
            _sampleProxyServer = new ServerEntity
            {
                AutoStart = false,
                RemoteServerHostName = SampleHostName,
                Port = 500,
                ServerId = "ABCD",
                UseSsl = false
            };

            var originalRequest = new HttpRequestEntity()
            {
                HttpMethod = "GET",
                BodyResourceName = null,
                Headers = new List<NameValuePairEntity>(),
                ProtocolVersion = "1.1",
                RawUrl = "/"
            };

            var originalResponse = new HttpResponseEntity()
            {
                BodyResourceName = null,
                Headers = new List<NameValuePairEntity>()
                {
                    new NameValuePairEntity
                    {
                        Name = "Content-Type",
                        Value = "text/plain"
                    }
                },
                RedirectAddress = null,
                StatusCode = 200,
                StatusDescription = "OK"
            };

            var response = new HttpResponseEntity()
            {
                BodyResourceName = null,
                Headers = new List<NameValuePairEntity>()
                {
                    new NameValuePairEntity
                    {
                        Name = "Content-Type",
                        Value = "text/plain"
                    }
                },
                RedirectAddress = null,
                StatusCode = 200,
                StatusDescription = "OK"
            };

            _sampleStump = new StumpEntity
            {
                OriginalRequest = originalRequest,
                OriginalResponse = originalResponse,
                Response = response,
                Rules = new List<RuleEntity>(),
                StumpCategory = "Uncategorized",
                StumpId = "ABCD",
                StumpName = "MyStump"
            };

            _sampleBytes = new byte[]
            {
                1, 2, 3
            };
        }
Пример #4
0
        /// <summary>
        ///     Creates a new <see cref="T:Stumps.Server.Data.StumpEntity" /> for an existing Stumps server.
        /// </summary>
        /// <param name="serverId">The unique identifier for the Stumps server.</param>
        /// <param name="entity">The <see cref="T:Stumps.Server.Data.StumpEntity" /> to persist.</param>
        /// <param name="originalRequestBody">The array of bytes representing the original request's HTTP body.</param>
        /// <param name="originalResponseBody">The array of bytes representing the original response's HTTP body.</param>
        /// <param name="responseBody">The array of bytes returned as the HTTP body in response to the stump.</param>
        /// <returns>
        ///     The created <see cref="T:Stumps.Server.Data.StumpEntity" /> object.
        /// </returns>
        /// <exception cref="System.ArgumentNullException">
        /// <paramref name="serverId"/> is <c>null</c>.
        /// or
        /// <paramref name="entity"/> is <c>null</c>.
        /// </exception>
        public StumpEntity StumpCreate(string serverId, StumpEntity entity, byte[] originalRequestBody, byte[] originalResponseBody, byte[] responseBody)
        {
            if (string.IsNullOrWhiteSpace(serverId))
            {
                throw new ArgumentNullException("serverId");
            }

            if (entity == null)
            {
                throw new ArgumentNullException("entity");
            }

            var stumpsPath = Path.Combine(_storagePath, serverId, DataAccess.StumpsPathName);

            var stumpFileName = Path.Combine(stumpsPath, entity.StumpId + DataAccess.StumpFileExtension);
            var originalRequestFileName = entity.StumpId + DataAccess.OriginalRequestBodyFileExtension;
            var originalResponseFileName = entity.StumpId + DataAccess.OriginalResponseBodyFileExtension;
            var responseFileName = entity.StumpId + DataAccess.ResponseBodyFileExtension;

            if (originalRequestBody != null && originalRequestBody.Length > 0)
            {
                entity.OriginalRequest.BodyResourceName = originalRequestFileName;

                var file = Path.Combine(stumpsPath, originalRequestFileName);
                File.WriteAllBytes(file, originalRequestBody);
            }

            if (originalResponseBody != null && originalResponseBody.Length > 0)
            {
                entity.OriginalResponse.BodyResourceName = originalResponseFileName;

                var file = Path.Combine(stumpsPath, originalResponseFileName);
                File.WriteAllBytes(file, originalResponseBody);
            }

            if (responseBody != null && responseBody.Length > 0)
            {
                entity.Response.BodyResourceName = responseFileName;

                var file = Path.Combine(stumpsPath, responseFileName);
                File.WriteAllBytes(file, responseBody);
            }

            JsonUtility.SerializeToFile(entity, stumpFileName);

            return entity;
        }
Пример #5
0
        /// <summary>
        ///     Creates a Stump data entity from a Stump contract.
        /// </summary>
        /// <param name="contract">The <see cref="T:Stumps.Server.StumpContract"/> used to create the entity.</param>
        /// <returns>
        ///     A <see cref="T:Stumps.Server.Data.StumpEntity"/> created from the specified <paramref name="contract"/>.
        /// </returns>
        public static StumpEntity CreateEntityFromContract(StumpContract contract)
        {
            var originalRequest = new HttpRequestEntity
            {
                BodyResourceName = string.Empty,
                Headers = CreateNameValuePairFromHeaders(contract.OriginalRequest.Headers),
                HttpMethod = contract.OriginalRequest.HttpMethod,
                LocalEndPoint = contract.OriginalRequest.LocalEndPoint.ToString(),
                ProtocolVersion = contract.OriginalRequest.ProtocolVersion,
                RawUrl = contract.OriginalRequest.RawUrl,
                RemoteEndPoint = contract.OriginalRequest.RemoteEndPoint.ToString()
            };

            var originalResponse = new HttpResponseEntity
            {
                BodyResourceName = string.Empty,
                Headers = CreateNameValuePairFromHeaders(contract.OriginalResponse.Headers),
                RedirectAddress = contract.OriginalResponse.RedirectAddress,
                StatusCode = contract.OriginalResponse.StatusCode,
                StatusDescription = contract.OriginalResponse.StatusDescription
            };

            var response = new HttpResponseEntity
            {
                BodyResourceName = string.Empty,
                Headers = CreateNameValuePairFromHeaders(contract.Response.Headers),
                RedirectAddress = contract.Response.RedirectAddress,
                StatusCode = contract.Response.StatusCode,
                StatusDescription = contract.Response.StatusDescription
            };

            var entity = new StumpEntity
            {
                OriginalRequest = originalRequest,
                OriginalResponse = originalResponse,
                Response = response,
                ResponseDelay = contract.ResponseDelay,
                Rules = new List<RuleEntity>(),
                StumpCategory = contract.StumpCategory,
                StumpId = contract.StumpId,
                StumpName = contract.StumpName,
                TerminateConnection = contract.TerminateConnection
            };

            foreach (var rule in contract.Rules)
            {
                var ruleEntity = new RuleEntity
                {
                    RuleName = rule.RuleName,
                    Settings = new List<NameValuePairEntity>()
                };

                var settings = rule.GetRuleSettings();
                foreach (var setting in settings)
                {
                    ruleEntity.Settings.Add(
                        new NameValuePairEntity
                        {
                            Name = setting.Name,
                            Value = setting.Value
                        });
                }

                entity.Rules.Add(ruleEntity);

            }

            return entity;
        }