Skip to content

Commit 66287c0

Browse files
miya789mjcheetham
andcommitted
http: add tests about CurlCookie.cs
based on the review Co-authored-by: Matthew John Cheetham <[email protected]>
1 parent 8d7675a commit 66287c0

File tree

1 file changed

+134
-0
lines changed

1 file changed

+134
-0
lines changed
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Net;
4+
using GitCredentialManager;
5+
using GitCredentialManager.Tests.Objects;
6+
using CurlCookie;
7+
using Xunit;
8+
9+
namespace Core.Tests;
10+
11+
public class CurlCookieParserTests
12+
{
13+
[Fact]
14+
public void CurlCookieParser_EmptyFile_ReturnsNoCookies()
15+
{
16+
const string content = "";
17+
18+
var trace = new NullTrace();
19+
var parser = new CurlCookieParser(trace);
20+
21+
IList<Cookie> actual = parser.Parse(content);
22+
23+
Assert.Empty(actual);
24+
}
25+
26+
[Fact]
27+
public void CurlCookieParser_Parse_MissingFields_SkipsInvalidLines()
28+
{
29+
const string content =
30+
// Valid cookie
31+
".example.com\tTRUE\t/path/here\tTRUE\t0\tcookie1\tvalue1\n" +
32+
33+
// Missing several fields - not a valid cookie so should be skipped
34+
".example.com\tTRUE\tTRUE\tcookie1\tvalue1\n";
35+
36+
var trace = new NullTrace();
37+
var parser = new CurlCookieParser(trace);
38+
39+
IList<Cookie> actual = parser.Parse(content);
40+
41+
Assert.Equal(1, actual.Count);
42+
AssertCookie(actual[0], ".example.com", "/path/here", true, 0, "cookie1", "value1");
43+
}
44+
45+
[Fact]
46+
public void CurlCookieParser_Parse_MissingFields_ReturnsValidCookiesWithDefaults()
47+
{
48+
const string content =
49+
// Empty path field (default "/")
50+
".example.com\tTRUE\t\tTRUE\t852852\tcookie1\tvalue1\n" +
51+
52+
// Empty expiration field (default 0)
53+
".example.com\tTRUE\t/path/here\tTRUE\t\tcookie1\tvalue1";
54+
55+
var trace = new NullTrace();
56+
var parser = new CurlCookieParser(trace);
57+
58+
IList<Cookie> actual = parser.Parse(content);
59+
60+
Assert.Equal(2, actual.Count);
61+
AssertCookie(actual[0], ".example.com", "/", true, 852852, "cookie1", "value1");
62+
AssertCookie(actual[1], ".example.com", "/path/here", true, 0, "cookie1", "value1");
63+
}
64+
65+
[Fact]
66+
public void CurlCookieParser_Parse_ValidFields_ReturnsValidCookies()
67+
{
68+
const string content =
69+
".example.com\tTRUE\t/path\tTRUE\t0\tcookie1\tvalue1\n" +
70+
".example.com\tfAlSe\t/path\ttRuE\t0\tcookie1\tvalue1\n" +
71+
".example.com\tTRUE\t/path\tTRUE\t0\tcookie1 with spaces\tvalue1 with spaces\n" +
72+
".example.com\tFALSE\t/path\tTRUE\t0\tcookie1\tvalue1\n" +
73+
"example.com\tFALSE\t/path\tTRUE\t0\tcookie1\tvalue1\n" +
74+
"example.com\tTRUE\t/path\tTRUE\t0\tcookie1\tvalue1\n" +
75+
".example.com\tTRUE\t/path\tFALSE\t0\tcookie1\tvalue1\n" +
76+
".example.com\tTRUE\t/path\tFALSE\t401654\tcookie1\tvalue1\n";
77+
78+
var trace = new NullTrace();
79+
var parser = new CurlCookieParser(trace);
80+
81+
IList<Cookie> actual = parser.Parse(content);
82+
83+
Assert.Equal(8, actual.Count);
84+
AssertCookie(actual[0], ".example.com", "/path", true, 0, "cookie1", "value1");
85+
AssertCookie(actual[1], "example.com", "/path", true, 0, "cookie1", "value1");
86+
AssertCookie(actual[2], ".example.com", "/path", true, 0, "cookie1 with spaces", "value1 with spaces");
87+
AssertCookie(actual[3], "example.com", "/path", true, 0, "cookie1", "value1");
88+
AssertCookie(actual[4], "example.com", "/path", true, 0, "cookie1", "value1");
89+
AssertCookie(actual[5], "example.com", "/path", true, 0, "cookie1", "value1");
90+
AssertCookie(actual[6], ".example.com", "/path", false, 0, "cookie1", "value1");
91+
AssertCookie(actual[7], ".example.com", "/path", false, 401654, "cookie1", "value1");
92+
}
93+
94+
[Fact]
95+
public void CurlCookieParser_Parse_Comments_ReturnsCookies()
96+
{
97+
const string content =
98+
// Comment block
99+
"# This is a cookie file with various comments!\n" +
100+
"# Lines starting with # are comments, except those that\n" +
101+
"# start with exactly '#HttpOnly_'.. two #s is a comment still!\n" +
102+
103+
// This is still a comment!
104+
"##HttpOnly_ <-- this is a comment still!\n" +
105+
106+
// Valid line
107+
".example.com\tTRUE\t/\tTRUE\t0\tcookie1\tvalue1\n" +
108+
109+
// Commented out cookie line
110+
"#.example.com\tTRUE\t/\tTRUE\t0\tcookie1\tvalue1\n" +
111+
112+
// Valid cookie but HTTP only
113+
"#HttpOnly_.example.com\tTRUE\t/\tTRUE\t0\tcookie1\tvalue1\n";
114+
115+
var trace = new NullTrace();
116+
var parser = new CurlCookieParser(trace);
117+
118+
IList<Cookie> actual = parser.Parse(content);
119+
120+
Assert.Equal(2, actual.Count);
121+
AssertCookie(actual[0], ".example.com", "/", true, 0, "cookie1", "value1");
122+
AssertCookie(actual[1], ".example.com", "/", true, 0, "cookie1", "value1");
123+
}
124+
125+
private static void AssertCookie(Cookie cookie, string domain, string path, bool secureOnly, long expires, string name, string value)
126+
{
127+
Assert.Equal(name, cookie.Name);
128+
Assert.Equal(value, cookie.Value);
129+
Assert.Equal(domain, cookie.Domain);
130+
Assert.Equal(path, cookie.Path);
131+
Assert.Equal(secureOnly, cookie.Secure);
132+
Assert.Equal(expires, cookie.Expires.Subtract(DateTime.UnixEpoch).TotalSeconds);
133+
}
134+
}

0 commit comments

Comments
 (0)