A class that represents the persisted form of an HTTP request.
Inheritance: HttpContextPartEntity
Exemplo n.º 1
0
        /// <summary>
        ///     Initializes a new instance of the <see cref="T:Stumps.Server.Data.HttpRequestEntityReader"/> class.
        /// </summary>
        /// <param name="serverId">The unique identifier for the server.</param>
        /// <param name="requestEntity">The request entity.</param>
        /// <param name="dataAccess">The data access provider used by the instance.</param>
        /// <exception cref="System.ArgumentNullException">
        /// <paramref name="serverId"/> is <c>null</c>.
        /// or
        /// <paramref name="requestEntity"/> is <c>null</c>.
        /// or 
        /// <paramref name="dataAccess"/> is <c>null</c>.
        /// </exception>
        public HttpRequestEntityReader(string serverId, HttpRequestEntity requestEntity, IDataAccess dataAccess)
        {
            if (string.IsNullOrWhiteSpace(serverId))
            {
                throw new ArgumentNullException("serverId");
            }

            if (requestEntity == null)
            {
                throw new ArgumentNullException("requestEntity");

            }

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

            _entity = requestEntity;

            this.Headers = new HttpHeaders();
            foreach (var pair in _entity.Headers)
            {
                this.Headers[pair.Name] = pair.Value;
            }

            _body = dataAccess.ServerReadResource(serverId, requestEntity.BodyResourceName) ?? new byte[0];
        }
Exemplo n.º 2
0
        /// <summary>
        ///     Initializes a new instance of the <see cref="HttpRequestEntityReader"/> class.
        /// </summary>
        /// <param name="serverId">The unique identifier for the server.</param>
        /// <param name="requestEntity">The request entity.</param>
        /// <param name="dataAccess">The data access provider used by the instance.</param>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="serverId"/> is <c>null</c>.
        /// or
        /// <paramref name="requestEntity"/> is <c>null</c>.
        /// or
        /// <paramref name="dataAccess"/> is <c>null</c>.
        /// </exception>
        public HttpRequestEntityReader(string serverId, HttpRequestEntity requestEntity, IDataAccess dataAccess)
        {
            if (string.IsNullOrWhiteSpace(serverId))
            {
                throw new ArgumentNullException(nameof(serverId));
            }

            dataAccess = dataAccess ?? throw new ArgumentNullException(nameof(dataAccess));

            _entity = requestEntity ?? throw new ArgumentNullException(nameof(requestEntity));

            foreach (var pair in _entity.Headers)
            {
                this.Headers[pair.Name] = pair.Value;
            }

            _body = dataAccess.ServerReadResource(serverId, requestEntity.BodyResourceName) ?? new byte[0];
        }
Exemplo n.º 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
            };
        }
Exemplo n.º 4
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
            };
        }
Exemplo n.º 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;
        }