private static string DeriveContentType(HttpResponseContext httpResponseContext, RpcHttp rpcHttp)
 {
     return(httpResponseContext.ContentType ??
            (rpcHttp.Body.DataCase == TypedData.DataOneofCase.Json
                             ? ApplicationJsonMediaType
                             : TextPlainMediaType));
 }
        public void TestObjectToTypedDataRpcHttpContentTypeInHeader()
        {
            var data = "<html></html>";

            var input = new HttpResponseContext
            {
                Body    = data,
                Headers = new Hashtable {
                    { "content-type", "text/html" }
                }
            };
            var expected = new TypedData
            {
                Http = new RpcHttp
                {
                    StatusCode = "200",
                    Body       = new TypedData {
                        String = data
                    },
                    Headers = { { "content-type", "text/html" } }
                }
            };

            Assert.Equal(expected, input.ToTypedData());
        }
 private static string DeriveContentType(HttpResponseContext httpResponseContext, RpcHttp rpcHttp)
 {
     return(httpResponseContext.ContentType ??
            (rpcHttp.Body.DataCase == TypedData.DataOneofCase.Json
                             ? "application/json"
                             : "text/plain"));
 }
        private static RpcHttp ToRpcHttp(this HttpResponseContext httpResponseContext)
        {
            var rpcHttp = new RpcHttp
            {
                StatusCode = httpResponseContext.StatusCode.ToString("d")
            };

            if (httpResponseContext.Body != null)
            {
                rpcHttp.Body = httpResponseContext.Body.ToTypedData();
            }

            rpcHttp.EnableContentNegotiation = httpResponseContext.EnableContentNegotiation;

            // Add all the headers. ContentType is separated for convenience
            if (httpResponseContext.Headers != null)
            {
                foreach (DictionaryEntry item in httpResponseContext.Headers)
                {
                    rpcHttp.Headers.Add(item.Key.ToString(), item.Value.ToString());
                }
            }

            // Allow the user to set content-type in the Headers
            if (!rpcHttp.Headers.ContainsKey(ContentTypeHeaderKey))
            {
                rpcHttp.Headers.Add(ContentTypeHeaderKey, DeriveContentType(httpResponseContext, rpcHttp));
            }

            return(rpcHttp);
        }
        private static RpcHttp ToRpcHttp(this HttpResponseContext httpResponseContext, PowerShellManager psHelper)
        {
            var rpcHttp = new RpcHttp
            {
                StatusCode = httpResponseContext.StatusCode.ToString("d")
            };

            if (httpResponseContext.Body != null)
            {
                rpcHttp.Body = httpResponseContext.Body.ToTypedData(psHelper);
            }

            // Add all the headers. ContentType is separated for convenience
            foreach (var item in httpResponseContext.Headers)
            {
                rpcHttp.Headers.Add(item.Key, item.Value);
            }

            // Allow the user to set content-type in the Headers
            if (!rpcHttp.Headers.ContainsKey("content-type"))
            {
                rpcHttp.Headers.Add("content-type", httpResponseContext.ContentType);
            }

            return(rpcHttp);
        }
 public RequestControllerTest()
 {
     _controller = new MyController();
     _request    = new HttpTestRequest {
         HttpVersion = "HTTP/1.1"
     };
     _stream   = new MyStream();
     _context  = new HttpResponseContext();
     _response = _request.CreateResponse(_context);
 }
Ejemplo n.º 7
0
 public FileModuleTest()
 {
     _request = new HttpTestRequest {
         HttpVersion = "HTTP/1.1"
     };
     _context  = new HttpResponseContext();
     _response = _request.CreateResponse(_context);
     _module   = new FileModule("/files/", Environment.CurrentDirectory);
     _module.MimeTypes.Add("txt", "text/plain");
 }
        public void TestObjectToTypedData_WhenBodyIsJsonAndNoContentType_AddsApplicationJsonContentTypeHeader()
        {
            var input = new HttpResponseContext
            {
                Body = new TypedData {
                    Json = "{\"Hello\":\"World\"}"
                }
            };

            Assert.Equal("application/json", input.ToTypedData().Http.Headers["content-type"]);
        }
        public void TestObjectToTypedData_WhenBodyIsJsonAndContentTypeIsSpecified_AddsSpecifiedContentTypeHeader()
        {
            var input = new HttpResponseContext
            {
                Body = new TypedData {
                    Json = "{\"Hello\":\"World\"}"
                },
                ContentType = "text/plain"
            };

            Assert.Equal("text/plain", input.ToTypedData().Http.Headers["content-type"]);
        }
        public void TestObjectToTypedData_WhenBodyIsNotJsonAndNoContentType_AddsTextPlainContentTypeHeader()
        {
            var input = new HttpResponseContext
            {
                // Even though the content looks like Json,
                // the intent is to respond with a string.
                Body = new TypedData {
                    String = "{\"Hello\":\"World\"}"
                }
            };

            Assert.Equal("text/plain", input.ToTypedData().Http.Headers["content-type"]);
        }
        public void TestObjectToTypedData_WhenBodyIsNull(string contentType, HttpStatusCode statusCode)
        {
            var input = new HttpResponseContext
            {
                Body        = null,
                ContentType = contentType,
                StatusCode  = statusCode,
            };

            var result = input.ToTypedData().Http;

            Assert.Equal(string.Empty, result.Body.String);
            var expectedContentType = contentType ?? "text/plain";

            Assert.Equal(expectedContentType, result.Headers["content-type"]);
        }
        public void TestObjectToTypedDataRpcHttpBasic()
        {
            var data = "Hello World";

            var input = new HttpResponseContext
            {
                Body = data
            };
            var expected = new TypedData
            {
                Http = new RpcHttp
                {
                    StatusCode = "200",
                    Body       = new TypedData {
                        String = data
                    },
                    Headers = { { "content-type", "text/plain" } }
                }
            };

            Assert.Equal(expected, input.ToTypedData());
        }
Ejemplo n.º 13
0
 public HttpTransportException(HttpResponseContext response)
     : base(response.GetBodyAsString(ValueFactory.Create("UTF-8")).AsString())
 {
     Data ["StatusCode"] = response.StatusCode;
 }