public void TimeoutManager_AccessBeforeAndAfterClose_GetObjectDisposedException()
        {
            // Access the TimeoutManager after calling Close and make sure it is not accessible.
            _listener.Start();
            HttpListenerTimeoutManager timeoutManager = _listener.TimeoutManager;

            Assert.NotNull(timeoutManager);
            _listener.Close();
            Assert.Throws <ObjectDisposedException>(() => timeoutManager.MinSendBytesPerSecond = 10);
        }
 public void UnsupportedProperties_Throw()
 {
     using (var listener = new HttpListener())
     {
         HttpListenerTimeoutManager m = listener.TimeoutManager;
         Assert.Throws <PlatformNotSupportedException>(() => m.EntityBody            = TimeSpan.Zero);
         Assert.Throws <PlatformNotSupportedException>(() => m.HeaderWait            = TimeSpan.Zero);
         Assert.Throws <PlatformNotSupportedException>(() => m.MinSendBytesPerSecond = 0);
         Assert.Throws <PlatformNotSupportedException>(() => m.RequestQueue          = TimeSpan.Zero);
     }
 }
        public void TimeoutManager_AccessNoStart_Success()
        {
            // Access the TimeoutManager without calling Start and make sure it is initialized.
            HttpListenerTimeoutManager timeoutManager = _listener.TimeoutManager;

            Assert.NotNull(timeoutManager);

            uint rate = GetServerTimeout(_listener, HTTP_TIMEOUT_TYPE.MinSendRate);

            Assert.Equal(rate, timeoutManager.MinSendBytesPerSecond);
        }
        public void IdleConnection_Roundtrips()
        {
            using (var listener = new HttpListener())
            {
                HttpListenerTimeoutManager m = listener.TimeoutManager;
                Assert.Equal(TimeSpan.Zero, m.IdleConnection);

                TimeSpan value = TimeSpan.FromSeconds(123);
                m.IdleConnection = value;
                Assert.Equal(value, m.IdleConnection);
            }
        }
        public void DrainEntityBody_Roundtrips()
        {
            using (var listener = new HttpListener())
            {
                HttpListenerTimeoutManager m = listener.TimeoutManager;
                Assert.Equal(TimeSpan.Zero, m.DrainEntityBody);

                TimeSpan value = TimeSpan.FromSeconds(123);
                m.DrainEntityBody = value;
                Assert.Equal(value, m.DrainEntityBody);
            }
        }
 public void Properties_DefaultValues()
 {
     using (var listener = new HttpListener())
     {
         HttpListenerTimeoutManager m = listener.TimeoutManager;
         Assert.NotNull(m);
         Assert.Equal(TimeSpan.Zero, m.DrainEntityBody);
         Assert.Equal(TimeSpan.Zero, m.EntityBody);
         Assert.Equal(TimeSpan.Zero, m.HeaderWait);
         Assert.Equal(TimeSpan.Zero, m.IdleConnection);
         Assert.Equal(0, m.MinSendBytesPerSecond);
         Assert.Equal(TimeSpan.Zero, m.RequestQueue);
     }
 }
        public void TimeoutManager_AccessAfterStop_Success()
        {
            // Access the TimeoutManager after calling Stop and make sure it is accessible.
            _listener.Start();
            _listener.Stop();

            HttpListenerTimeoutManager timeoutManager = _listener.TimeoutManager;

            Assert.NotNull(timeoutManager);

            uint rate = GetServerTimeout(_listener, HTTP_TIMEOUT_TYPE.MinSendRate);

            Assert.Equal(rate, timeoutManager.MinSendBytesPerSecond);
        }
Пример #8
0
        private void AuthorizeSnipForMammal()
        {
            try
            {
                // Start up Process to begin authorization with Spotify API
                string  authURL = string.Format(CultureInfo.InvariantCulture, "{0}?client_id={1}&response_type={2}&redirect_uri={3}&scope={4}", this.spotifyAPIAuthURL, ApplicationKeys.client, this.responseType, this.localCallbackURL, this.scopes);
                Process authorizationProcess = OpenURL(authURL);

                using (HttpListener callbackListener = new HttpListener())
                {
                    HttpListenerTimeoutManager timeoutManager = callbackListener.TimeoutManager;
                    timeoutManager.IdleConnection = new TimeSpan(0, 0, 5); // hr, min, s
                    timeoutManager.HeaderWait     = new TimeSpan(0, 0, 5);

                    callbackListener.Prefixes.Add(this.localCallbackURL);
                    callbackListener.Start();

                    HttpListenerContext  context             = callbackListener.GetContext();
                    HttpListenerRequest  request             = context.Request;
                    HttpListenerResponse response            = context.Response;
                    NameValueCollection  nameValueCollection = request.QueryString;

                    string spotifyAPIAccessGranted   = "Access granted. You may close this window now.";
                    string spotifyAPIAccessDenied    = "Access denied. In order to use this application you must grant it access.";
                    string callbackHtmlStart         = @"<!doctype html><html lang=en><head><meta charset=utf-8><style>div { font-size:xx-large; }</style><title>SnipForMammal Auth</title></head><body><div>";
                    string callbackHtmlEnd           = @"</div></body></html>";
                    string callbackCloseWindowScript = @"<script>window.close();</script>";

                    string outputString = string.Empty;

                    foreach (string keyValue in nameValueCollection.AllKeys)
                    {
                        switch (keyValue.ToLower())
                        {
                        case "code":
                            if (Global.autoCloseAuthWindow)
                            {
                                outputString = string.Format(CultureInfo.InvariantCulture, "{0}{1}{2}{3}", callbackHtmlStart, callbackCloseWindowScript, spotifyAPIAccessGranted, callbackHtmlEnd);
                            }
                            else
                            {
                                outputString = string.Format(CultureInfo.InvariantCulture, "{0}{1}{2}", callbackHtmlStart, spotifyAPIAccessGranted, callbackHtmlEnd);
                            }
                            this.authorizationCode = nameValueCollection[keyValue];
                            Global.debugConsole?.WriteLine("Successfully authorized through Spotify Auth API.");
                            break;

                        case "error":
                            outputString = string.Format(CultureInfo.InvariantCulture, "{0}{1}{2}", callbackHtmlStart, spotifyAPIAccessDenied, callbackHtmlEnd);
                            Global.debugConsole?.WriteLine("Failed authorizing through Spotify Auth API.");
                            break;

                        default:
                            break;
                        }
                    }

                    byte[] buffer = Encoding.UTF8.GetBytes(outputString);
                    response.ContentLength64 = buffer.Length;
                    Stream output = response.OutputStream;
                    output.Write(buffer, 0, buffer.Length);
                    output.Close();

                    callbackListener.Stop();
                }
            }
            catch (Exception e)
            {
                Global.debugConsole?.WriteLine(e.ToString());
            }
        }