Skip to content

Commit 336f424

Browse files
authored
Consistent encoding and line ending (#47)
1 parent 9228869 commit 336f424

32 files changed

+4774
-4770
lines changed

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,7 @@ Thumbs.db
1717
nuget.exe
1818
*.nupkg
1919
*TestResults/
20-
.vs/
20+
.vs/
21+
.vscode/
22+
.envrc
23+
.editorconfig
Lines changed: 143 additions & 142 deletions
Original file line numberDiff line numberDiff line change
@@ -1,142 +1,143 @@
1-
using Newtonsoft.Json;
2-
using NUnit.Framework;
3-
using System;
4-
using System.Collections.Generic;
5-
using System.Linq;
6-
using System.Text;
7-
using System.Threading.Tasks;
8-
9-
namespace stream_net_tests
10-
{
11-
[Parallelizable(ParallelScope.Self)]
12-
[TestFixture]
13-
public class ClientTests
14-
{
15-
private Stream.IStreamClient _client;
16-
17-
[SetUp]
18-
public void Setup()
19-
{
20-
_client = Credentials.Instance.Client;
21-
}
22-
23-
[Test]
24-
25-
public void TestClientArgumentsValidation()
26-
{
27-
Assert.Throws<ArgumentNullException>(() =>
28-
{
29-
var client = new Stream.StreamClient("", "asfd");
30-
});
31-
Assert.Throws<ArgumentNullException>(() =>
32-
{
33-
var client = new Stream.StreamClient("asdf", null);
34-
});
35-
}
36-
37-
[Test]
38-
39-
public void TestFeedIdValidation()
40-
{
41-
Assert.Throws<ArgumentNullException>(() =>
42-
{
43-
var feed = _client.Feed(null, null);
44-
});
45-
Assert.Throws<ArgumentNullException>(() =>
46-
{
47-
var feed = _client.Feed("flat", "");
48-
});
49-
Assert.Throws<ArgumentNullException>(() =>
50-
{
51-
var feed = _client.Feed("", "1");
52-
});
53-
Assert.Throws<ArgumentException>(() =>
54-
{
55-
var feed = _client.Feed("flat:1", "2");
56-
});
57-
Assert.Throws<ArgumentException>(() =>
58-
{
59-
var feed = _client.Feed("flat 1", "2");
60-
});
61-
Assert.Throws<ArgumentException>(() =>
62-
{
63-
var feed = _client.Feed("flat1", "2:3");
64-
});
65-
}
66-
67-
[Test]
68-
public void TestFollowFeedIdValidation()
69-
{
70-
var user1 = _client.Feed("user", "11");
71-
72-
Assert.Throws<ArgumentNullException>(() =>
73-
{
74-
user1.FollowFeed(null, null).GetAwaiter().GetResult();
75-
});
76-
Assert.Throws<ArgumentNullException>(() =>
77-
{
78-
user1.FollowFeed("flat", "").GetAwaiter().GetResult();
79-
});
80-
Assert.Throws<ArgumentNullException>(() =>
81-
{
82-
user1.FollowFeed("", "1").GetAwaiter().GetResult();
83-
});
84-
Assert.Throws<ArgumentException>(() =>
85-
{
86-
user1.FollowFeed("flat:1", "2").GetAwaiter().GetResult();
87-
});
88-
Assert.Throws<ArgumentException>(() =>
89-
{
90-
user1.FollowFeed("flat 1", "2").GetAwaiter().GetResult();
91-
});
92-
Assert.Throws<ArgumentException>(() =>
93-
{
94-
user1.FollowFeed("flat1", "2:3").GetAwaiter().GetResult();
95-
});
96-
}
97-
98-
[Test]
99-
public void TestActivityPartialUpdateArgumentValidation()
100-
{
101-
Assert.ThrowsAsync<ArgumentException>(async () =>
102-
{
103-
await _client.ActivityPartialUpdate();
104-
});
105-
Assert.ThrowsAsync<ArgumentException>(async () =>
106-
{
107-
await _client.ActivityPartialUpdate("id", new Stream.ForeignIDTime("fid", DateTime.Now));
108-
});
109-
}
110-
111-
[Test]
112-
public void TestToken()
113-
{
114-
var result = DecodeJWT(_client.CreateUserToken("user"));
115-
Assert.AreEqual("user", (string)result["user_id"]);
116-
117-
var extra = new Dictionary<string, object>()
118-
{
119-
{"client","dotnet"},
120-
{"testing", true}
121-
};
122-
result = DecodeJWT(_client.CreateUserToken("user2", extra));
123-
124-
Assert.AreEqual("user2", (string)result["user_id"]);
125-
Assert.AreEqual("dotnet", (string)result["client"]);
126-
Assert.AreEqual(true, (bool)result["testing"]);
127-
Assert.False(result.ContainsKey("missing"));
128-
}
129-
130-
private Dictionary<string, object> DecodeJWT(string token)
131-
{
132-
var segment = token.Split('.')[1];
133-
var mod = segment.Length % 4;
134-
if (mod > 0) {
135-
segment += "".PadLeft(4-mod, '=');
136-
}
137-
var encoded = Convert.FromBase64String(segment.Replace('-', '+').Replace('_', '/'));
138-
var payload = Encoding.UTF8.GetString(encoded);
139-
return JsonConvert.DeserializeObject<Dictionary<string, object>>(payload);
140-
}
141-
}
142-
}
1+
using Newtonsoft.Json;
2+
using NUnit.Framework;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using System.Text;
7+
using System.Threading.Tasks;
8+
9+
namespace stream_net_tests
10+
{
11+
[Parallelizable(ParallelScope.Self)]
12+
[TestFixture]
13+
public class ClientTests
14+
{
15+
private Stream.IStreamClient _client;
16+
17+
[SetUp]
18+
public void Setup()
19+
{
20+
_client = Credentials.Instance.Client;
21+
}
22+
23+
[Test]
24+
25+
public void TestClientArgumentsValidation()
26+
{
27+
Assert.Throws<ArgumentNullException>(() =>
28+
{
29+
var client = new Stream.StreamClient("", "asfd");
30+
});
31+
Assert.Throws<ArgumentNullException>(() =>
32+
{
33+
var client = new Stream.StreamClient("asdf", null);
34+
});
35+
}
36+
37+
[Test]
38+
39+
public void TestFeedIdValidation()
40+
{
41+
Assert.Throws<ArgumentNullException>(() =>
42+
{
43+
var feed = _client.Feed(null, null);
44+
});
45+
Assert.Throws<ArgumentNullException>(() =>
46+
{
47+
var feed = _client.Feed("flat", "");
48+
});
49+
Assert.Throws<ArgumentNullException>(() =>
50+
{
51+
var feed = _client.Feed("", "1");
52+
});
53+
Assert.Throws<ArgumentException>(() =>
54+
{
55+
var feed = _client.Feed("flat:1", "2");
56+
});
57+
Assert.Throws<ArgumentException>(() =>
58+
{
59+
var feed = _client.Feed("flat 1", "2");
60+
});
61+
Assert.Throws<ArgumentException>(() =>
62+
{
63+
var feed = _client.Feed("flat1", "2:3");
64+
});
65+
}
66+
67+
[Test]
68+
public void TestFollowFeedIdValidation()
69+
{
70+
var user1 = _client.Feed("user", "11");
71+
72+
Assert.Throws<ArgumentNullException>(() =>
73+
{
74+
user1.FollowFeed(null, null).GetAwaiter().GetResult();
75+
});
76+
Assert.Throws<ArgumentNullException>(() =>
77+
{
78+
user1.FollowFeed("flat", "").GetAwaiter().GetResult();
79+
});
80+
Assert.Throws<ArgumentNullException>(() =>
81+
{
82+
user1.FollowFeed("", "1").GetAwaiter().GetResult();
83+
});
84+
Assert.Throws<ArgumentException>(() =>
85+
{
86+
user1.FollowFeed("flat:1", "2").GetAwaiter().GetResult();
87+
});
88+
Assert.Throws<ArgumentException>(() =>
89+
{
90+
user1.FollowFeed("flat 1", "2").GetAwaiter().GetResult();
91+
});
92+
Assert.Throws<ArgumentException>(() =>
93+
{
94+
user1.FollowFeed("flat1", "2:3").GetAwaiter().GetResult();
95+
});
96+
}
97+
98+
[Test]
99+
public void TestActivityPartialUpdateArgumentValidation()
100+
{
101+
Assert.ThrowsAsync<ArgumentException>(async () =>
102+
{
103+
await _client.ActivityPartialUpdate();
104+
});
105+
Assert.ThrowsAsync<ArgumentException>(async () =>
106+
{
107+
await _client.ActivityPartialUpdate("id", new Stream.ForeignIDTime("fid", DateTime.Now));
108+
});
109+
}
110+
111+
[Test]
112+
public void TestToken()
113+
{
114+
var result = DecodeJWT(_client.CreateUserToken("user"));
115+
Assert.AreEqual("user", (string)result["user_id"]);
116+
117+
var extra = new Dictionary<string, object>()
118+
{
119+
{"client","dotnet"},
120+
{"testing", true}
121+
};
122+
result = DecodeJWT(_client.CreateUserToken("user2", extra));
123+
124+
Assert.AreEqual("user2", (string)result["user_id"]);
125+
Assert.AreEqual("dotnet", (string)result["client"]);
126+
Assert.AreEqual(true, (bool)result["testing"]);
127+
Assert.False(result.ContainsKey("missing"));
128+
}
129+
130+
private Dictionary<string, object> DecodeJWT(string token)
131+
{
132+
var segment = token.Split('.')[1];
133+
var mod = segment.Length % 4;
134+
if (mod > 0)
135+
{
136+
segment += "".PadLeft(4 - mod, '=');
137+
}
138+
var encoded = Convert.FromBase64String(segment.Replace('-', '+').Replace('_', '/'));
139+
var payload = Encoding.UTF8.GetString(encoded);
140+
return JsonConvert.DeserializeObject<Dictionary<string, object>>(payload);
141+
}
142+
}
143+
}

src/stream-net-tests/Extensions.cs

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,33 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
5-
namespace stream_net_tests
6-
{
7-
internal static class Extensions
8-
{
9-
internal static IEnumerable<T> OrEmpty<T>(this IEnumerable<T> input)
10-
{
11-
if (input == null) return Enumerable.Empty<T>();
12-
return input;
13-
}
14-
15-
internal static IEnumerable<T> Yield<T>(this T one)
16-
{
17-
yield return one;
18-
}
19-
20-
internal static void ForEach<T>(this IEnumerable<T> items, Action<T> action)
21-
{
22-
if ((items == null) || (action == null)) return; // do nothing
23-
foreach (var item in items)
24-
action(item);
25-
}
26-
27-
internal static int SafeCount<T>(this IEnumerable<T> list, int nullCountAs = 0)
28-
{
29-
if (list == null) return nullCountAs;
30-
return list.Count();
31-
}
32-
}
33-
}
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
5+
namespace stream_net_tests
6+
{
7+
internal static class Extensions
8+
{
9+
internal static IEnumerable<T> OrEmpty<T>(this IEnumerable<T> input)
10+
{
11+
if (input == null) return Enumerable.Empty<T>();
12+
return input;
13+
}
14+
15+
internal static IEnumerable<T> Yield<T>(this T one)
16+
{
17+
yield return one;
18+
}
19+
20+
internal static void ForEach<T>(this IEnumerable<T> items, Action<T> action)
21+
{
22+
if ((items == null) || (action == null)) return; // do nothing
23+
foreach (var item in items)
24+
action(item);
25+
}
26+
27+
internal static int SafeCount<T>(this IEnumerable<T> list, int nullCountAs = 0)
28+
{
29+
if (list == null) return nullCountAs;
30+
return list.Count();
31+
}
32+
}
33+
}

0 commit comments

Comments
 (0)