// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.Encodings.Web;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc.Routing;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
using Microsoft.Extensions.WebEncoders.Testing;
using Moq;
using Xunit;
namespace Microsoft.AspNetCore.Mvc.Rendering
{
///
/// Tests the 's and
/// methods.
///
public class HtmlHelperFormTest
{
// actionName, controllerName, routeValues, method, htmlAttributes
public static TheoryData BeginFormDataSet
{
get
{
return new TheoryData
{
{
null, null, null, FormMethod.Get, null
},
{
"Details", "Product", null, FormMethod.Get, null
},
{
"Details", "Product", null, FormMethod.Post, null
},
{
"Details", "Product", new { isprint = "false", showreviews = "false" }, FormMethod.Get, null
},
{
"Details", "Product", new { isprint = "false", showreviews = "true" }, FormMethod.Post, null
},
{
"Details", "Product", new { isprint = "true", showreviews = "false" }, FormMethod.Get,
new { p1_name = "p1-value" }
},
{
"Details", "Product", new { isprint = "true", showreviews = "true" }, FormMethod.Post,
new { p1_name = "p1-value" }
},
{
"Details", "Product",
new Dictionary { { "isprint", "false" }, { "showreviews", "false" }, },
FormMethod.Get,
new Dictionary { { "p1-name", "p1-value" }, { "p2-name", "p2-value" } }
},
{
"Details", "Product",
new Dictionary { { "isprint", "false" }, { "showreviews", "false" }, },
FormMethod.Post,
new Dictionary { { "p1-name", "p1-value" }, { "p2-name", "p2-value" } }
},
};
}
}
// routeName, routeValues, method, htmlAttributes
public static TheoryData BeginRouteFormDataSet
{
get
{
return new TheoryData
{
{
null, null, FormMethod.Get, null
},
{
null, null, FormMethod.Post, null
},
{
"default", null, FormMethod.Get, null
},
{
"default", null, FormMethod.Post, null
},
{
"default", new { isprint = "false", showreviews = "false" }, FormMethod.Get, null
},
{
"default", new { isprint = "false", showreviews = "true" }, FormMethod.Post, null
},
{
"default", new { isprint = "true", showreviews = "false" }, FormMethod.Get,
new { p1 = "p1-value" }
},
{
"default", new { isprint = "true", showreviews = "true" }, FormMethod.Post,
new { p1 = "p1-value" }
},
{
"default",
new Dictionary { { "isprint", "false" }, { "showreviews", "false" }, },
FormMethod.Get,
new Dictionary { { "p1-name", "p1-value" }, { "p2-name", "p2-value" } }
},
{
"default",
new Dictionary { { "isprint", "false" }, { "showreviews", "false" }, },
FormMethod.Post,
new Dictionary { { "p1-name", "p1-value" }, { "p2-name", "p2-value" } }
},
};
}
}
[Fact]
public void BeginForm_RendersExpectedValues_WithDefaultArguments()
{
// Arrange
var pathBase = "/Base";
var path = "/Path";
var queryString = "?query=string";
var expectedAction = pathBase + path + queryString;
var expectedStartTag = string.Format("", builder.ToString());
}
[Fact]
public void BeginForm_RendersExpectedValues_WithDefaultArgumentsAndHtmlAttributes()
{
// Arrange
var pathBase = "/Base";
var path = "/Path";
var queryString = "?query=string";
var expectedAction = pathBase + path + queryString;
var htmlAttributes = new { p1_name = "p1-value" };
var expectedStartTag = string.Format("", builder.ToString());
}
[Theory]
[MemberData(nameof(BeginFormDataSet))]
public void BeginForm_RendersExpectedValues(
string actionName,
string controllerName,
object routeValues,
FormMethod method,
object htmlAttributes)
{
// Arrange
var expectedAction = "http://localhost/Hello/World";
var expectedStartTag = string.Format(
"",
builder.ToString());
}
// This is an integration for the implicit antiforgery token added by BeginForm.
[Fact]
public void BeginForm_EndForm_RendersAntiforgeryToken()
{
// Arrange
var htmlGenerator = new Mock(MockBehavior.Strict);
htmlGenerator
.Setup(g => g.GenerateForm(
It.IsAny(),
It.IsAny(),
It.IsAny(),
It.IsAny