Represents an HttpMessageHanlder that can invoke a request directly against an OWIN pipeline (an 'AppFunc').
Inheritance: HttpMessageHandler
 public AutoRedirectTests()
 {
     var responders = new Dictionary<string, Action<IOwinContext>>
     {
         { "/redirect-absolute-302", context =>
             {
                 context.Response.StatusCode = 302;
                 context.Response.ReasonPhrase = "Found";
                 context.Response.Headers.Add("Location", new [] { "http://localhost/redirect" });
             }
         },
         { "/redirect-relative", context =>
             {
                 context.Response.StatusCode = 302;
                 context.Response.ReasonPhrase = "Found";
                 context.Response.Headers.Add("Location", new [] { "redirect" });
             }
         },
         { "/redirect-absolute-301", context =>
             {
                 context.Response.StatusCode = 301;
                 context.Response.ReasonPhrase = "Moved Permanently";
                 context.Response.Headers.Add("Location", new [] { "http://localhost/redirect" });
             }
         },                
         { "/redirect-absolute-303", context =>
             {
                 context.Response.StatusCode = 303;
                 context.Response.ReasonPhrase = "See Other";
                 context.Response.Headers.Add("Location", new [] { "http://localhost/redirect" });
             }
         },
         { "/redirect-absolute-307", context =>
             {
                 context.Response.StatusCode = 307;
                 context.Response.ReasonPhrase = "Temporary Redirect";
                 context.Response.Headers.Add("Location", new [] { "http://localhost/redirect" });
             }
         },
         { "/redirect-loop", context =>
             {
                 context.Response.StatusCode = 302;
                 context.Response.ReasonPhrase = "Found";
                 context.Response.Headers.Add("Location", new[] { "http://localhost/redirect-loop" });
             }
         },
         { "/redirect", context => context.Response.StatusCode = 200 }
     };
     AppFunc appFunc = env =>
     {
         var context = new OwinContext(env);
         responders[context.Request.Path.Value](context);
         return Task.FromResult((object)null);
     };
     _handler = new OwinHttpMessageHandler(appFunc)
     {
         AllowAutoRedirect = true
     };
 }
        public async Task Using_OwinHttpMessageHandler_then_should_have_2_cookies()
        {
            var handler = new OwinHttpMessageHandler(_appFunc)
            {
                UseCookies = true
            };

            using (var client = new HttpClient(handler)
                {
                    BaseAddress = _uri
                })
            {
                var response = await client.GetAsync(_uri);

                var setCookies = response.Headers.GetValues("Set-Cookie");

                //OMG :)
                foreach (var cookie in setCookies)
                {
                    Console.WriteLine("Set-Cookie: {0}", cookie);
                }

                setCookies.Should().HaveCount(2);
            }
        }
        public async Task Setting_cookie_when_headers_are_sent_then_should_have_cookie_in_container()
        {
            const string cookieName1 = "testcookie1";

            var uri = new Uri("http://localhost/");

            AppFunc inner = async env =>
            {
                var context = new OwinContext(env);
                context.Response.Headers.Append("Location", "/");
                await context.Response.WriteAsync("Test");
            };

            AppFunc appFunc = async env =>
            {
                var context = new OwinContext(env);
                context.Response.OnSendingHeaders(_ =>
                {
                    context.Response.Cookies.Append(cookieName1, "c1");
                }, null);
                await inner(env);
            };

            var handler = new OwinHttpMessageHandler(appFunc) { UseCookies = true };
            using (var client = new HttpClient(handler))
            {
                await client.GetAsync(uri);
            }

            handler
                .CookieContainer
                .GetCookies(uri)[cookieName1]
                .ShouldNotBeNull();
        }
        public static HttpClient CreateHttpClient(IdentityServerBearerTokenAuthenticationOptions options)
        {
            var app = PipelineFactory.Create(options);
            var handler = new OwinHttpMessageHandler(app.Build());
            var client = new HttpClient(handler);

            return client;
        }
        public async Task When_host_Nancy_via_IAppBuilder_should_read_X509Certificate2()
        {
            // Given
            var app = new AppBuilder();
            var bootstrapper = new ConfigurableBootstrapper(config => config.Module<TestModule>());

            var cert = @"-----BEGIN CERTIFICATE-----
                            MIICNTCCAZ4CCQC21XwOAYG32zANBgkqhkiG9w0BAQUFADBfMQswCQYDVQQGEwJH
                            QjETMBEGA1UECBMKU29tZS1TdGF0ZTEOMAwGA1UEChMFTmFuY3kxDjAMBgNVBAsT
                            BU5hbmN5MRswGQYDVQQDExJodHRwOi8vbmFuY3lmeC5vcmcwHhcNMTYwMjIyMTE1
                            NzQ3WhcNMTcwMjIxMTE1NzQ3WjBfMQswCQYDVQQGEwJHQjETMBEGA1UECBMKU29t
                            ZS1TdGF0ZTEOMAwGA1UEChMFTmFuY3kxDjAMBgNVBAsTBU5hbmN5MRswGQYDVQQD
                            ExJodHRwOi8vbmFuY3lmeC5vcmcwgZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJAoGB
                            AMT4vOtIH9Fzad+8KCGjMPkkVpCtn+L5H97bnI3x+y3x5lY0WRsK8FyxVshY/7fv
                            TDeeVKUWanmbMkQjgFRYffA3ep3/AIguswYdANiNVHrx0L7DXNDcgsjRwaa6JVgQ
                            9iavlli0a80AF67FN1wfidlHCX53u3/fAjiSTwf7D+NBAgMBAAEwDQYJKoZIhvcN
                            AQEFBQADgYEAh12A4NntHHdVMGaw+2umXkWqCOyAPuNhyBGUHK5vGON+VG0HPFaf
                            8P8eMtdF4deBHkrfoWxRuGGn2tJzNpZLiEf23BAivEf36IqwfkVP7/zDwI+bjVXC
                            k64Un2uN8ALR/wLwfJzHfOLPtsca7ySWhlv8oZo2nk0vR34asQiGJDQ=
                            -----END CERTIFICATE-----
                            ";

            var embeddedCert = Encoding.UTF8.GetBytes(cert);

            app.Use(new Func<AppFunc, AppFunc>(next => (async env =>
            {
                env.Add("ssl.ClientCertificate", new X509Certificate(embeddedCert));
                await next.Invoke(env);
            })));

            app.UseNancy(opts =>
            {
                opts.Bootstrapper = bootstrapper;
                opts.EnableClientCertificates = true;
            });

            var appFunc = app.Build();

            var handler = new OwinHttpMessageHandler(appFunc);

            using (var httpClient = new HttpClient(handler)
            {
                BaseAddress = new Uri("http://localhost")
            })
            {
                // When
                var response = await httpClient.GetAsync(new Uri("http://localhost/ssl"));

                // Then
                Assert.Equal(System.Net.HttpStatusCode.OK, response.StatusCode);
            }
        }
 public static HttpClient CreateClient(this AppFunc appFunc)
 {
     var handler = new OwinHttpMessageHandler(appFunc)
     {
         CookieContainer = new CookieContainer(),
         UseCookies = true, // need this else the auth cookie won't be carried to subsequent requests,
         AllowAutoRedirect = true
     };
     return new HttpClient(handler, true)
     {
         BaseAddress = new Uri("https://localhost")
     };
 }
        public static HttpClient CreateEmbeddedClient(this MidFunc midFunc, Uri baseAddress = null)
        {
            Ensure.That(midFunc).IsNotNull();

            baseAddress = baseAddress ?? new Uri("http://localhost");

            var handler = new OwinHttpMessageHandler(midFunc)
            {
                UseCookies = true
            };

            return new HttpClient(handler, true)
            {
                BaseAddress = baseAddress
            };
        }
        public async Task When_server_sets_cookie_should_have_in_cookie_container()
        {
            const string cookieName1 = "testcookie1";
            const string cookieName2 = "testcookie2";
            var uri = new Uri("http://localhost/");
            AppFunc appFunc = env =>
            {
                var context = new OwinContext(env);
                context.Response.Cookies.Append(cookieName1, "c1");
                context.Response.Cookies.Append(cookieName2, "c2");
                return Task.FromResult(0);
            };
            var handler = new OwinHttpMessageHandler(appFunc) { UseCookies = true };
            using (var client = new HttpClient(handler))
            {
                await client.GetAsync(uri);
            }

            handler
                .CookieContainer
                .GetCookies(uri)[cookieName1]
                .Should()
                .NotBeNull();

            handler
                .CookieContainer
                .GetCookies(uri)[cookieName1]
                .Value
                .Should()
                .Be("c1");

            handler
                .CookieContainer
                .GetCookies(uri)[cookieName2]
                .Should()
                .NotBeNull();

            handler
                .CookieContainer
                .GetCookies(uri)[cookieName2]
                .Value
                .Should()
                .Be("c2");
        }
        public async Task Using_OwinHttpMessageHandler_then_should_have_2_cookies()
        {
            var handler = new OwinHttpMessageHandler(_appFunc)
            {
                UseCookies = true
            };

            using (var client = new HttpClient(handler)
                {
                    BaseAddress = _uri
                })
            {
                var response = await client.GetAsync(_uri);

                var setCookies = response.Headers.GetValues("Set-Cookie");

                setCookies.Count().ShouldBe(2);
            }
        }
        public async Task Should_get_OK()
        {
            var app = new AppBuilder();
            new ExampleStartup().Configuration(app);
            var appFunc = app.Build();

            var handler = new OwinHttpMessageHandler(appFunc)
            {
                UseCookies = true,
                AllowAutoRedirect = true
            };
            using(var httpClient = new HttpClient(handler)
            {
                BaseAddress = new Uri("http://localhost")
            })
            {
                var response = await httpClient.GetAsync("/test");

                Assert.Equal(HttpStatusCode.OK, response.StatusCode);
            }
        }
        public async Task When_host_Nancy_via_IAppBuilder_then_should_handle_requests()
        {
            // Given
            var app = new AppBuilder();
            var bootstrapper = new ConfigurableBootstrapper(config => config.Module<TestModule>());
            app.UseNancy(opts => opts.Bootstrapper = bootstrapper);
            var appFunc = app.Build();

            var handler = new OwinHttpMessageHandler(appFunc);

            using (var httpClient = new HttpClient(handler)
            {
                BaseAddress = new Uri("http://localhost")
            })
            {
                // When
                var response = await httpClient.GetAsync(new Uri("http://localhost/"));

                // Then
                Assert.Equal(System.Net.HttpStatusCode.OK, response.StatusCode);
            }
        }
        public async Task When_changing_use_cookies_after_initial_operation_then_should_throw()
        {
            var handler = new OwinHttpMessageHandler(AppFuncHelpers.NoopAppFunc);
            using (var client = new HttpClient(handler))
            {
                await client.GetAsync("http://localhost/");
            }

            Action act = () => { handler.UseCookies = true; };

            act.ShouldThrow<InvalidOperationException>();
        }
 public ClientCredentialsClient()
 {
     var app = TokenClientIdentityServer.Create();
     _handler = new OwinHttpMessageHandler(app.Build());
     _client = new HttpClient(_handler);
 }
 public IntrospectionEndpointTests()
 {
     var app = Setup.IntrospectionIdentityServer.Create();
     _handler = new OwinHttpMessageHandler(app.Build());
     _client = new HttpClient(_handler);
 }
Exemple #15
0
        public void When_midfunc_paramater_is_null_Then_should_throw()
        {
            Action act = () => { var handler = new OwinHttpMessageHandler((MidFunc)null); };

            act.ShouldThrow <ArgumentNullException>();
        }
        public async Task Environment_should_contain_content_length_header()
        {
            OwinContext owinContext = null;
            var handler = new OwinHttpMessageHandler(env =>
            {
                owinContext = new OwinContext(env);
                return Task.FromResult(0);
            });

            using (var client = new HttpClient(handler))
            {
                var stringContent = new StringContent("hello");
                await client.PostAsync("http://localhost/", stringContent);
            }

            owinContext.Request.Headers.ContainsKey("Content-Length").ShouldBeTrue();
        }
        public AutoRedirectTests()
        {
            Func<IOwinRequest, Task<string>> readToEnd = async request =>
            {
                using(var reader = new StreamReader(request.Body))
                {
                    return await reader.ReadToEndAsync();
                }
            };

            var responders = new Dictionary<string, Action<IOwinContext>>
            {
                { "/redirect-301-absolute", async context =>
                    {
                        context.Response.StatusCode = 301;
                        context.Response.ReasonPhrase = "Moved Permanently";
                        context.Response.Headers.Add("Location", new [] { "http://localhost/redirect" });
                        await readToEnd(context.Request);
                    }
                },
                { "/redirect-301-relative", async context =>
                    {
                        context.Response.StatusCode = 301;
                        context.Response.ReasonPhrase = "Moved Permanently";
                        context.Response.Headers.Add("Location", new [] { "redirect" });
                        await readToEnd(context.Request);
                    }
                },
                { "/redirect-301-absolute-setcookie", async context =>
                    {
                        context.Response.StatusCode = 302;
                        context.Response.ReasonPhrase = "Moved Permanently";
                        context.Response.Headers.Add("Location", new [] { "http://localhost/redirect" });
                        context.Response.Headers.Add("Set-Cookie", new []{ "foo=bar"});
                        await readToEnd(context.Request);
                    }
                },
                { "/redirect-302-absolute", async context =>
                    {
                        context.Response.StatusCode = 302;
                        context.Response.ReasonPhrase = "Found";
                        context.Response.Headers.Add("Location", new [] { "http://localhost/redirect" });
                        await readToEnd(context.Request);
                    }
                },
                { "/redirect-302-relative", async context =>
                    {
                        context.Response.StatusCode = 302;
                        context.Response.ReasonPhrase = "Found";
                        context.Response.Headers.Add("Location", new [] { "redirect" });
                        await readToEnd(context.Request);
                    }
                },
                { "/redirect-302-relative-setcookie", async context =>
                    {
                        context.Response.StatusCode = 302;
                        context.Response.ReasonPhrase = "Found";
                        context.Response.Headers.Add("Location", new [] { "redirect" });
                        context.Response.Headers.Add("Set-Cookie", new []{ "foo=bar"});
                        await readToEnd(context.Request);
                    }
                },
                { "/redirect-303-absolute", async context =>
                    {
                        context.Response.StatusCode = 303;
                        context.Response.ReasonPhrase = "See Other";
                        context.Response.Headers.Add("Location", new [] { "http://localhost/redirect" });
                        await readToEnd(context.Request);
                    }
                },
                { "/redirect-303-relative", async context =>
                    {
                        context.Response.StatusCode = 303;
                        context.Response.ReasonPhrase = "See Other";
                        context.Response.Headers.Add("Location", new [] { "redirect" });
                        await readToEnd(context.Request);
                    }
                },
                { "/redirect-307-absolute", async context =>
                    {
                        context.Response.StatusCode = 307;
                        context.Response.ReasonPhrase = "Temporary Redirect";
                        context.Response.Headers.Add("Location", new [] { "http://localhost/redirect" });
                        await readToEnd(context.Request);
                    }
                },
                { "/redirect-307-relative", async context =>
                    {
                        context.Response.StatusCode = 307;
                        context.Response.ReasonPhrase = "Temporary Redirect";
                        context.Response.Headers.Add("Location", new [] { "redirect" });
                        await readToEnd(context.Request);
                    }
                },
                { "/redirect-loop", async context =>
                    {
                        context.Response.StatusCode = 302;
                        context.Response.ReasonPhrase = "Found";
                        context.Response.Headers.Add("Location", new[] { "http://localhost/redirect-loop" });
                        await readToEnd(context.Request);
                    }
                },
                {
                    "/redirect", async context =>
                    {
                        context.Response.StatusCode = 200;
                        var requestBody = await readToEnd(context.Request);
                        await context.Response.WriteAsync(requestBody);
                    }
                }
            };
            AppFunc appFunc = env =>
            {
                var context = new OwinContext(env);
                responders[context.Request.Path.Value](context);
                return Task.FromResult((object)null);
            };
            _handler = new OwinHttpMessageHandler(appFunc)
            {
                AllowAutoRedirect = true
            };
        }
        public void When_changing_use_cookies_after_disposing_then_should_throw()
        {
            var handler = new OwinHttpMessageHandler(_ => Task.FromResult(0));
            handler.Dispose();

            Action act = () => { handler.UseCookies = true; };

            act.ShouldThrow<InvalidOperationException>();
        }
        public void When_changing_cookie_container_after_disposing_then_then_should_throw()
        {
            var handler = new OwinHttpMessageHandler(AppFuncHelpers.NoopAppFunc);
            handler.Dispose();

            Action act = () => { handler.CookieContainer = new CookieContainer(); };

            act.ShouldThrow<InvalidOperationException>();
        }
        public AutoRedirectTests()
        {
            Func <IOwinRequest, Task <string> > readToEnd = async request =>
            {
                using (var reader = new StreamReader(request.Body))
                {
                    return(await reader.ReadToEndAsync());
                }
            };

            var responders = new Dictionary <string, Action <IOwinContext> >
            {
                { "/redirect-301-absolute", async context =>
                  {
                      context.Response.StatusCode   = 301;
                      context.Response.ReasonPhrase = "Moved Permanently";
                      context.Response.Headers.Add("Location", new [] { "http://localhost/redirect" });
                      await readToEnd(context.Request);
                  } },
                { "/redirect-301-relative", async context =>
                  {
                      context.Response.StatusCode   = 301;
                      context.Response.ReasonPhrase = "Moved Permanently";
                      context.Response.Headers.Add("Location", new [] { "redirect" });
                      await readToEnd(context.Request);
                  } },
                { "/redirect-301-absolute-setcookie", async context =>
                  {
                      context.Response.StatusCode   = 302;
                      context.Response.ReasonPhrase = "Moved Permanently";
                      context.Response.Headers.Add("Location", new [] { "http://localhost/redirect" });
                      context.Response.Headers.Add("Set-Cookie", new [] { "foo=bar" });
                      await readToEnd(context.Request);
                  } },
                { "/redirect-302-absolute", async context =>
                  {
                      context.Response.StatusCode   = 302;
                      context.Response.ReasonPhrase = "Found";
                      context.Response.Headers.Add("Location", new [] { "http://localhost/redirect" });
                      await readToEnd(context.Request);
                  } },
                { "/redirect-302-relative", async context =>
                  {
                      context.Response.StatusCode   = 302;
                      context.Response.ReasonPhrase = "Found";
                      context.Response.Headers.Add("Location", new [] { "redirect" });
                      await readToEnd(context.Request);
                  } },
                { "/redirect-302-relative-setcookie", async context =>
                  {
                      context.Response.StatusCode   = 302;
                      context.Response.ReasonPhrase = "Found";
                      context.Response.Headers.Add("Location", new [] { "redirect" });
                      context.Response.Headers.Add("Set-Cookie", new [] { "foo=bar" });
                      await readToEnd(context.Request);
                  } },
                { "/redirect-303-absolute", async context =>
                  {
                      context.Response.StatusCode   = 303;
                      context.Response.ReasonPhrase = "See Other";
                      context.Response.Headers.Add("Location", new [] { "http://localhost/redirect" });
                      await readToEnd(context.Request);
                  } },
                { "/redirect-303-relative", async context =>
                  {
                      context.Response.StatusCode   = 303;
                      context.Response.ReasonPhrase = "See Other";
                      context.Response.Headers.Add("Location", new [] { "redirect" });
                      await readToEnd(context.Request);
                  } },
                { "/redirect-307-absolute", async context =>
                  {
                      context.Response.StatusCode   = 307;
                      context.Response.ReasonPhrase = "Temporary Redirect";
                      context.Response.Headers.Add("Location", new [] { "http://localhost/redirect" });
                      await readToEnd(context.Request);
                  } },
                { "/redirect-307-relative", async context =>
                  {
                      context.Response.StatusCode   = 307;
                      context.Response.ReasonPhrase = "Temporary Redirect";
                      context.Response.Headers.Add("Location", new [] { "redirect" });
                      await readToEnd(context.Request);
                  } },
                { "/redirect-loop", async context =>
                  {
                      context.Response.StatusCode   = 302;
                      context.Response.ReasonPhrase = "Found";
                      context.Response.Headers.Add("Location", new[] { "http://localhost/redirect-loop" });
                      await readToEnd(context.Request);
                  } },
                {
                    "/redirect", async context =>
                    {
                        context.Response.StatusCode = 200;
                        var requestBody = await readToEnd(context.Request);

                        await context.Response.WriteAsync(requestBody);
                    }
                }
            };
            AppFunc appFunc = env =>
            {
                var context = new OwinContext(env);
                responders[context.Request.Path.Value](context);
                return(Task.FromResult((object)null));
            };

            _handler = new OwinHttpMessageHandler(appFunc)
            {
                AllowAutoRedirect = true
            };
        }
 public void When_midfunc_paramater_is_null_Then_should_throw()
 {
     Action act = () => { var handler = new OwinHttpMessageHandler((MidFunc)null); };
     act.ShouldThrow<ArgumentNullException>();
 }