Example #1
0
 private void 启动WEB网关ToolStripMenuItem_Click(object sender, EventArgs e)
 {
     httpServer             = new MyHttpServer(Convert.ToInt32(toolStripTextBox1.Text));
     httpServer.EventMylog += HttpServer_EventMylog;
     httpServer.Start(Convert.ToInt32(toolStripTextBox1.Text));
     textBox1.Text = toolStripTextBox1.Text + "端口已启动,可以接收HTTP请求";
 }
Example #2
0
        static void Main(string[] args)
        {
            var handle = GetConsoleWindow();

            if (args.Length > 0)
            {
                if (args[0].Equals("debug"))
                {
                    ShowWindow(handle, SW_SHOW);  // show the console window for debug purposes
                }
                else
                {
                    ShowWindow(handle, SW_HIDE);  // hdie the console
                }
            }
            else
            {
                ShowWindow(handle, SW_HIDE);  // hide the console
            }


            HttpServer httpServer;

            httpServer = new MyHttpServer(8090);

            Thread thread = new Thread(new ThreadStart(httpServer.listen));

            thread.Start();
            //var handle = GetConsoleWindow();
            // Hide
            //ShowWindow(handle, SW_HIDE);

            // Show
            //ShowWindow(handle, SW_SHOW);
        }
Example #3
0
    void Start()
    {
        HttpServer httpServer = new MyHttpServer(8080);

        thread = new Thread(new ThreadStart(httpServer.listen));
        thread.Start();
    }
Example #4
0
        public void RemoteServerShouldWorkCorrectlyWithSpecificBaseAddress()
        {
            Assert.False(RemoteServer.GlobalIsConfigured);

            MyHttpServer
            .WorkingRemotely("http://google.com")
            .WithHttpRequestMessage(req => req.WithRequestUri("/users/ivaylokenov/repos"))
            .ShouldReturnHttpResponseMessage()
            .WithResponseTime(time => time.TotalMilliseconds > 0)
            .WithStatusCode(HttpStatusCode.NotFound)
            .ContainingContentHeader(HttpContentHeader.ContentType, "text/html; charset=UTF-8");

            Assert.False(RemoteServer.GlobalIsConfigured);

            MyHttpServer
            .WorkingRemotely("https://api.github.com")
            .WithHttpRequestMessage(req => req
                                    .WithRequestUri("/users/ivaylokenov/repos")
                                    .WithHeader(HttpHeader.UserAgent, "MyTested.WebApi"))
            .ShouldReturnHttpResponseMessage()
            .WithResponseTime(time => time.TotalMilliseconds > 0)
            .WithStatusCode(HttpStatusCode.OK)
            .ContainingContentHeader(HttpContentHeader.ContentType, "application/json; charset=utf-8");

            Assert.False(RemoteServer.GlobalIsConfigured);
        }
Example #5
0
        public void RemoteServerShouldWorkCorrectlyWithGlobalConfiguration()
        {
            MyHttpServer.IsLocatedAt("http://google.com", clientHandler =>
            {
                clientHandler.AllowAutoRedirect = true;
            });

            MyHttpServer
            .WorkingRemotely()
            .WithHttpRequestMessage(req => req.WithMethod(HttpMethod.Get))
            .ShouldReturnHttpResponseMessage()
            .WithResponseTime(time => time.TotalMilliseconds > 0)
            .WithStatusCode(HttpStatusCode.OK)
            .ContainingContentHeader(HttpContentHeader.ContentType);

            MyHttpServer
            .WorkingRemotely()
            .WithHttpRequestMessage(req => req.WithMethod(HttpMethod.Delete))
            .ShouldReturnHttpResponseMessage()
            .WithResponseTime(time => time.TotalMilliseconds > 0)
            .WithStatusCode(HttpStatusCode.MethodNotAllowed);

            MyHttpServer
            .WorkingRemotely()
            .WithHttpRequestMessage(req => req.WithRequestUri("/notfound"))
            .ShouldReturnHttpResponseMessage()
            .WithResponseTime(time => time.TotalMilliseconds > 0)
            .WithStatusCode(HttpStatusCode.NotFound);

            RemoteServer.DisposeGlobal();
        }
Example #6
0
        private void init()
        {
            _dispatcher  = new RoomEventDispatcher();
            _roomFactory = new ServerRoomFactory(_dispatcher, _coRoutineManager, _assetManager);
            _roomManager = new SingleRoomManager(_roomFactory, _dispatcher, false, _roomListener);


            _loginServer = new LoginServer(_dispatcher, _roomManager);
            _loginServer.Start(new NetworkPortInfo(
                                   SingletonManager.Get <ServerFileSystemConfigManager>().BootConfig.BattleServer.TcpPort,
                                   SingletonManager.Get <ServerFileSystemConfigManager>().BootConfig.BattleServer.UdpPort));

            _hallServerNetwork = new HallServerNetwork(_dispatcher);
            _hallServerNetwork.StartServer(new NetworkPortInfo(
                                               SingletonManager.Get <ServerFileSystemConfigManager>().BootConfig.HallRoomServer.ListenPort, 0));

            MyHttpServer.Start(SingletonManager.Get <ServerFileSystemConfigManager>().BootConfig.HttpPort);
            if (SingletonManager.Get <ServerFileSystemConfigManager>().BootConfig.AllocationServer.Open == 1)
            {
                _hallServerNetwork.ClientConnect(new NetworkPortInfo(
                                                     SingletonManager.Get <ServerFileSystemConfigManager>().BootConfig.AllocationServer.ConnectPort, 0));
            }
            else
            {
                _logger.InfoFormat("Server Configuration: Do Not connect to Allocation Server!");
            }
        }
Example #7
0
        public void StartListening(string app = "", int port = 80)
        {
            httpserver = new MyHttpServer(app, port);

            httpserver.HandleRequest += Httpserver_HandleRequest;

            httpserver.Start();
        }
Example #8
0
 public void WithStringContentAssertionsShouldWorkCorrectlyWithCorrectPredicate()
 {
     MyHttpServer
     .WorkingRemotely(BaseLocalAddress)
     .WithHttpRequestMessage(req => req.WithMethod(HttpMethod.Get))
     .ShouldReturnHttpResponseMessage()
     .WithContent(content => content == "Not found!");
 }
Example #9
0
 public void WithStringContentShouldNotThrowExceptionWithCorrectContent()
 {
     MyHttpServer
     .WorkingRemotely(BaseLocalAddress)
     .WithHttpRequestMessage(req => req.WithMethod(HttpMethod.Get))
     .ShouldReturnHttpResponseMessage()
     .WithContent("Not found!");
 }
Example #10
0
 public void WithSuccessStatusCodeShouldNotThrowExceptionWithValidStatusCode()
 {
     MyHttpServer
     .WorkingRemotely(BaseAddress)
     .WithHttpRequestMessage(req => req.WithMethod(HttpMethod.Get))
     .ShouldReturnHttpResponseMessage()
     .WithSuccessStatusCode();
 }
Example #11
0
 public void WithReasonPhraseShouldNotThrowExceptionWithValidPhrase()
 {
     MyHttpServer
     .WorkingRemotely(BaseAddress)
     .WithHttpRequestMessage(req => req.WithMethod(HttpMethod.Get))
     .ShouldReturnHttpResponseMessage()
     .WithReasonPhrase("OK");
 }
Example #12
0
 public void WithVersionShouldNotThrowExceptionWithValidVersion()
 {
     MyHttpServer
     .WorkingRemotely(BaseAddress)
     .WithHttpRequestMessage(req => req.WithMethod(HttpMethod.Get))
     .ShouldReturnHttpResponseMessage()
     .WithVersion(new Version(1, 1));
 }
Example #13
0
 public void ContainingContentHeaderShouldNotThrowExceptionWithCorrectHeaderNameAndValues()
 {
     MyHttpServer
     .WorkingRemotely(BaseAddress)
     .WithHttpRequestMessage(req => req.WithMethod(HttpMethod.Get))
     .ShouldReturnHttpResponseMessage()
     .ContainingContentHeader("Content-Type", new[] { "text/html; charset=utf-8" });
 }
Example #14
0
 public void ContainingContentHeaderShouldNotThrowExceptionWithCorrectHeaderName()
 {
     MyHttpServer
     .WorkingRemotely(BaseAddress)
     .WithHttpRequestMessage(req => req.WithMethod(HttpMethod.Get))
     .ShouldReturnHttpResponseMessage()
     .ContainingContentHeader("Content-Type");
 }
Example #15
0
 public void ContainingHeaderShouldNotThrowExceptionWithCorrectHeaderNameAndValues()
 {
     MyHttpServer
     .WorkingRemotely(BaseAddress)
     .WithHttpRequestMessage(req => req.WithMethod(HttpMethod.Get))
     .ShouldReturnHttpResponseMessage()
     .ContainingHeader("Server", new[] { "Microsoft-IIS/8.0" });
 }
Example #16
0
        /// <summary>
        /// Initilizes the Http server and starts the listening thread.
        /// </summary>
        /// <returns>the initialized http server</returns>
        private static HttpServer InitializeHttpServer()
        {
            HttpServer httpServer = new MyHttpServer(8080, IsLocalIpAddress(""));
            Thread     thread     = new Thread(httpServer.Listen);

            thread.Start();
            return(httpServer);
        }
Example #17
0
        public void WithMethodShouldPopulateCorrectMethod()
        {
            var httpRequestMessage = MyHttpServer
                                     .WorkingRemotely(BaseAddress)
                                     .WithHttpRequestMessage(request => request.WithMethod(HttpMethod.Get))
                                     .AndProvideTheHttpRequestMessage();

            Assert.Equal(HttpMethod.Get, httpRequestMessage.Method);
        }
Example #18
0
        public void WithVersionShouldPopulateCorrectVersion()
        {
            var httpRequestMessage = MyHttpServer
                                     .WorkingRemotely(BaseAddress)
                                     .WithHttpRequestMessage(request => request.WithVersion(new Version(1, 1)))
                                     .AndProvideTheHttpRequestMessage();

            Assert.Equal(Version, httpRequestMessage.Version.ToString());
        }
Example #19
0
        public void WithRequestUriShouldPopulateCorrectUri()
        {
            var httpRequestMessage = MyHttpServer
                                     .WorkingRemotely(BaseAddress)
                                     .WithHttpRequestMessage(request => request.WithRequestUri(new Uri(RequestUri)))
                                     .AndProvideTheHttpRequestMessage();

            Assert.Equal(new Uri(RequestUri), httpRequestMessage.RequestUri);
        }
Example #20
0
        private void SetupHttpServer()
        {
            ReleaseSocketRelatedResource();

            _listener   = new MyHttpServer(this, this.Port);
            _httpThread = new Thread(new ThreadStart(_listener.listen));
            _httpThread.IsBackground = true;
            _httpThread.Start();
        }
Example #21
0
        public void WithHeaderShouldPopulateCorrectHeader()
        {
            var httpRequestMessage = MyHttpServer
                                     .WorkingRemotely(BaseAddress)
                                     .WithHttpRequestMessage(request => request.WithHeader(TestHeader, TestHeaderValue))
                                     .AndProvideTheHttpRequestMessage();

            Assert.True(httpRequestMessage.Headers.Contains(TestHeader));
            Assert.True(httpRequestMessage.Headers.First(h => h.Key == TestHeader).Value.Contains(TestHeaderValue));
        }
Example #22
0
        public void WithStringContentEncodingAndMediaTypeShouldPopulateCorrectContent()
        {
            var httpRequestMessage = MyHttpServer
                                     .WorkingRemotely(BaseAddress)
                                     .WithHttpRequestMessage(request => request.WithStringContent(StringContent, Encoding.UTF8, MediaType.ApplicationJson))
                                     .AndProvideTheHttpRequestMessage();

            Assert.IsAssignableFrom <StringContent>(httpRequestMessage.Content);
            Assert.Equal(StringContent, httpRequestMessage.Content.ReadAsStringAsync().Result);
        }
Example #23
0
        public void WithJsonContentShouldPopulateCorrectContent()
        {
            var httpRequestMessage = MyHttpServer
                                     .WorkingRemotely(BaseAddress)
                                     .WithHttpRequestMessage(request => request.WithJsonContent(@"{""Age"":5}"))
                                     .AndProvideTheHttpRequestMessage();

            Assert.Equal(@"{""Age"":5}", httpRequestMessage.Content.ReadAsStringAsync().Result);
            Assert.Equal(MediaType.ApplicationJson, httpRequestMessage.Content.Headers.ContentType.MediaType);
        }
Example #24
0
        public void WithFormUrlEncodedContentShouldPopulateCorrectContentWithDirectString()
        {
            var httpRequestMessage = MyHttpServer
                                     .WorkingRemotely(BaseAddress)
                                     .WithHttpRequestMessage(request => request.WithFormUrlEncodedContent("First=FirstValue&Second=SecondValue"))
                                     .AndProvideTheHttpRequestMessage();

            Assert.Equal("First=FirstValue&Second=SecondValue", httpRequestMessage.Content.ReadAsStringAsync().Result);
            Assert.Equal(MediaType.FormUrlEncoded, httpRequestMessage.Content.Headers.ContentType.MediaType);
        }
Example #25
0
 public void AndAlsoShouldWorkCorrectly()
 {
     MyHttpServer
     .WorkingRemotely(BaseAddress)
     .WithHttpRequestMessage(req => req.WithMethod(HttpMethod.Get))
     .ShouldReturnHttpResponseMessage()
     .WithSuccessStatusCode()
     .AndAlso()
     .WithReasonPhrase("OK");
 }
Example #26
0
        public void WithContentShouldPopulateCorrectContent()
        {
            var httpRequestMessage = MyHttpServer
                                     .WorkingRemotely(BaseAddress)
                                     .WithHttpRequestMessage(request => request.WithContent(new StringContent(StringContent)))
                                     .AndProvideTheHttpRequestMessage();

            Assert.IsAssignableFrom <StringContent>(httpRequestMessage.Content);
            Assert.Equal(StringContent, httpRequestMessage.Content.ReadAsStringAsync().Result);
        }
Example #27
0
        public void WithByteArrayContentAndBufferSizeShouldPopulateCorrectContent()
        {
            var httpRequestMessage = MyHttpServer
                                     .WorkingRemotely(BaseAddress)
                                     .WithHttpRequestMessage(request => request.WithByteArrayContent(this.buffer, 2, 4))
                                     .AndProvideTheHttpRequestMessage();

            Assert.IsAssignableFrom <ByteArrayContent>(httpRequestMessage.Content);
            Assert.Equal(4, httpRequestMessage.Content.ReadAsByteArrayAsync().Result.Length);
        }
Example #28
0
        public void WithStreamContentShouldPopulateCorrectContent()
        {
            var httpRequestMessage = MyHttpServer
                                     .WorkingRemotely(BaseAddress)
                                     .WithHttpRequestMessage(request => request.WithStreamContent(new MemoryStream(this.buffer)))
                                     .AndProvideTheHttpRequestMessage();

            Assert.IsAssignableFrom <StreamContent>(httpRequestMessage.Content);
            Assert.Equal(this.buffer.Length, httpRequestMessage.Content.ReadAsByteArrayAsync().Result.Length);
        }
Example #29
0
 public void WithHeaderCookiesShouldSetCorrectCookieValue()
 {
     MyHttpServer
     .WorkingRemotely(BaseLocalAddress)
     .WithHttpRequestMessage(req => req
                             .WithMethod(HttpMethod.Get)
                             .WithRequestUri("/cookies")
                             .WithHeader(HttpHeader.Cookie, "cookiename=cookievalue;anothercookie=anothervalue"))
     .ShouldReturnHttpResponseMessage()
     .WithContent("cookiename+cookievalue!anothercookie+anothervalue");
 }
Example #30
0
 static async Task Main(string[] args)
 {
     string  uri     = "http://localhost:5000/";
     IServer server  = new MyHttpServer(uri);
     var     handler = new ApplicationBuilder()
                       .Use(FooMiddleware)
                       .Use(BarMiddleware)
                       .Use(BazMiddleware)
                       .Build();
     await server.StartAsync(handler);
 }
        public Tuple<IEnumerable<string>, IEnumerable<string>> Get()
        {
            HttpConfiguration configuration = new HttpConfiguration();
            configuration.MessageHandlers.Add(new FooHandler());          //利用HttpConfiguration注册3个自定义的DelegatingHandler
            configuration.MessageHandlers.Add(new BarHandler());
            configuration.MessageHandlers.Add(new BazHandler());

            MyHttpServer httpServer = new MyHttpServer(configuration);       //创建自定义的HttpServer
            IEnumerable<string> chain1 = this.GetHandlerChain(httpServer).ToArray();
            httpServer.Initialize();                             //调用HttpServer的Initialize方法,从而初始化整个消息处理管道
            IEnumerable<string> chain2 = this.GetHandlerChain(httpServer).ToArray();
            return new Tuple<IEnumerable<string>, IEnumerable<string>>(chain1, chain2);     //两个字符串集合组成一个Tuple<IEnumerable<string>, IEnumerable<string>>对象并将其作为Get方法的返回值,这个Tuple反映了HttpMessageHandler链在HttpServer初始化前后的结构
        }
        public IEnumerable<string> Get()
        {
            MyHttpServer httpServer = new MyHttpServer();

            //Thread.CurrentPrincipal == Null
            Thread.CurrentPrincipal = null;
            HttpRequestMessage request = new HttpRequestMessage();
            httpServer.SendAsync(request, new CancellationToken(false));
            GenericPrincipal principal = (GenericPrincipal)Thread.CurrentPrincipal;
            string identity1 = string.IsNullOrEmpty(principal.Identity.Name) ? "N/A" : principal.Identity.Name;

            //Thread.CurrentPrincipal != Null
            GenericIdentity identity = new GenericIdentity("Artech");
            Thread.CurrentPrincipal = new GenericPrincipal(identity, new string[0]);
            request = new HttpRequestMessage();
            httpServer.SendAsync(request, new CancellationToken(false));
            principal = (GenericPrincipal)Thread.CurrentPrincipal;
            string identity2 = string.IsNullOrEmpty(principal.Identity.Name) ? "N/A" : principal.Identity.Name;

            return new string[] { identity1, identity2 };
        }