Skip to content

Commit

Permalink
Added functional tests for Antiforgery related to setting no-cache he…
Browse files Browse the repository at this point in the history
…aders
  • Loading branch information
kichalla committed Nov 2, 2016
1 parent c28ad48 commit 82b2e9c
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
30 changes: 30 additions & 0 deletions test/Microsoft.AspNetCore.Mvc.FunctionalTests/AntiforgeryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ public async Task MultipleAFTokensWithinTheSamePage_GeneratesASingleCookieToken(
// Even though there are two forms there should only be one response cookie,
// as for the second form, the cookie from the first token should be reused.
Assert.Single(setCookieHeader);

Assert.True(response.Headers.CacheControl.NoCache);
var pragmaValue = Assert.Single(response.Headers.Pragma.ToArray());
Assert.Equal("no-cache", pragmaValue.Name);
}

[Fact]
Expand Down Expand Up @@ -84,6 +88,10 @@ public async Task SetCookieAndHeaderBeforeFlushAsync_GeneratesCookieTokenAndHead

var setCookieHeader = response.Headers.GetValues("Set-Cookie").ToArray();
Assert.Single(setCookieHeader);

Assert.True(response.Headers.CacheControl.NoCache);
var pragmaValue = Assert.Single(response.Headers.Pragma.ToArray());
Assert.Equal("no-cache", pragmaValue.Name);
}

[Fact]
Expand Down Expand Up @@ -145,5 +153,27 @@ public async Task Antiforgery_HeaderNotSet_SendsBadRequest()
// Assert
Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode);
}

[Fact]
public async Task AntiforgeryTokenGeneration_SetsDoNotCacheHeaders_OverridesExistingCachingHeaders()
{
// Arrange & Act
var response = await Client.GetAsync("http://localhost/Antiforgery/AntiforgeryTokenAndResponseCaching");

// Assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
var header = Assert.Single(response.Headers.GetValues("X-Frame-Options"));
Assert.Equal("SAMEORIGIN", header);

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

// Even though there are two forms there should only be one response cookie,
// as for the second form, the cookie from the first token should be reused.
Assert.Single(setCookieHeader);

Assert.True(response.Headers.CacheControl.NoCache);
var pragmaValue = Assert.Single(response.Headers.Pragma.ToArray());
Assert.Equal("no-cache", pragmaValue.Name);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,13 @@ public string FlushAsyncLogin(LoginViewModel model)
{
return "OK";
}

[HttpGet]
[AllowAnonymous]
[ResponseCache(Duration = 60)]
public ActionResult AntiforgeryTokenAndResponseCaching()
{
return View();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

@{
ViewData["Title"] = "Antiforgery token and response caching";
}

<h2>@ViewData["Title"]</h2>

@using (Html.BeginForm("Login", "Antiforgery", FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
{
<label>Name</label>
<input type="text" name="Name" />
<input type="submit" />
}

0 comments on commit 82b2e9c

Please sign in to comment.