Created
August 30, 2012 16:56
-
-
Save derFunk/3533182 to your computer and use it in GitHub Desktop.
Cached binary serialization unit tests for ServiceStack
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Text; | |
using NUnit.Framework; | |
using ServiceStack.Plugins.ProtoBuf; | |
using ServiceStack.ServiceClient.Web; | |
using ServiceStack.Text; | |
namespace ServiceStack.WebHost.IntegrationTests.Tests | |
{ | |
[TestFixture] | |
public class BinarySerializedTests | |
{ | |
private string RandomString(int Length) | |
{ | |
var rnd = new Random(); | |
var tmp = new StringBuilder(); | |
for (Int64 i = 0; i < Length; i++) | |
{ | |
tmp.Append(Convert.ToChar(((byte)rnd.Next(254))).ToString()); | |
} | |
return tmp.ToString(); | |
} | |
[Test] | |
public void Can_call_cached_WebService_with_Protobuf() | |
{ | |
var client = new ProtoBufServiceClient(Config.ServiceStackBaseUri); | |
try | |
{ | |
var fromEmail = RandomString(32); | |
var response = client.Get<ProtoBufEmail>("/cached/protobuf/?format=x-protobuf"); | |
Console.WriteLine(response.Dump()); | |
Assert.That(response.FromAddress, Is.EqualTo(fromEmail)); | |
} | |
catch (WebServiceException webEx) | |
{ | |
Console.WriteLine(webEx.ResponseDto.Dump()); | |
Assert.Fail(webEx.Message); | |
} | |
} | |
[Test] | |
public void Can_call_WebService_with_Protobuf() | |
{ | |
//new ProtoBufServiceTests().Can_Send_ProtoBuf_request(); | |
var client = new ProtoBufServiceClient(Config.ServiceStackBaseUri); | |
try | |
{ | |
var fromEmail = RandomString(32); | |
var response = client.Get<ProtoBufEmail>("/uncached/protobuf/?format=x-protobuf"); | |
Console.WriteLine(response.Dump()); | |
Assert.That(response.FromAddress, Is.EqualTo(fromEmail)); | |
} | |
catch (WebServiceException webEx) | |
{ | |
Console.WriteLine(webEx.ResponseDto.Dump()); | |
Assert.Fail(webEx.Message); | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Runtime.Serialization; | |
using ServiceStack.CacheAccess; | |
using ServiceStack.Common; | |
using ServiceStack.OrmLite; | |
using ServiceStack.ServiceHost; | |
using ServiceStack.ServiceInterface; | |
using ServiceStack.WebHost.IntegrationTests.Tests; | |
namespace ServiceStack.WebHost.IntegrationTests.Services | |
{ | |
[DataContract] | |
[RestService("/cached/protobuf", "GET")] | |
[RestService("/cached/protobuf/{FromAddress}")] | |
public class CachedProtoBufEmail | |
{ | |
[DataMember(Order = 1)] | |
public string FromAddress { get; set; } | |
} | |
[DataContract] | |
[RestService("/uncached/protobuf", "GET")] | |
[RestService("/uncached/protobuf/{FromAddress}")] | |
public class UncachedProtoBufEmail | |
{ | |
[DataMember(Order = 1)] | |
public string FromAddress { get; set; } | |
} | |
class UncachedProtoBufEmailService : RestServiceBase<UncachedProtoBufEmail> | |
{ | |
public IDbConnectionFactory DbFactory { get; set; } | |
public ICacheClient CacheClient { get; set; } | |
public override object OnGet(UncachedProtoBufEmail request) | |
{ | |
return new ProtoBufEmail() {FromAddress = request.FromAddress}; | |
} | |
} | |
class CachedProtoBufEmailService : RestServiceBase<CachedProtoBufEmail> | |
{ | |
public IDbConnectionFactory DbFactory { get; set; } | |
public ICacheClient CacheClient { get; set; } | |
public override object OnGet(CachedProtoBufEmail request) | |
{ | |
return base.RequestContext.ToOptimizedResultUsingCache( | |
this.CacheClient, | |
UrnId.Create<ProtoBufEmail>(request.FromAddress ?? "none"), | |
() => new ProtoBufEmail() { FromAddress = request.FromAddress }); | |
} | |
} | |
} |
these tests take wrong assumptions in a few places:
var fromEmail = RandomString(32);
// You don't need to append ?format=x-protobuf since the ProtoBufServiceClient already sends the ContentType in the Request HTTP Header
var response = client.Get<ProtoBufEmail>("/cached/protobuf/?format=x-protobuf"); //You're not actually sending the random string
Assert.That(response.FromAddress, Is.EqualTo(fromEmail)); // that you're testing here
I've rewritten them to use POST's
https://github.com/ServiceStack/ServiceStack/blob/master/tests/ServiceStack.WebHost.IntegrationTests/Tests/BinarySerializedTests.cs
And changed the services to inherit from ServiceBase
so they can accept POST's as well:
https://github.com/ServiceStack/ServiceStack/blob/master/tests/ServiceStack.WebHost.IntegrationTests/Services/CachedProtoBufEmailService.cs
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Can_call_cached_WebService_with_Protobuf should fail with a protobuf deserialization exception (in streamreader, could not read submessage or so).
The caching client on server side (MemoryCacheClientin the tests) stores binary data wrongly serialized.