コード例 #1
0
        public void Can_Process_Non_Post_Request(string requestListing)
        {
            var request = requestListing + "\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/JSON\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";

            var zSocket = new MockZSocket().StubReceive("")
                          .StubSentToReturn(10)
                          .StubConnect(true);

            zSocket = zSocket.StubAcceptObject(zSocket);
            var properties = new ServerProperties(@"Home",
                                                  8080, new ServerTime(),
                                                  new MockPrinter());
            var process = new RequestProcessor();
            var status  = process.HandleRequest(request, zSocket,
                                                new MockHttpService()
                                                .StubProcessRequest("200 OK"),
                                                properties, new HttpResponse(zSocket));

            Assert.Equal("200 OK", status);
            zSocket.VerifyManyReceive(0);
        }
コード例 #2
0
        public void Process_Request_Send_Json()
        {
            var zSocket      = new MockZSocket();
            var ticTacToeBox = new List <string>
            {
                "x",
                "-2-",
                "-3-",
                "-4-",
                "-5-",
                "-6-",
                "-7-",
                "@",
                "-9-"
            };

            var mockAi = new MockAi()
                         .StubMove(
                new TicTacToeBoxClass.TicTacToeBox(
                    ListModule.OfSeq(ticTacToeBox)));

            var service          = new TicTacToeService();
            var serverProperties = new ServerProperties(null,
                                                        5555, new ServerTime(),
                                                        new MockPrinter(),
                                                        new ServiceDependents(new JsonConverter(),
                                                                              new TicTacToeGame(new User(), mockAi,
                                                                                                MakeSettings())));
            var statusCode = service.ProcessRequest(GetJsonData(),
                                                    new HttpResponse(zSocket), serverProperties);
            var example =
                @"{ ""board"" : [""x"", ""-2-"", ""-3-"", ""-4-"", ""-5-"", ""-6-"", ""-7-"", ""@"", ""-9-""], ""gameOver"" : ""false""}";

            zSocket.VerifySend(GetByte("HTTP/1.1 200 OK\r\n"),
                               GetByteCount("HTTP/1.1 200 OK\r\n"));

            zSocket.VerifySend(GetByte(example),
                               GetByteCount(example));

            zSocket.VerifySend(GetByte("Content-Length: "
                                       + GetByteCount(example)
                                       + "\r\n\r\n"),
                               GetByteCount("Content-Length: "
                                            + GetByteCount(example)
                                            + "\r\n\r\n"));

            zSocket.VerifySend(GetByte("Content-Type: application/JSON\r\n"),
                               GetByteCount("Content-Type: application/JSON\r\n"));

            zSocket.VerifySend(GetByte("Access-Control-Allow-Origin: *\r\n"),
                               GetByteCount("Access-Control-Allow-Origin: *\r\n"));
            Assert.Equal("200 OK", statusCode);
        }
コード例 #3
0
        public void Process_Request_Options()
        {
            var zSocket          = new MockZSocket();
            var service          = new TicTacToeService();
            var serverProperties = new ServerProperties(null,
                                                        5555, new ServerTime(),
                                                        new MockPrinter());
            var statusCode = service.ProcessRequest(GetOptionData(),
                                                    new HttpResponse(zSocket), serverProperties);

            zSocket.VerifySend(GetByte("HTTP/1.1 200 OK\r\n"),
                               GetByteCount("HTTP/1.1 200 OK\r\n"));
            zSocket.VerifySend(GetByte("Access-Control-Allow-Origin: *\r\n"),
                               GetByteCount("Access-Control-Allow-Origin: *\r\n"));
            zSocket.VerifySend(GetByte("Access-Control-Allow-Methods: POST, GET, OPTIONS\r\n"),
                               GetByteCount("Access-Control-Allow-Methods: POST, GET, OPTIONS\r\n"));
            Assert.Equal("200 OK", statusCode);
        }
コード例 #4
0
        public void Post_Request_Two_Parts()
        {
            var request = GetJson();
            var zSocket = new MockZSocket()
                          .StubReceive(request)
                          .StubSentToReturn(10)
                          .StubConnect(true);

            zSocket = zSocket.StubAcceptObject(zSocket);
            var properties = new ServerProperties(@"Home",
                                                  8080, new ServerTime(),
                                                  new MockPrinter());
            var process = new RequestProcessor();
            var status  = process.HandleRequest(GetRequestHeader(), zSocket,
                                                new MockHttpService()
                                                .StubProcessRequest("200 OK"),
                                                properties, new HttpResponse(zSocket));

            zSocket.VerifyManyReceive(1);
            Assert.Equal("200 OK", status);
        }
コード例 #5
0
        public void Process_Request_No_Send_Json()
        {
            var zSocket          = new MockZSocket();
            var service          = new TicTacToeService();
            var serverProperties = new ServerProperties(null,
                                                        5555, new ServerTime(),
                                                        new MockPrinter(),
                                                        new ServiceDependents(new JsonConverter(),
                                                                              new TicTacToeGame(new User(), new Ai(),
                                                                                                MakeSettings())));
            var statusCode =
                service.ProcessRequest(GetRequestNoJson(),
                                       new HttpResponse(zSocket), serverProperties);
            var example =
                @"{ ""board"" : [""-1-"", ""-2-"", ""-3-"", ""-4-"", ""-5-"", ""-6-"", ""-7-"", ""-8-"", ""-9-""], ""gameOver"" : ""false""}";


            zSocket.VerifySend(GetByte("HTTP/1.1 200 OK\r\n"),
                               GetByteCount("HTTP/1.1 200 OK\r\n"));

            zSocket.VerifySend(GetByte(example),
                               GetByteCount(example));

            zSocket.VerifySend(GetByte("Content-Length: "
                                       + GetByteCount(example)
                                       + "\r\n\r\n"),
                               GetByteCount("Content-Length: "
                                            + GetByteCount(example)
                                            + "\r\n\r\n"));

            zSocket.VerifySend(GetByte("Content-Type: application/JSON\r\n"),
                               GetByteCount("Content-Type: application/JSON\r\n"));

            zSocket.VerifySend(GetByte("Access-Control-Allow-Origin: *\r\n"),
                               GetByteCount("Access-Control-Allow-Origin: *\r\n"));
            Assert.Equal("200 OK", statusCode);
        }
コード例 #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()));
        }