Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit 2be9f01

Browse files
committed
Add generic typed string escaping tests
1 parent 24a970c commit 2be9f01

2 files changed

Lines changed: 40 additions & 36 deletions

File tree

src/ServiceStack.Redis/Generic/RedisTypedClient.cs

Lines changed: 11 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,9 @@ static RedisTypedClient()
3636
readonly ITypeSerializer<T> serializer = new JsonSerializer<T>();
3737
private readonly RedisClient client;
3838

39-
public IRedisClient RedisClient
40-
{
41-
get { return client; }
42-
}
39+
public IRedisClient RedisClient => client;
4340

44-
public IRedisNativeClient NativeClient
45-
{
46-
get { return client; }
47-
}
41+
public IRedisNativeClient NativeClient => client;
4842

4943
/// <summary>
5044
/// Use this to share the same redis connection with another
@@ -88,26 +82,14 @@ public IDisposable AcquireLock(TimeSpan timeOut)
8882

8983
public IRedisTransactionBase Transaction
9084
{
91-
get
92-
{
93-
return client.Transaction;
94-
}
95-
set
96-
{
97-
client.Transaction = value;
98-
}
85+
get => client.Transaction;
86+
set => client.Transaction = value;
9987
}
10088

10189
public IRedisPipelineShared Pipeline
10290
{
103-
get
104-
{
105-
return client.Pipeline;
106-
}
107-
set
108-
{
109-
client.Pipeline = value;
110-
}
91+
get => client.Pipeline;
92+
set => client.Pipeline = value;
11193
}
11294

11395
public void Watch(params string[] keys)
@@ -155,18 +137,12 @@ public string UrnKey(T entity)
155137
return client.UrnKey(entity);
156138
}
157139

158-
public IRedisSet TypeIdsSet
159-
{
160-
get
161-
{
162-
return new RedisClientSet(client, client.GetTypeIdsSetKey<T>());
163-
}
164-
}
140+
public IRedisSet TypeIdsSet => new RedisClientSet(client, client.GetTypeIdsSetKey<T>());
165141

166142
public T this[string key]
167143
{
168-
get { return GetValue(key); }
169-
set { SetValue(key, value); }
144+
get => GetValue(key);
145+
set => SetValue(key, value);
170146
}
171147

172148
public byte[] SerializeValue(T value)
@@ -184,7 +160,7 @@ public T DeserializeValue(byte[] value)
184160
public void SetValue(string key, T entity)
185161
{
186162
if (key == null)
187-
throw new ArgumentNullException("key");
163+
throw new ArgumentNullException(nameof(key));
188164

189165
client.Set(key, SerializeValue(entity));
190166
client.RegisterTypeId(entity);
@@ -193,7 +169,7 @@ public void SetValue(string key, T entity)
193169
public void SetValue(string key, T entity, TimeSpan expireIn)
194170
{
195171
if (key == null)
196-
throw new ArgumentNullException("key");
172+
throw new ArgumentNullException(nameof(key));
197173

198174
client.Set(key, SerializeValue(entity), expireIn);
199175
client.RegisterTypeId(entity);

tests/ServiceStack.Redis.Tests/Generic/RedisTypedClientAppTests.cs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System.Collections.Generic;
22
using NUnit.Framework;
3-
using ServiceStack.Common;
43
using ServiceStack.Redis.Generic;
54

65
namespace ServiceStack.Redis.Tests.Generic
@@ -229,5 +228,34 @@ public void Can_GetEarliestFromRecentsList()
229228
Assert.That(expectedAnswers.EquivalentTo(earliest3Answers));
230229
}
231230

231+
[Test]
232+
public void Can_save_quoted_strings()
233+
{
234+
var str = "string \"with\" \"quotes\"";
235+
var cacheKey = "quotetest";
236+
237+
Redis.As<string>().SetValue(cacheKey, str);
238+
var fromRedis = Redis.As<string>().GetValue(cacheKey);
239+
Assert.That(fromRedis, Is.EqualTo(str));
240+
241+
Redis.Set(cacheKey, str);
242+
fromRedis = Redis.Get<string>(cacheKey);
243+
Assert.That(fromRedis, Is.EqualTo(str));
244+
245+
Redis.SetValue(cacheKey, str);
246+
fromRedis = Redis.GetValue(cacheKey);
247+
Assert.That(fromRedis, Is.EqualTo(str));
248+
249+
Redis.SetValue(cacheKey, str.ToJson());
250+
fromRedis = Redis.GetValue(cacheKey).FromJson<string>();
251+
Assert.That(fromRedis, Is.EqualTo(str));
252+
}
253+
254+
[Test]
255+
public void Does_return_non_existent_keys_as_defaultValue()
256+
{
257+
Assert.That(Redis.Get<string>("notexists"), Is.Null);
258+
Assert.That(Redis.Get<int>("notexists"), Is.EqualTo(0));
259+
}
232260
}
233261
}

0 commit comments

Comments
 (0)