예제 #1
0
        public void Send_Error_Data()
        {
            var zSocket = new MockZSocket();
            var guid    = Guid.NewGuid();

            var mockFileSearch = new MockFileProcessor()
                                 .StubExists(true)
                                 .StubGetFileStream(null)
                                 .StubFileSize(2);

            var properties = new ServerProperties(@"c:/",
                                                  5555, new ServerTime(),
                                                  new MockPrinter(),
                                                  new Readers
            {
                DirectoryProcess = new MockDirectoryProcessor(),
                FileProcess      = mockFileSearch
            });
            var inlinePngService = new InlinePngService();

            var statusCode = inlinePngService
                             .ProcessRequest("GET /" + guid + ".png HTTP/1.1",
                                             new HttpResponse(zSocket),
                                             properties);

            Assert.Equal("200 OK", statusCode);
        }
예제 #2
0
        public void Get_Directory_Listing(string getRequest)
        {
            var mockRead = new MockDirectoryProcessor()
                           .StubGetDirectories(new[] { "Home/dir 1", "Home/dir2" })
                           .StubGetFiles(new[] { "Home/file 1", "Home/file2", "Home/file3" });
            var zSocket = new MockZSocket()
                          .StubSentToReturn(10)
                          .StubReceive(getRequest)
                          .StubConnect(true);

            zSocket = zSocket.StubAcceptObject(zSocket);
            var properties = new ServerProperties(@"Home", 8080,
                                                  new ServerTime(),
                                                  new MockPrinter(),
                                                  new Readers
            {
                DirectoryProcess = mockRead,
                FileProcess      = new MockFileProcessor()
            });
            var directoryServer = new DirectoryService();
            var statueCode      = directoryServer
                                  .ProcessRequest(getRequest,
                                                  new HttpResponse(zSocket),
                                                  properties);

            var correctOutput = new StringBuilder();

            correctOutput.Append(@"<!DOCTYPE html>");
            correctOutput.Append(@"<html>");
            correctOutput.Append(@"<head><title>Vatic Server Directory Listing</title></head>");
            correctOutput.Append(@"<body>");
            correctOutput.Append(@"<br><a href=""http://*****:*****@"<br><a href=""http://*****:*****@"<br><a href=""http://*****:*****@"<br><a href=""http://*****:*****@"<br><a href=""http://*****:*****@"</body>");
            correctOutput.Append(@"</html>");

            zSocket.VerifySend(GetByte("HTTP/1.1 200 OK\r\n"),
                               GetByteCount("HTTP/1.1 200 OK\r\n"));
            zSocket.VerifySend(GetByte("Cache-Control: no-cache\r\n"),
                               GetByteCount("Cache-Control: no-cache\r\n"));
            zSocket.VerifySend(GetByte("Content-Type: text/html\r\n"),
                               GetByteCount("Content-Type: text/html\r\n"));
            zSocket.VerifySend(GetByte("Content-Length: "
                                       + GetByteCount(correctOutput.ToString())
                                       + "\r\n\r\n"),
                               GetByteCount("Content-Length: "
                                            + GetByteCount(correctOutput.ToString())
                                            + "\r\n\r\n"));

            zSocket.VerifySend(GetByte(correctOutput.ToString()),
                               GetByteCount(correctOutput.ToString()));
        }
예제 #3
0
        public void Send_Data()
        {
            var zSocket = new MockZSocket();
            var guid    = Guid.NewGuid();
            var data    = new byte[1024];

            data[0] = 0;
            data[1] = 1;
            var writeStream = File.Open("c:/" + guid + ".txt",
                                        FileMode.OpenOrCreate, FileAccess.ReadWrite,
                                        FileShare.Read);

            writeStream.Write(data, 0, 2);
            writeStream.Close();

            var readStream = File.Open("c:/" + guid + ".txt",
                                       FileMode.Open, FileAccess.Read,
                                       FileShare.Read);
            var mockFileSearch = new MockFileProcessor()
                                 .StubExists(true)
                                 .StubGetFileStream(readStream)
                                 .StubFileSize(2);
            var properties = new ServerProperties(@"c:/",
                                                  5555, new ServerTime(),
                                                  new MockPrinter(),
                                                  new Readers
            {
                DirectoryProcess = new MockDirectoryProcessor(),
                FileProcess      = mockFileSearch
            });
            var inlineTextDocService =
                new InlineTextDocService();

            var statusCode =
                inlineTextDocService
                .ProcessRequest("GET /" + guid + ".txt HTTP/1.1",
                                new HttpResponse(zSocket),
                                properties);

            File.Delete("c:/" + guid + ".png");
            Assert.Equal("200 OK", statusCode);
            zSocket.VerifySend(data, 2);
            zSocket.VerifySend(GetByte("HTTP/1.1 200 OK\r\n"),
                               GetByteCount("HTTP/1.1 200 OK\r\n"));
            zSocket.VerifySend(GetByte("Content-Length: 2\r\n\r\n"),
                               GetByteCount("Content-Length: 2\r\n\r\n"));
            zSocket.VerifySend(GetByte("Cache-Control: no-cache\r\n"),
                               GetByteCount("Cache-Control: no-cache\r\n"));
            zSocket.VerifySend(GetByte("Content-Disposition: inline"
                                       + "; filename = " + guid + ".txt\r\n"),
                               GetByteCount("Content-Disposition: inline"
                                            + "; filename = " + guid + ".txt\r\n"));
            zSocket.VerifySend(GetByte("Content-Type: text/plain\r\n"),
                               GetByteCount("Content-Type: text/plain\r\n"));
        }
예제 #4
0
        public void Send_Data_Get_Request()
        {
            var zSocket        = new MockZSocket();
            var mockFileSearch = new MockFileProcessor()
                                 .StubExists(true);
            var properties = new ServerProperties(@"c:/",
                                                  5555, new ServerTime(),
                                                  new MockPrinter(),
                                                  new Readers
            {
                DirectoryProcess = new DirectoryProcessor(),
                FileProcess      = mockFileSearch
            });
            var formService = new FormService();

            var statusCode = formService
                             .ProcessRequest("GET /form HTTP/1.1",
                                             new HttpResponse(zSocket), properties);

            var correctOutput = new StringBuilder();

            correctOutput.Append(@"<!DOCTYPE html>");
            correctOutput.Append(@"<html>");
            correctOutput.Append(@"<head><title>Vatic Form Page</title></head>");
            correctOutput.Append(@"<body>");
            correctOutput.Append(@"<form action=""form"" method=""post"">");
            correctOutput.Append(@"First name:<br>");
            correctOutput.Append(@"<input type=""text"" name=""firstname""><br>");
            correctOutput.Append(@"Last name:<br>");
            correctOutput.Append(@"<input type=""text"" name=""lastname""><br><br>");
            correctOutput.Append(@"<input type=""submit"" value=""Submit"">");
            correctOutput.Append(@"</form>");
            correctOutput.Append(@"</body>");
            correctOutput.Append(@"</html>");

            Assert.Equal("200 OK", statusCode);
            zSocket.VerifySend(GetByte("HTTP/1.1 200 OK\r\n"),
                               GetByteCount("HTTP/1.1 200 OK\r\n"));
            zSocket.VerifySend(GetByte("Cache-Control: no-cache\r\n"),
                               GetByteCount("Cache-Control: no-cache\r\n"));
            zSocket.VerifySend(GetByte("Content-Type: text/html\r\n"),
                               GetByteCount("Content-Type: text/html\r\n"));
            zSocket.VerifySend(GetByte("Content-Length: "
                                       + GetByteCount(correctOutput.ToString())
                                       + "\r\n\r\n"),
                               GetByteCount("Content-Length: "
                                            + GetByteCount(correctOutput.ToString())
                                            + "\r\n\r\n"));
            zSocket.VerifySend(GetByte(correctOutput.ToString()),
                               GetByteCount(correctOutput.ToString()));
            zSocket.VerifySend(GetByte(correctOutput.ToString()),
                               GetByteCount(correctOutput.ToString()));
        }
        public void Cant_Send_Data_Protected_Data()
        {
            var zSocket        = new MockZSocket();
            var mockFileSearch = new MockFileProcessor()
                                 .StubExists(true);
            var properties = new ServerProperties(@"c:/",
                                                  5555, new ServerTime(),
                                                  new MockPrinter(),
                                                  new Readers
            {
                DirectoryProcess = new MockDirectoryProcessor(),
                FileProcess      = mockFileSearch
            });
            var fileSendService = new FileSendService();
            var correctOutput   = new StringBuilder();

            correctOutput.Append(@"<!DOCTYPE html>");
            correctOutput.Append(@"<html>");
            correctOutput.Append(@"<head><title>Vatic Server 403 Error Page</title></head>");
            correctOutput.Append(@"<body>");
            correctOutput.Append(@"<h1>403 Forbidden, Can not process request on port 5555</h1>");
            correctOutput.Append(@"</body>");
            correctOutput.Append(@"</html>");
            var statusCode
                = fileSendService
                  .ProcessRequest("GET /pagefile.sys HTTP/1.1",
                                  new HttpResponse(zSocket),
                                  properties);

            Assert.Equal("403 Forbidden", statusCode);
            zSocket.VerifySend(GetByte("HTTP/1.1 403 Forbidden\r\n"),
                               GetByteCount("HTTP/1.1 403 Forbidden\r\n"));
            zSocket.VerifySend(GetByte("Cache-Control: no-cache\r\n"),
                               GetByteCount("Cache-Control: no-cache\r\n"));
            zSocket.VerifySend(GetByte("Content-Type: text/html\r\n"),
                               GetByteCount("Content-Type: text/html\r\n"));
            zSocket.VerifySend(GetByte("Content-Length: "
                                       + GetByteCount(correctOutput.ToString())
                                       + "\r\n\r\n"),
                               GetByteCount("Content-Length: "
                                            + GetByteCount(correctOutput.ToString())
                                            + "\r\n\r\n"));

            zSocket.VerifySend(GetByte(correctOutput.ToString()),
                               GetByteCount(correctOutput.ToString()));
        }
예제 #6
0
        public void Error_Message()
        {
            var zSocket       = new MockZSocket();
            var correctOutput = new StringBuilder();

            correctOutput.Append(@"<!DOCTYPE html>");
            correctOutput.Append(@"<html>");
            correctOutput.Append(@"<head><title>Vatic Server 404 Error Page</title></head>");
            correctOutput.Append(@"<body>");
            correctOutput.Append(@"<h1>404, Can not process request on port 5555</h1>");
            correctOutput.Append(@"</body>");
            correctOutput.Append(@"</html>");
            var serverProperties = new ServerProperties(null,
                                                        5555, new ServerTime(),
                                                        new MockPrinter());
            var service404 = new Service404();

            service404.ProcessRequest("", new HttpResponse(zSocket),
                                      serverProperties);

            zSocket.VerifySend(GetByte("HTTP/1.1 404 Not Found\r\n"),
                               GetByteCount("HTTP/1.1 404 Not Found\r\n"));
            zSocket.VerifySend(GetByte("Cache-Control: no-cache\r\n"),
                               GetByteCount("Cache-Control: no-cache\r\n"));
            zSocket.VerifySend(GetByte("Content-Type: text/html\r\n"),
                               GetByteCount("Content-Type: text/html\r\n"));
            zSocket.VerifySend(GetByte("Content-Length: "
                                       + GetByteCount(correctOutput.ToString())
                                       + "\r\n\r\n"),
                               GetByteCount("Content-Length: "
                                            + GetByteCount(correctOutput.ToString())
                                            + "\r\n\r\n"));

            zSocket.VerifySend(GetByte(correctOutput.ToString()),
                               GetByteCount(correctOutput.ToString()));
        }
예제 #7
0
        public void Get_Video_Page()
        {
            var zSocket = new MockZSocket();
            var guid    = Guid.NewGuid();
            var data    = new byte[1024];

            data[0] = 0;
            data[1] = 1;
            var writeStream = File.Open("c:/" + guid + ".mp4",
                                        FileMode.OpenOrCreate, FileAccess.ReadWrite,
                                        FileShare.Read);

            writeStream.Write(data, 0, 2);
            writeStream.Close();

            var mockFileSearch = new MockFileProcessor()
                                 .StubExists(true)
                                 .StubFileSize(2);

            var properties = new ServerProperties(@"c:/",
                                                  5555, new ServerTime(),
                                                  new MockPrinter(),
                                                  new Readers()
            {
                DirectoryProcess = new DirectoryProcessor(),
                FileProcess      = mockFileSearch
            });
            var videoStream = new VideoStreamingService();

            var statusCode = videoStream
                             .ProcessRequest("GET /" + guid + ".mp4 HTTP/1.1",
                                             new HttpResponse(zSocket), properties);
            var correctOutput = new StringBuilder();

            correctOutput.Append(@"<!DOCTYPE html>");
            correctOutput.Append(@"<html>");
            correctOutput.Append(@"<head><title>Vatic Video</title></head>");
            correctOutput.Append(@"<body>");
            correctOutput.Append(@"<video width=""320"" height=""240"" controls>");
            correctOutput.Append(@"<source src=""http://127.0.0.1:5555/" + guid + ".mp4.vaticToMp4");
            correctOutput.Append(@""" type=""video/mp4"">");
            correctOutput.Append(@"</video>");
            correctOutput.Append(@"</body>");
            correctOutput.Append(@"</html>");

            File.Delete("c:/" + guid + ".mp4");
            Assert.Equal("200 OK", statusCode);
            zSocket.VerifySend(GetByte("HTTP/1.1 200 OK\r\n"),
                               GetByteCount("HTTP/1.1 200 OK\r\n"));
            zSocket.VerifySend(GetByte("Cache-Control: no-cache\r\n"),
                               GetByteCount("Cache-Control: no-cache\r\n"));
            zSocket.VerifySend(GetByte("Content-Type: text/html\r\n"),
                               GetByteCount("Content-Type: text/html\r\n"));
            zSocket.VerifySend(GetByte("Content-Length: "
                                       + GetByteCount(correctOutput.ToString())
                                       + "\r\n\r\n"),
                               GetByteCount("Content-Length: "
                                            + GetByteCount(correctOutput.ToString())
                                            + "\r\n\r\n"));

            zSocket.VerifySend(GetByte(correctOutput.ToString()),
                               GetByteCount(correctOutput.ToString()));
        }
예제 #8
0
        public void Send_Data_Post_Request()
        {
            var zSocket        = new MockZSocket();
            var mockFileSearch = new MockFileProcessor()
                                 .StubExists(true);
            var properties = new ServerProperties(@"c:/",
                                                  5555, new ServerTime(),
                                                  new MockPrinter(),
                                                  new Readers
            {
                DirectoryProcess = new DirectoryProcessor(),
                FileProcess      = mockFileSearch
            });
            var formService = new FormService();

            var statusCode = formService.ProcessRequest("POST /form HTTP/1.1\r\n" +
                                                        "Host: localhost:8080\r\n" +
                                                        "Connection: keep-alive\r\n" +
                                                        "Content-Length: 33\r\n" +
                                                        "Cache - Control: max - age = 0\r\n" +
                                                        "Accept: text / html,application / xhtml + xml,application / xml; q = 0.9,image / webp,*/*;q=0.8\r\n" +
                                                        "Origin: http://localhost:8080\r\n" +
                                                        "Upgrade-Insecure-Requests: 1\r\n" +
                                                        "User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36\r\n" +
                                                        "Content-Type: application/x-www-form-urlencoded\r\n" +
                                                        "Referer: http://localhost:8080/form\r\n" +
                                                        "Accept-Encoding: gzip, deflate\r\n" +
                                                        "Accept-Language: en-US,en;q=0.8\r\n\r\n" +
                                                        "firstname=John%26"
                                                        + "&lastname=Walsher%26",
                                                        new HttpResponse(zSocket), properties);

            var correctOutput = new StringBuilder();

            correctOutput.Append(@"<!DOCTYPE html>");
            correctOutput.Append(@"<html>");
            correctOutput.Append(@"<head><title>Vatic Form Page</title></head>");
            correctOutput.Append(@"<body>");

            correctOutput.Append(@"First Name Submitted:<br>");
            correctOutput.Append(@"John&amp;<br>");
            correctOutput.Append(@"Last Name Submitted:<br>");
            correctOutput.Append(@"Walsher&amp;<br>");


            correctOutput.Append(@"</body>");
            correctOutput.Append(@"</html>");

            Assert.Equal("200 OK", statusCode);
            zSocket.VerifySend(GetByte("HTTP/1.1 200 OK\r\n"),
                               GetByteCount("HTTP/1.1 200 OK\r\n"));
            zSocket.VerifySend(GetByte("Cache-Control: no-cache\r\n"),
                               GetByteCount("Cache-Control: no-cache\r\n"));
            zSocket.VerifySend(GetByte("Content-Type: text/html\r\n"),
                               GetByteCount("Content-Type: text/html\r\n"));
            zSocket.VerifySend(GetByte("Content-Length: "
                                       + GetByteCount(correctOutput.ToString())
                                       + "\r\n\r\n"),
                               GetByteCount("Content-Length: "
                                            + GetByteCount(correctOutput.ToString())
                                            + "\r\n\r\n"));
            zSocket.VerifySend(GetByte(correctOutput.ToString()),
                               GetByteCount(correctOutput.ToString()));
            zSocket.VerifySend(GetByte(correctOutput.ToString()),
                               GetByteCount(correctOutput.ToString()));
        }