public async Task Can_seek_from_middle_to_middle()
        {
            var mockRequest = new MockHttpRequest();

            mockRequest.Headers.Add("Range", "bytes=3-5");
            var mockResponse = new MockHttpResponse(mockRequest);

            string customText = "1234567890";

            byte[] customTextBytes = customText.ToUtf8Bytes();
            var    ms = new MemoryStream();

            ms.Write(customTextBytes, 0, customTextBytes.Length);


            var httpResult = new HttpResult(ms, "audio/mpeg");

            bool reponseWasAutoHandled = await mockResponse.WriteToResponse(mockRequest, httpResult);

            Assert.That(reponseWasAutoHandled, Is.True);

            string writtenString = mockResponse.ReadAsString();

            Assert.That(writtenString, Is.EqualTo("456"));

            Assert.That(mockResponse.Headers["Content-Range"], Is.EqualTo("bytes 3-5/10"));
            Assert.That(mockResponse.Headers["Content-Length"], Is.EqualTo(writtenString.Length.ToString()));
            Assert.That(mockResponse.Headers["Accept-Ranges"], Is.EqualTo("bytes"));
            Assert.That(mockResponse.StatusCode, Is.EqualTo(206));
        }
        public async Task Can_use_fileStream()
        {
            byte[] fileBytes = uploadedTextFile.ReadFully();
            string fileText  = Encoding.ASCII.GetString(fileBytes);

            "File content size {0}".Print(fileBytes.Length);
            "File content is {0}".Print(fileText);

            var mockRequest  = new MockHttpRequest();
            var mockResponse = new MockHttpResponse(mockRequest);

            mockRequest.Headers.Add("Range", "bytes=6-8");

            var httpResult = new HttpResult(uploadedTextFile, "audio/mpeg");

            bool reponseWasAutoHandled = await mockResponse.WriteToResponse(mockRequest, httpResult);

            Assert.That(reponseWasAutoHandled, Is.True);

            string writtenString = mockResponse.ReadAsString();

            Assert.That(writtenString, Is.EqualTo(fileText.Substring(6, 3)));

            Assert.That(mockResponse.Headers["Content-Range"], Is.EqualTo("bytes 6-8/33"));
            Assert.That(mockResponse.Headers["Content-Length"], Is.EqualTo(writtenString.Length.ToString()));
            Assert.That(mockResponse.Headers["Accept-Ranges"], Is.EqualTo("bytes"));
            Assert.That(mockResponse.StatusCode, Is.EqualTo(206));
        }
        public async Task Can_respond_to_non_range_requests_with_200_OK_response()
        {
            var mockRequest  = new MockHttpRequest();
            var mockResponse = new MockHttpResponse(mockRequest);

            string customText = "1234567890";

            byte[] customTextBytes = customText.ToUtf8Bytes();
            var    ms = new MemoryStream();

            ms.Write(customTextBytes, 0, customTextBytes.Length);

            var httpResult = new HttpResult(ms, "audio/mpeg");

            bool responseWasAutoHandled = await mockResponse.WriteToResponse(mockRequest, httpResult);

            Assert.That(responseWasAutoHandled, Is.True);

            string writtenString = mockResponse.ReadAsString();

            Assert.That(writtenString, Is.EqualTo(customText));

            Assert.That(mockResponse.Headers.ContainsKey("Content-Range"), Is.False);
            Assert.That(mockResponse.Headers["Accept-Ranges"], Is.EqualTo("bytes"));
            Assert.That(mockResponse.StatusCode, Is.EqualTo(200));
        }
        public async Task Can_seek_from_beginning_to_further_than_end()
        {
            // Not sure if this would ever occur in real streaming scenarios, but it does occur
            // when some crawlers use range headers to specify a max size to return.
            // e.g. Facebook crawler always sends range header of 'bytes=0-524287'.

            var mockRequest  = new MockHttpRequest();
            var mockResponse = new MockHttpResponse(mockRequest);

            mockRequest.Headers[HttpHeaders.Range] = "bytes=0-524287";

            string customText = "1234567890";

            byte[] customTextBytes = customText.ToUtf8Bytes();
            var    ms = new MemoryStream();

            ms.Write(customTextBytes, 0, customTextBytes.Length);

            var httpResult = new HttpResult(ms, "audio/mpeg");

            bool reponseWasAutoHandled = await mockResponse.WriteToResponse(mockRequest, httpResult);

            Assert.That(reponseWasAutoHandled, Is.True);

            string writtenString = mockResponse.ReadAsString();

            Assert.That(writtenString, Is.EqualTo(customText));

            Assert.That(mockResponse.Headers["Content-Range"], Is.EqualTo("bytes 0-9/10"));
            Assert.That(mockResponse.Headers["Content-Length"], Is.EqualTo(writtenString.Length.ToString()));
            Assert.That(mockResponse.Headers["Accept-Ranges"], Is.EqualTo("bytes"));
            Assert.That(mockResponse.StatusCode, Is.EqualTo(206));
        }
        public void Can_send_ResponseStream_test_with_Custom_Header()
        {
            var mockResponse = new MockHttpResponse();

            var customText      = "<h1>Custom Stream</h1>";
            var customTextBytes = customText.ToUtf8Bytes();
            var ms = new MemoryStream();

            ms.Write(customTextBytes, 0, customTextBytes.Length);


            var httpResult = new HttpResult(ms, MimeTypes.Html)
            {
                Headers =
                {
                    { "X-Custom", "Header" }
                }
            };

            var responseWasAutoHandled = mockResponse.WriteToResponse(httpResult, MimeTypes.Html);

            Assert.That(responseWasAutoHandled.Result, Is.True);

            var writtenString = mockResponse.ReadAsString();

            Assert.That(writtenString, Is.EqualTo(customText));
            Assert.That(mockResponse.Headers["X-Custom"], Is.EqualTo("Header"));
        }
        private string ExecuteContentPage(string path)
        {
            var mockReq = new MockHttpRequest
            {
                OperationName = "Razor_PageResolver",
                PathInfo      = path,
            };
            var mockRes = new MockHttpResponse(mockReq);

            RazorFormat.ProcessContentPageRequest(mockReq, mockRes);
            return(mockRes.ReadAsString());
        }
        private string ExecuteViewPage <TRequest>() where TRequest : new()
        {
            var responseDtoType = typeof(TRequest).Assembly.GetType(typeof(TRequest).FullName + "Response");
            var responseDto     = Activator.CreateInstance(responseDtoType);
            var mockReq         = new MockHttpRequest {
                OperationName = typeof(TRequest).Name, Dto = new TRequest()
            };
            var mockRes = new MockHttpResponse {
                Dto = responseDto
            };

            RazorFormat.ProcessRequest(mockReq, mockRes, responseDto);
            return(mockRes.ReadAsString());
        }
        private static string GetRazorInstanceHtml()
        {
            var razorFormat = RazorFormat.Instance;
            var mockReq     = new MockHttpRequest {
                OperationName = "RazorInstance"
            };
            var mockRes = new MockHttpResponse();
            var dto     = new RockstarsResponse {
                Results = Rockstar.SeedData.ToList()
            };

            razorFormat.ProcessRequest(mockReq, mockRes, dto);
            var html = mockRes.ReadAsString();

            return(html);
        }
Esempio n. 9
0
        public void Benchmark_Razor_vs_Markdown()
        {
            var iterations  = 10000;
            var razorFormat = RazorFormat.Instance;
            var markdownFmt = MarkdownFormat.Instance;
            var dto         = new RockstarsResponse {
                Results = Rockstar.SeedData.ToList()
            };

            "Warm up MVC Razor...".Print();
            var mockReq = new MockHttpRequest {
                OperationName = "RockstarsRazor"
            };
            var mockRes = new MockHttpResponse();

            razorFormat.ProcessRequest(mockReq, mockRes, dto);
            mockRes.ReadAsString().Print();

            "Warm up Markdown Razor...".Print();
            mockReq = new MockHttpRequest {
                OperationName = "RockstarsMark"
            };
            mockRes = new MockHttpResponse();
            markdownFmt.ProcessRequest(mockReq, mockRes, dto);
            mockRes.ReadAsString().Print();

            "\n\nRunning for {0} times...".Fmt(iterations).Print();
            CompareRuns(iterations,
                        "MVC Razor", () => {
                mockReq = new MockHttpRequest {
                    OperationName = "RockstarsRazor"
                };
                mockRes = new MockHttpResponse();
                razorFormat.ProcessRequest(mockReq, mockRes, dto);
            },
                        "Markdown Razor", () => {
                mockReq = new MockHttpRequest {
                    OperationName = "RockstarsMark"
                };
                mockRes = new MockHttpResponse();
                markdownFmt.ProcessRequest(mockReq, mockRes, dto);
            });
        }
        public void Test_response_with_html_result()
        {
            var mockResponse = new MockHttpResponse();

            const string url        = "http://www.servicestack.net";
            var          htmlResult = Html.RedirectTo(url);

            var reponseWasAutoHandled = mockResponse.WriteToResponse(htmlResult, "text/xml");

            Assert.That(reponseWasAutoHandled.Result, Is.True);

            var expectedOutput = string.Format(
                "<html><head><meta http-equiv=\"refresh\" content=\"0;url={0}\"></head></html>", url);

            var writtenString = mockResponse.ReadAsString();

            Assert.That(writtenString, Is.EqualTo(expectedOutput));
            Assert.That(mockResponse.Headers["Location"], Is.EqualTo(url));
        }
Esempio n. 11
0
        public void Can_change_serialization_options()
        {
            var mockResponse = new MockHttpResponse();

            var dto = new Poco();

            Assert.That(dto.ToJson(), Is.EqualTo("{}"));

            var httpResult = new HttpResult(dto)
            {
                ResultScope = () => JsConfig.With(includeNullValues: true)
            };

            var reponseWasAutoHandled = mockResponse.WriteToResponse(httpResult, MimeTypes.Html);

            Assert.That(reponseWasAutoHandled.Result, Is.True);

            Assert.That(mockResponse.ReadAsString(), Is.EqualTo("{\"Text\":null}").Or.EqualTo("{\"text\":null}"));
        }
        public void Can_send_ResponseText_test_with_Custom_Header()
        {
            var mockResponse = new MockHttpResponse();

            var customText = "<h1>Custom Text</h1>";

            var httpResult = new HttpResult(customText, MimeTypes.Html)
            {
                Headers =
                {
                    { "X-Custom", "Header" }
                }
            };

            var responseWasAutoHandled = mockResponse.WriteToResponse(httpResult, MimeTypes.Html);

            Assert.That(responseWasAutoHandled.Result, Is.True);

            var writtenString = mockResponse.ReadAsString();

            Assert.That(writtenString, Is.EqualTo(customText));
            Assert.That(mockResponse.Headers["X-Custom"], Is.EqualTo("Header"));
        }