示例#1
0
        public async Task CreateArticleAndRedirect()
        {
            var createPage = await _client.GetAsync("MicroArticles/Create");

            var antiForgeryToken = await AntiForgeryHelper.ExtractAntiForgeryToken(createPage);

            var content = await HtmlHelpers.GetDocumentAsync(createPage);

            var name        = "Test";
            var body        = "Test";
            var imageAdress = "http://localhost/image3.jpg";

            //CreateArticle
            var response = await _client.PostAsync("MicroArticles/Create", new FormUrlEncodedContent(
                                                       new Dictionary <string, string> {
                { "__RequestVerificationToken", antiForgeryToken },
                { "Name", name },
                { "Body", body },
                { "ImageAddress", imageAdress }
            })
                                                   );


            Assert.AreEqual(HttpStatusCode.OK, createPage.StatusCode);
            Assert.AreEqual(HttpStatusCode.Redirect, response.StatusCode);
            Assert.AreEqual("/", response.Headers.Location.OriginalString);
        }
        private static async Task <HttpResponseMessage> SignIn(HttpClient client)
        {
            var getLoginResponse = await client.GetAsync("/Identity/Account/Login");

            getLoginResponse.EnsureSuccessStatusCode();
            var loginAntiForgeryToken = await AntiForgeryHelper.ExtractAntiForgeryToken(getLoginResponse);

            var loginContent = new FormUrlEncodedContent(new Dictionary <string, string> {
                { "__RequestVerificationToken", loginAntiForgeryToken },
                { "Input.Email", "*****@*****.**" },
                { "Input.Password", "HelloSatnam123" }
            }
                                                         );

            var postLoginResponse = await client.PostAsync("/Identity/Account/Login", loginContent);

            return(postLoginResponse);
        }
        public async Task Get_EndpointsReturnSuccessAndCorrectContentType()
        {
            var client = _appFactory
                         .WithWebHostBuilder(builder => builder.ConfigureTestServices(x => {
                //x.AddTransient<ISomeThinger, TestSomeThinger>();
            }))
                         .CreateClient();

            //Register
            var getRegisterResponse = await client.GetAsync("/Identity/Account/Register");

            getRegisterResponse.EnsureSuccessStatusCode();
            var registerAntiForgeryToken = await AntiForgeryHelper.ExtractAntiForgeryToken(getRegisterResponse);

            var registerContent = new FormUrlEncodedContent(new Dictionary <string, string> {
                { "__RequestVerificationToken", registerAntiForgeryToken },
                { "Input.Email", "*****@*****.**" },
                { "Input.Password", "HelloSatnam123!" },
                { "Input.ConfirmPassword", "HelloSatnam123!" }
            }
                                                            );
            var postRegisterResponse = await client.PostAsync("/Identity/Account/Register", registerContent);

            //Sign-in
            HttpResponseMessage postLoginResponse1 = await SignIn(client);

            HttpResponseMessage postLoginResponse2 = await SignIn(client);

            HttpResponseMessage postLoginResponse3 = await SignIn(client);

            HttpResponseMessage postLoginResponse4 = await SignIn(client);

            HttpResponseMessage postLoginResponse5 = await SignIn(client);

            //Verify lockout
            Assert.EndsWith("Lockout", postLoginResponse5.RequestMessage.RequestUri.AbsolutePath);

            //// Assert
            //postLoginResponse.EnsureSuccessStatusCode(); // Status Code 200-299
            //Assert.Equal("text/html; charset=utf-8",
            //    postRegisterResponse.Content.Headers.ContentType.ToString());
        }