示例#1
0
        public async Task TestAnonymousAuthentication()
        {
            string       url      = UrlPrefix.CreateLocal();
            HttpListener listener = new HttpListener();

            try
            {
                listener.Prefixes.Add(url);
                listener.AuthenticationSchemes = AuthenticationSchemes.Anonymous;
                listener.Start();

                await ValidateNullUser(url, listener);
            }
            finally
            {
                listener.Stop();
                listener.Close();
            }
        }
示例#2
0
        public async Task TestBasicAuthentication(AuthenticationSchemes authScheme)
        {
            string       url      = UrlPrefix.CreateLocal();
            HttpListener listener = new HttpListener();

            try
            {
                listener.Prefixes.Add(url);
                listener.AuthenticationSchemes = authScheme;
                listener.Start();

                await ValidateValidUser(url, listener);
            }
            finally
            {
                listener.Stop();
                listener.Close();
            }
        }
示例#3
0
        public async Task TestAnonymousAuthenticationWithDelegate()
        {
            string       url      = UrlPrefix.CreateLocal();
            HttpListener listener = new HttpListener();

            try
            {
                listener.Prefixes.Add(url);
                listener.AuthenticationSchemes = AuthenticationSchemes.None;
                AuthenticationSchemeSelector selector = new AuthenticationSchemeSelector(SelectAnonymousScheme);
                listener.AuthenticationSchemeSelectorDelegate += selector;
                listener.Start();

                await ValidateNullUser(url, listener);
            }
            finally
            {
                listener.Stop();
                listener.Close();
            }
        }
示例#4
0
        public static async Task SimpleRequest_Succeeds()
        {
            string       url = UrlPrefix.CreateLocal();
            const string expectedResponse = "hello from HttpListener";

            using (HttpListener listener = new HttpListener())
            {
                listener.Prefixes.Add(url);
                listener.Start();

                var serverContextTask = listener.GetContextAsync();

                using (HttpClient client = new HttpClient())
                {
                    var clientTask = client.GetStringAsync(url);

                    var serverContext = await serverContextTask;
                    using (var response = serverContext.Response)
                    {
                        var responseBuffer = Encoding.UTF8.GetBytes(expectedResponse);
                        response.ContentLength64 = responseBuffer.Length;

                        using (var output = response.OutputStream)
                        {
                            await output.WriteAsync(responseBuffer, 0, responseBuffer.Length);
                        }
                    }

                    var clientString = await clientTask;

                    Assert.Equal(expectedResponse, clientString);
                }

                listener.Stop();
            }
        }