Пример #1
0
        public void Convert_Json_ReturnsDictionary(string jsonContentType)
        {
            dynamic jsonData = new ExpandoObject();

            jsonData.String      = "value";
            jsonData.Int         = 100;
            jsonData.Array       = new[] { "hello", "world", "!" };
            jsonData.ObjectArray = new Dictionary <string, object> {
                { "a", 1 }, { "b", 2.0 }, { "c", "Hello world!" }
            };

            dynamic httpContext = new ExpandoObject();

            httpContext.Request             = new ExpandoObject();
            httpContext.Request.ContentType = jsonContentType;
            httpContext.Request.InputStream = new MemoryStream(
                Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(jsonData)));

            var converted = HttpRequestBodyConverter.Convert(httpContext);

            Assert.That((object)converted, Is.Not.Null);
            Assert.That((object)converted, Is.TypeOf <Dictionary <string, object> >());
            Assert.That((object)converted, Has.Member(new KeyValuePair <string, object>("String", "value")));
            Assert.That((object)converted, Has.Member(new KeyValuePair <string, object>("Int", 100)));
            Assert.That((object)converted["Array"], Is.Not.Null);
            Assert.That((string[])converted["Array"].ToObject <string[]>(), Is.EquivalentTo(new[] { "hello", "world", "!" }));
            Assert.That((object)converted["ObjectArray"], Is.Not.Null);
            Assert.That((Dictionary <string, object>)converted["ObjectArray"].ToObject <Dictionary <string, object> >(),
                        Has.Member(new KeyValuePair <string, object>("b", 2.0)));
        }
Пример #2
0
        public ISentryRequest Create()
        {
            var httpRequest = _accessor?.HttpContext?.Request;

            if (httpRequest == null)
            {
                return(new SentryRequest());
            }

            var request = new SentryRequest
            {
                Url         = $"{httpRequest.Scheme}://{httpRequest.Host}{httpRequest.Path}",
                Method      = httpRequest.Method,
                QueryString = httpRequest.QueryString.ToString(),
                Headers     = httpRequest.Headers.ToDictionary(h => h.Key, h => h.Value.ToString()),
                Cookies     = httpRequest.Cookies.ToDictionary(c => c.Key, c => c.Value.ToString())
            };

            try
            {
                request.Data = HttpRequestBodyConverter.Convert(_accessor.HttpContext);
            }
            catch (Exception)
            {
                //
            }

            return(request);
        }
Пример #3
0
        public void Convert_UnknownType_ReturnsString()
        {
            dynamic httpContext = new ExpandoObject();

            httpContext.Request             = new ExpandoObject();
            httpContext.Request.ContentType = "unkown/type";
            httpContext.Request.InputStream = new MemoryStream(Encoding.UTF8.GetBytes("Hello world!"));

            object converted = HttpRequestBodyConverter.Convert(httpContext);

            Assert.That(converted, Is.EqualTo("Hello world!"));
        }
        public object GetRequestBody()
        {
            try
            {
                dynamic wrapper = new ExpandoObject();
                wrapper.Request = new DynamicHttpContextRequest(_httpContext);
                return(HttpRequestBodyConverter.Convert(wrapper));
            }
            catch (Exception exception)
            {
                SystemUtil.WriteError(exception);
            }

            return(null);
        }
Пример #5
0
        public void Convert_Form_ReturnsForm()
        {
            dynamic httpContext = new ExpandoObject();

            httpContext.Request             = new ExpandoObject();
            httpContext.Request.ContentType = "application/x-www-form-urlencoded";
            httpContext.Request.Form        = new NameValueCollection {
                { "Key", "Value" }
            };

            object converted = HttpRequestBodyConverter.Convert(httpContext);

            Assert.That(converted, Is.Not.Null);
            Assert.That(converted, Is.TypeOf <Dictionary <string, string> >());
            Assert.That(converted, Has.Member(new KeyValuePair <string, string>("Key", "Value")));
        }
Пример #6
0
        public void Convert_MultiPartForm_ReturnsForm()
        {
            dynamic httpContext = new ExpandoObject();

            httpContext.Request             = new ExpandoObject();
            httpContext.Request.ContentType = "multipart/form-data";
            httpContext.Request.Form        = new NameValueCollection {
                { "Key", "Value" }
            };

            var converted = HttpRequestBodyConverter.Convert(httpContext);

            Assert.That(converted, Is.Not.Null);
            Assert.That(converted, Is.TypeOf <Dictionary <string, string> >());
            Assert.That(converted, Has.Member(new KeyValuePair <string, string>("Key", "Value")));
        }