Skip to content

Commit 7f57236

Browse files
authored
Merge pull request #17 from geeklearningio/assert-helper
Make the CopyCookiesFromResponse helper method an extension method
2 parents 4c3bb91 + be08e33 commit 7f57236

File tree

4 files changed

+73
-62
lines changed

4 files changed

+73
-62
lines changed

src/GeekLearning.Test.Integration/Helpers/Http/AntiForgeryHelper.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
// http://www.stefanhendriks.com/2016/05/11/integration-testing-your-asp-net-core-app-dealing-with-anti-request-forgery-csrf-formdata-and-cookies/
88
public static class AntiForgeryHelper
99
{
10-
internal static string ExtractAntiForgeryToken(string htmlResponseText)
10+
internal static string ExtractAntiForgeryTokenCore(string htmlResponseText)
1111
{
1212
if (htmlResponseText == null) throw new ArgumentNullException("htmlResponseText");
1313

@@ -18,7 +18,7 @@ internal static string ExtractAntiForgeryToken(string htmlResponseText)
1818
public static async Task<string> ExtractAntiForgeryTokenAsync(this HttpResponseMessage response)
1919
{
2020
string responseAsString = await response.Content.ReadAsStringAsync();
21-
return await Task.FromResult(ExtractAntiForgeryToken(responseAsString));
21+
return await Task.FromResult(ExtractAntiForgeryTokenCore(responseAsString));
2222
}
2323
}
2424
}
Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
1-
namespace GeekLearning.Test.Integration.Helpers
1+
namespace System.Net.Http
22
{
33
using Microsoft.Net.Http.Headers;
44
using System.Collections.Generic;
55
using System.Linq;
6-
using System.Net.Http;
76

87
// http://www.stefanhendriks.com/2016/05/11/integration-testing-your-asp-net-core-app-dealing-with-anti-request-forgery-csrf-formdata-and-cookies/
9-
public class CookiesHelper
8+
public static class CookiesHelper
109
{
10+
public static HttpRequestMessage CopyCookiesFromResponse(this HttpRequestMessage request, HttpResponseMessage response)
11+
{
12+
return PutCookiesOnRequest(request, ExtractCookiesFromResponse(response));
13+
}
14+
1115
// Inspired from:
1216
// https://github.com/aspnet/Mvc/blob/538cd9c19121f8d3171cbfddd5d842cbb756df3e/test/Microsoft.AspNet.Mvc.FunctionalTests/TempDataTest.cs#L201-L202
13-
public static IDictionary<string, string> ExtractCookiesFromResponse(HttpResponseMessage response)
17+
private static IDictionary<string, string> ExtractCookiesFromResponse(HttpResponseMessage response)
1418
{
1519
IDictionary<string, string> result = new Dictionary<string, string>();
1620
IEnumerable<string> values;
@@ -25,7 +29,7 @@ public static IDictionary<string, string> ExtractCookiesFromResponse(HttpRespons
2529
return result;
2630
}
2731

28-
public static HttpRequestMessage PutCookiesOnRequest(HttpRequestMessage request, IDictionary<string, string> cookies)
32+
private static HttpRequestMessage PutCookiesOnRequest(HttpRequestMessage request, IDictionary<string, string> cookies)
2933
{
3034
cookies.Keys.ToList().ForEach(key =>
3135
{
@@ -34,10 +38,5 @@ public static HttpRequestMessage PutCookiesOnRequest(HttpRequestMessage request,
3438

3539
return request;
3640
}
37-
38-
public static HttpRequestMessage CopyCookiesFromResponse(HttpRequestMessage request, HttpResponseMessage response)
39-
{
40-
return PutCookiesOnRequest(request, ExtractCookiesFromResponse(response));
41-
}
4241
}
4342
}
Lines changed: 11 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,37 @@
11
namespace System.Net.Http
22
{
33
using Collections.Generic;
4-
using GeekLearning.Test.Integration.Helpers;
5-
using Globalization;
6-
using Linq;
7-
using Newtonsoft.Json.Linq;
4+
using GeekLearning.Test.Integration.Helpers.Http;
85
using System.Threading.Tasks;
96

107
public static class HttpClientHelper
118
{
129
public static async Task<HttpResponseMessage> PostAsJsonAntiForgeryAsync<TContent>(this HttpClient httpClient, string requestUri, TContent content)
1310
{
14-
IDictionary<string, string> contentData = ExtractContent(content);
15-
11+
// Get the form view
1612
HttpResponseMessage responseMsg = await httpClient.GetAsync(requestUri);
1713
if (!responseMsg.IsSuccessStatusCode)
1814
{
1915
return responseMsg;
2016
}
2117

18+
// Extract Anti Forgery Token
2219
var antiForgeryToken = await responseMsg.ExtractAntiForgeryTokenAsync();
23-
contentData.Add("__RequestVerificationToken", antiForgeryToken);
2420

21+
// Serialize data to Key/Value pairs
22+
IDictionary<string, string> contentData = content.ToKeyValue();
23+
24+
// Create the request message with previously serialized data + the Anti Forgery Token
25+
contentData.Add("__RequestVerificationToken", antiForgeryToken);
2526
var requestMsg = new HttpRequestMessage(HttpMethod.Post, requestUri)
2627
{
2728
Content = new FormUrlEncodedContent(contentData)
2829
};
2930

30-
CookiesHelper.CopyCookiesFromResponse(requestMsg, responseMsg);
31+
// Copy the cookies from the response (containing the Anti Forgery Token) to the request that is about to be sent
32+
requestMsg.CopyCookiesFromResponse(responseMsg);
3133

3234
return await httpClient.SendAsync(requestMsg);
3335
}
34-
35-
private static IDictionary<string, string> ExtractContent(object metaToken)
36-
{
37-
if (metaToken == null)
38-
{
39-
return null;
40-
}
41-
42-
JToken token = metaToken as JToken;
43-
if (token == null)
44-
{
45-
return ExtractContent(JObject.FromObject(metaToken));
46-
}
47-
48-
if (token.HasValues)
49-
{
50-
var contentData = new Dictionary<string, string>();
51-
foreach (var child in token.Children().ToList())
52-
{
53-
var childContent = ExtractContent(child);
54-
if (childContent != null)
55-
{
56-
contentData = contentData.Concat(childContent).ToDictionary(k => k.Key, v => v.Value);
57-
}
58-
}
59-
60-
return contentData;
61-
}
62-
63-
var jValue = token as JValue;
64-
if (jValue?.Value == null)
65-
{
66-
return null;
67-
}
68-
69-
var value = jValue?.Type == JTokenType.Date ?
70-
jValue?.ToString("o", CultureInfo.InvariantCulture) :
71-
jValue?.ToString(CultureInfo.InvariantCulture);
72-
73-
return new Dictionary<string, string> { { token.Path, value } };
74-
}
75-
}
36+
}
7637
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
namespace GeekLearning.Test.Integration.Helpers.Http
2+
{
3+
using Newtonsoft.Json.Linq;
4+
using System.Collections.Generic;
5+
using System.Globalization;
6+
using System.Linq;
7+
8+
public static class SerializationHelper
9+
{
10+
public static IDictionary<string, string> ToKeyValue(this object metaToken)
11+
{
12+
if (metaToken == null)
13+
{
14+
return null;
15+
}
16+
17+
JToken token = metaToken as JToken;
18+
if (token == null)
19+
{
20+
return ToKeyValue(JObject.FromObject(metaToken));
21+
}
22+
23+
if (token.HasValues)
24+
{
25+
var contentData = new Dictionary<string, string>();
26+
foreach (var child in token.Children().ToList())
27+
{
28+
var childContent = ToKeyValue(child);
29+
if (childContent != null)
30+
{
31+
contentData = contentData.Concat(childContent).ToDictionary(k => k.Key, v => v.Value);
32+
}
33+
}
34+
35+
return contentData;
36+
}
37+
38+
var jValue = token as JValue;
39+
if (jValue?.Value == null)
40+
{
41+
return null;
42+
}
43+
44+
var value = jValue?.Type == JTokenType.Date ?
45+
jValue?.ToString("o", CultureInfo.InvariantCulture) :
46+
jValue?.ToString(CultureInfo.InvariantCulture);
47+
48+
return new Dictionary<string, string> { { token.Path, value } };
49+
}
50+
}
51+
}

0 commit comments

Comments
 (0)