Skip to content

Instantly share code, notes, and snippets.

@derFunk
Created August 30, 2012 16:56
Show Gist options
  • Save derFunk/3533182 to your computer and use it in GitHub Desktop.
Save derFunk/3533182 to your computer and use it in GitHub Desktop.

Revisions

  1. derFunk created this gist Aug 30, 2012.
    70 changes: 70 additions & 0 deletions BinarySerializedTests.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,70 @@
    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);
    }
    }

    }
    }
    56 changes: 56 additions & 0 deletions CachedProtoBufEmailService.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,56 @@
    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 });
    }
    }
    }