Esempio n. 1
0
        public async Task Create_WhenPOSTExecuted_ReturnsToIndexView()
        {
            var initResponse = await _client.GetAsync("/Movies/Create");

            var antiForgeryValues = await AntiForgeryTokenExtractor.ExtractAntiForgeryValues(initResponse);

            var postRequest = new HttpRequestMessage(HttpMethod.Post, "/Movies/Create");

            postRequest.Headers.Add("Cookie", new CookieHeaderValue(AntiForgeryTokenExtractor.AntiForgeryCookieName, antiForgeryValues.cookieValue).ToString());

            var modelData = new Dictionary <string, string>
            {
                { AntiForgeryTokenExtractor.AntiForgeryFieldName, antiForgeryValues.fieldValue },
                { "Name", "New Movie" },
                { "ReleasedYear", "2021" },
                { "Director", "Movie Director" },
                { "ImdbRating", "9.5" },
            };

            postRequest.Content = new FormUrlEncodedContent(modelData);

            var response = await _client.SendAsync(postRequest);

            response.EnsureSuccessStatusCode();

            var responseString = await response.Content.ReadAsStringAsync();

            Assert.Contains("New Movie", responseString);
            Assert.Contains("Movie Director", responseString);
        }
Esempio n. 2
0
        public async Task Create_SentWrongModel_ReturnsViewWithErrorMessages()
        {
            var initResponse = await _client.GetAsync("/Movies/Create");

            var antiForgeryValues = await AntiForgeryTokenExtractor.ExtractAntiForgeryValues(initResponse);

            var postRequest = new HttpRequestMessage(HttpMethod.Post, "/Movies/Create");

            postRequest.Headers.Add("Cookie", new CookieHeaderValue(AntiForgeryTokenExtractor.AntiForgeryCookieName, antiForgeryValues.cookieValue).ToString());

            var formModel = new Dictionary <string, string>
            {
                {
                    AntiForgeryTokenExtractor.AntiForgeryFieldName,
                    antiForgeryValues.fieldValue
                },
                { "Name", "New Movie" },
                { "ReleasedYear", "2019" },
            };

            postRequest.Content = new FormUrlEncodedContent(formModel);

            var response = await _client.SendAsync(postRequest);

            response.EnsureSuccessStatusCode();
            var responseString = await response.Content.ReadAsStringAsync();

            Assert.Contains("ImdbRating is required", responseString);
        }