[Fixes #2157] Added additional functional tests for TempData
This commit is contained in:
parent
ab31be20ed
commit
9c1e2f54a5
|
|
@ -43,6 +43,12 @@ namespace Microsoft.AspNet.Mvc
|
||||||
// If we got it from Session, remove it so that no other request gets it
|
// If we got it from Session, remove it so that no other request gets it
|
||||||
session.Remove(TempDataSessionStateKey);
|
session.Remove(TempDataSessionStateKey);
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// Since we call Save() after the response has been sent, we need to initialize an empty session
|
||||||
|
// so that it is established before the headers are sent.
|
||||||
|
session[TempDataSessionStateKey] = new byte[] { };
|
||||||
|
}
|
||||||
|
|
||||||
return tempDataDictionary;
|
return tempDataDictionary;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -90,6 +90,7 @@ namespace Microsoft.AspNet.Mvc
|
||||||
if (sessionEnabled)
|
if (sessionEnabled)
|
||||||
{
|
{
|
||||||
httpContext.Setup(h => h.GetFeature<ISessionFeature>()).Returns(Mock.Of<ISessionFeature>());
|
httpContext.Setup(h => h.GetFeature<ISessionFeature>()).Returns(Mock.Of<ISessionFeature>());
|
||||||
|
httpContext.Setup(h => h.Session[It.IsAny<string>()]);
|
||||||
}
|
}
|
||||||
return httpContext.Object;
|
return httpContext.Object;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -18,6 +18,39 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
|
||||||
private readonly Action<IApplicationBuilder> _app = new TempDataWebSite.Startup().Configure;
|
private readonly Action<IApplicationBuilder> _app = new TempDataWebSite.Startup().Configure;
|
||||||
private readonly Action<IServiceCollection> _configureServices = new TempDataWebSite.Startup().ConfigureServices;
|
private readonly Action<IServiceCollection> _configureServices = new TempDataWebSite.Startup().ConfigureServices;
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task TempData_PersistsJustForNextRequest()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var server = TestHelper.CreateServer(_app, SiteName, _configureServices);
|
||||||
|
var client = server.CreateClient();
|
||||||
|
var nameValueCollection = new List<KeyValuePair<string, string>>
|
||||||
|
{
|
||||||
|
new KeyValuePair<string, string>("value", "Foo"),
|
||||||
|
};
|
||||||
|
var content = new FormUrlEncodedContent(nameValueCollection);
|
||||||
|
|
||||||
|
// Act 1
|
||||||
|
var response = await client.PostAsync("/Home/SetTempData", content);
|
||||||
|
|
||||||
|
// Assert 1
|
||||||
|
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
|
||||||
|
|
||||||
|
// Act 2
|
||||||
|
response = await client.SendAsync(GetRequest("Home/GetTempData", response));
|
||||||
|
|
||||||
|
// Assert 2
|
||||||
|
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
|
||||||
|
var body = await response.Content.ReadAsStringAsync();
|
||||||
|
Assert.Equal("Foo", body);
|
||||||
|
|
||||||
|
// Act 3
|
||||||
|
response = await client.SendAsync(GetRequest("Home/GetTempData", response));
|
||||||
|
|
||||||
|
// Assert 3
|
||||||
|
Assert.Equal(HttpStatusCode.NoContent, response.StatusCode);
|
||||||
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public async Task ViewRendersTempData()
|
public async Task ViewRendersTempData()
|
||||||
{
|
{
|
||||||
|
|
@ -38,5 +71,85 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
|
||||||
var body = await response.Content.ReadAsStringAsync();
|
var body = await response.Content.ReadAsStringAsync();
|
||||||
Assert.Equal("Foo", body);
|
Assert.Equal("Foo", body);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Redirect_RetainsTempData_EvenIfAccessed()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var server = TestHelper.CreateServer(_app, SiteName, _configureServices);
|
||||||
|
var client = server.CreateClient();
|
||||||
|
var nameValueCollection = new List<KeyValuePair<string, string>>
|
||||||
|
{
|
||||||
|
new KeyValuePair<string, string>("value", "Foo"),
|
||||||
|
};
|
||||||
|
var content = new FormUrlEncodedContent(nameValueCollection);
|
||||||
|
|
||||||
|
// Act 1
|
||||||
|
var response = await client.PostAsync("/Home/SetTempData", content);
|
||||||
|
|
||||||
|
// Assert 1
|
||||||
|
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
|
||||||
|
|
||||||
|
// Act 2
|
||||||
|
var redirectResponse = await client.SendAsync(GetRequest("/Home/GetTempDataAndRedirect", response));
|
||||||
|
|
||||||
|
// Assert 2
|
||||||
|
Assert.Equal(HttpStatusCode.Redirect, redirectResponse.StatusCode);
|
||||||
|
|
||||||
|
// Act 3
|
||||||
|
response = await client.SendAsync(GetRequest(redirectResponse.Headers.Location.ToString(), response));
|
||||||
|
|
||||||
|
// Assert 3
|
||||||
|
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
|
||||||
|
var body = await response.Content.ReadAsStringAsync();
|
||||||
|
Assert.Equal("Foo", body);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Peek_RetainsTempData()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var server = TestHelper.CreateServer(_app, SiteName, _configureServices);
|
||||||
|
var client = server.CreateClient();
|
||||||
|
var nameValueCollection = new List<KeyValuePair<string, string>>
|
||||||
|
{
|
||||||
|
new KeyValuePair<string, string>("value", "Foo"),
|
||||||
|
};
|
||||||
|
var content = new FormUrlEncodedContent(nameValueCollection);
|
||||||
|
|
||||||
|
// Act 1
|
||||||
|
var response = await client.PostAsync("/Home/SetTempData", content);
|
||||||
|
|
||||||
|
// Assert 1
|
||||||
|
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
|
||||||
|
|
||||||
|
// Act 2
|
||||||
|
var peekResponse = await client.SendAsync(GetRequest("/Home/PeekTempData", response));
|
||||||
|
|
||||||
|
// Assert 2
|
||||||
|
Assert.Equal(HttpStatusCode.OK, peekResponse.StatusCode);
|
||||||
|
var body = await peekResponse.Content.ReadAsStringAsync();
|
||||||
|
Assert.Equal("Foo", body);
|
||||||
|
|
||||||
|
// Act 3
|
||||||
|
var getResponse = await client.SendAsync(GetRequest("/Home/GetTempData", response));
|
||||||
|
|
||||||
|
// Assert 3
|
||||||
|
Assert.Equal(HttpStatusCode.OK, getResponse.StatusCode);
|
||||||
|
body = await getResponse.Content.ReadAsStringAsync();
|
||||||
|
Assert.Equal("Foo", body);
|
||||||
|
}
|
||||||
|
|
||||||
|
private HttpRequestMessage GetRequest(string path, HttpResponseMessage response)
|
||||||
|
{
|
||||||
|
var request = new HttpRequestMessage(HttpMethod.Get, path);
|
||||||
|
IEnumerable<string> values;
|
||||||
|
if (response.Headers.TryGetValues("Set-Cookie", out values))
|
||||||
|
{
|
||||||
|
request.Headers.Add("Cookie", values);
|
||||||
|
}
|
||||||
|
|
||||||
|
return request;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -17,5 +17,29 @@ namespace TempDataWebSite.Controllers
|
||||||
TempData["key"] = value;
|
TempData["key"] = value;
|
||||||
return View();
|
return View();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public IActionResult SetTempData(string value)
|
||||||
|
{
|
||||||
|
TempData["key"] = value;
|
||||||
|
return Content(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
public IActionResult GetTempDataAndRedirect()
|
||||||
|
{
|
||||||
|
var value = TempData["key"];
|
||||||
|
return RedirectToAction("GetTempData");
|
||||||
|
}
|
||||||
|
|
||||||
|
public string GetTempData()
|
||||||
|
{
|
||||||
|
var value = TempData["key"];
|
||||||
|
return value?.ToString();
|
||||||
|
}
|
||||||
|
|
||||||
|
public IActionResult PeekTempData()
|
||||||
|
{
|
||||||
|
var peekValue = TempData.Peek("key");
|
||||||
|
return Content(peekValue.ToString());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue