|
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 | +} |
0 commit comments