Пример #1
0
        public void Can_send_ResponseStream_test_with_Custom_Header()
        {
            var mockResponse = new HttpResponseMock();

            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, ContentType.Html)
            {
                Headers =
                {
                    {"X-Custom","Header"}
                }
            };

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

            Assert.That(reponseWasAutoHandled, Is.True);

            var writtenString = mockResponse.GetOutputStreamAsString();
            Assert.That(writtenString, Is.EqualTo(customText));
            Assert.That(mockResponse.Headers["X-Custom"], Is.EqualTo("Header"));
        }
Пример #2
0
        public void Can_handle_null_HttpResult_StatusDescription()
        {
            var mockResponse = new HttpResponseMock();

            var httpResult = new HttpResult();
            httpResult.StatusDescription = null;

            mockResponse.WriteToResponse(httpResult, ContentType.Html);

            Assert.IsNotNull(mockResponse.StatusDescription);
        }
Пример #3
0
        public void Can_send_ResponseText_test_with_StatusDescription()
        {
            var mockResponse = new HttpResponseMock();

            var customStatus = "Custom Status Description";

            var httpResult = new HttpResult(System.Net.HttpStatusCode.Accepted, customStatus);

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

            Assert.That(reponseWasAutoHandled, Is.True);

            var statusDesc = mockResponse.StatusDescription;
            Assert.That(mockResponse.StatusCode, Is.EqualTo(System.Net.HttpStatusCode.Accepted));
            Assert.That(statusDesc, Is.EqualTo(customStatus));
        }
Пример #4
0
        public void Test_response_with_html_result()
        {
            var mockResponse = new HttpResponseMock();

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

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

            Assert.That(reponseWasAutoHandled, Is.True);

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

            var writtenString = mockResponse.GetOutputStreamAsString();
            Assert.That(writtenString, Is.EqualTo(expectedOutput));
            Assert.That(mockResponse.Headers["Location"], Is.EqualTo(url));
        }
Пример #5
0
		public void Test_response_with_CompressedResult()
		{
			EndpointHost.Config = new EndpointHostConfig(
				"ServiceName",
				new ServiceManager(GetType().Assembly));

			var assembly = typeof (CompressionTests).Assembly;
			EndpointHost.ConfigureHost(
				new TestAppHost(new Container(), assembly), "Name", new ServiceManager(assembly));

			var mockResponse = new HttpResponseMock();

			var simpleDto = new TestCompress(1, "name");

			var simpleDtoXml = DataContractSerializer.Instance.Parse(simpleDto);

			const string expectedXml = "<TestCompress xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"http://schemas.ddnglobal.com/types/\"><Id>1</Id><Name>name</Name></TestCompress>";

			Assert.That(simpleDtoXml, Is.EqualTo(expectedXml));

			var simpleDtoZip = simpleDtoXml.Deflate();

			Assert.That(simpleDtoZip.Length, Is.GreaterThan(0));

			var compressedResult = new CompressedResult(simpleDtoZip);

			var reponseWasAutoHandled = mockResponse.WriteToResponse(
				compressedResult, CompressionTypes.Deflate);

			Assert.That(reponseWasAutoHandled, Is.True);

			//var bytesToWriteToResponseStream = new byte[simpleDtoZip.Length - 4];
			//Array.Copy(simpleDtoZip, CompressedResult.Adler32ChecksumLength, bytesToWriteToResponseStream, 0, bytesToWriteToResponseStream.Length);

			var bytesToWriteToResponseStream = simpleDtoZip;

			var writtenBytes = mockResponse.GetOutputStreamAsBytes();
			Assert.That(writtenBytes, Is.EqualTo(bytesToWriteToResponseStream));
			Assert.That(mockResponse.ContentType, Is.EqualTo(MimeTypes.Xml));
			Assert.That(mockResponse.Headers[HttpHeaders.ContentEncoding], Is.EqualTo(CompressionTypes.Deflate));

			Log.Debug("Content-length: " + writtenBytes.Length);
			Log.Debug(BitConverter.ToString(writtenBytes));
		}
Пример #6
0
		public void Can_send_ResponseText_test_with_StatusDescription()
		{
            var mockRequest = new MockHttpRequest { ContentType = MimeTypes.Json };
			var mockRequestContext = new HttpRequestContext(mockRequest, null, new object());
			var mockResponse = new HttpResponseMock();

			var customStatus = "Custom Status Description";

			var httpResult = new HttpResult(System.Net.HttpStatusCode.Accepted, customStatus) {
				RequestContext = mockRequestContext
			};

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

			Assert.That(reponseWasAutoHandled, Is.True);

			var statusDesc = mockResponse.StatusDescription;
			Assert.That(mockResponse.StatusCode, Is.EqualTo((int)System.Net.HttpStatusCode.Accepted));
			Assert.That(statusDesc, Is.EqualTo(customStatus));
		}
Пример #7
0
        public void Can_send_ResponseText_test_with_Custom_Header()
        {
            var mockResponse = new HttpResponseMock();

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

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

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

            Assert.That(reponseWasAutoHandled, Is.True);

            var writtenString = mockResponse.GetOutputStreamAsString();
            Assert.That(writtenString, Is.EqualTo(customText));
            Assert.That(mockResponse.Headers["X-Custom"], Is.EqualTo("Header"));
        }
        public void 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 HttpRequestMock();
            var mockResponse = new HttpResponseMock();
            mockRequest.Headers.Add("Range", "bytes=6-8");

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

            bool reponseWasAutoHandled = mockResponse.WriteToResponse(mockRequest, httpResult);
            Assert.That(reponseWasAutoHandled, Is.True);

            string writtenString = mockResponse.GetOutputStreamAsString();
            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 void Can_seek_from_middle_to_middle()
        {
            var mockRequest = new HttpRequestMock();
            mockRequest.Headers.Add("Range", "bytes=3-5");
            var mockResponse = new HttpResponseMock();

            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 = mockResponse.WriteToResponse(mockRequest, httpResult);
            Assert.That(reponseWasAutoHandled, Is.True);

            string writtenString = mockResponse.GetOutputStreamAsString();
            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 void Can_respond_to_non_range_requests_with_200_OK_response()
        {
            var mockRequest = new HttpRequestMock();
            var mockResponse = new HttpResponseMock();

            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 = mockResponse.WriteToResponse(mockRequest, httpResult);
            Assert.That(reponseWasAutoHandled, Is.True);

            string writtenString = mockResponse.GetOutputStreamAsString();
            Assert.That(writtenString, Is.EqualTo(customText));

            Assert.That(mockResponse.Headers["Content-Range"], Is.Null);
            Assert.That(mockResponse.Headers["Accept-Ranges"], Is.EqualTo("bytes"));
            Assert.That(mockResponse.StatusCode, Is.EqualTo(200));
        }