public void TestDefaultMimeType()
        {
            ExampleHttpServer server = new ExampleHttpServer(new ExampleSearchImpl());

            //obviously if this was a 'real' transport we would not use the server, rather the server would be listening, the client transmitting
            IHttpTransfer wire = server;


            MemoryStream input  = new MemoryStream(new SearchRequest.Builder().AddCriteria("Test").Build().ToByteArray());
            MemoryStream output = new MemoryStream();

            //With this default set, any invalid/unknown mime-type will be mapped to use that format
            server.Options.DefaultContentType = MessageFormatOptions.ContentTypeProtoBuffer;

            wire.Execute("Search",
                         "foo", input,
                         "bar", output
                         );

            SearchResponse result = SearchResponse.ParseFrom(output.ToArray());

            Assert.AreEqual(1, result.ResultsCount);
            Assert.AreEqual("Test", result.ResultsList[0].Name);
            Assert.AreEqual("http://search.com", result.ResultsList[0].Url);
        }
        public void TestClientServerWithProtoFormat()
        {
            ExampleHttpServer server = new ExampleHttpServer(new ExampleSearchImpl());
            //obviously if this was a 'real' transport we would not use the server, rather the server would be listening, the client transmitting
            IHttpTransfer wire = server;

            ISearchService client = new SearchService(new ExampleClient(wire, "application/x-protobuf"));
            //now the client has a real, typed, interface to work with:
            SearchResponse result = client.Search(SearchRequest.CreateBuilder().AddCriteria("Test").Build());

            Assert.AreEqual(1, result.ResultsCount);
            Assert.AreEqual("Test", result.ResultsList[0].Name);
            Assert.AreEqual("http://search.com", result.ResultsList[0].Url);

            //The test part of this, call the only other method
            result =
                client.RefineSearch(
                    RefineSearchRequest.CreateBuilder().SetPreviousResults(result).AddCriteria("Refine").Build());
            Assert.AreEqual(2, result.ResultsCount);
            Assert.AreEqual("Test", result.ResultsList[0].Name);
            Assert.AreEqual("http://search.com", result.ResultsList[0].Url);

            Assert.AreEqual("Refine", result.ResultsList[1].Name);
            Assert.AreEqual("http://refine.com", result.ResultsList[1].Url);
        }
        public void TestInvalidMimeType()
        {
            ExampleHttpServer server = new ExampleHttpServer(new ExampleSearchImpl());
            //obviously if this was a 'real' transport we would not use the server, rather the server would be listening, the client transmitting
            IHttpTransfer wire = server;

            MemoryStream input  = new MemoryStream();
            MemoryStream output = new MemoryStream();

            //Call the server
            wire.Execute("Search",
                         "bad/mime", input,
                         MessageFormatOptions.ContentTypeProtoBuffer, output
                         );
            Assert.Fail();
        }
        public void TestClientServerWithCustomFormat()
        {
            ExampleHttpServer server = new ExampleHttpServer(new ExampleSearchImpl());

            //Setup our custom mime-type format as the only format supported:
            server.Options.MimeInputTypes.Clear();
            server.Options.MimeInputTypes.Add("foo/bar", CodedInputStream.CreateInstance);
            server.Options.MimeOutputTypes.Clear();
            server.Options.MimeOutputTypes.Add("foo/bar", CodedOutputStream.CreateInstance);

            //obviously if this was a 'real' transport we would not use the server, rather the server would be listening, the client transmitting
            IHttpTransfer wire = server;

            ExampleClient exclient = new ExampleClient(wire, "foo/bar");

            //Add our custom mime-type format
            exclient.Options.MimeInputTypes.Add("foo/bar", CodedInputStream.CreateInstance);
            exclient.Options.MimeOutputTypes.Add("foo/bar", CodedOutputStream.CreateInstance);
            ISearchService client = new SearchService(exclient);

            //now the client has a real, typed, interface to work with:
            SearchResponse result = client.Search(SearchRequest.CreateBuilder().AddCriteria("Test").Build());

            Assert.AreEqual(1, result.ResultsCount);
            Assert.AreEqual("Test", result.ResultsList[0].Name);
            Assert.AreEqual("http://search.com", result.ResultsList[0].Url);

            //The test part of this, call the only other method
            result =
                client.RefineSearch(
                    RefineSearchRequest.CreateBuilder().SetPreviousResults(result).AddCriteria("Refine").Build());
            Assert.AreEqual(2, result.ResultsCount);
            Assert.AreEqual("Test", result.ResultsList[0].Name);
            Assert.AreEqual("http://search.com", result.ResultsList[0].Url);

            Assert.AreEqual("Refine", result.ResultsList[1].Name);
            Assert.AreEqual("http://refine.com", result.ResultsList[1].Url);
        }
        public void TestServerWithUriFormat()
        {
            ExampleHttpServer server = new ExampleHttpServer(new ExampleSearchImpl());
            //obviously if this was a 'real' transport we would not use the server, rather the server would be listening, the client transmitting
            IHttpTransfer wire = server;

            MemoryStream input  = new MemoryStream(Encoding.UTF8.GetBytes("?Criteria=Test&Criteria=Test+of%20URI"));
            MemoryStream output = new MemoryStream();

            //Call the server
            wire.Execute("Search",
                         MessageFormatOptions.ContentFormUrlEncoded, input,
                         MessageFormatOptions.ContentTypeProtoBuffer, output
                         );

            SearchResponse result = SearchResponse.ParseFrom(output.ToArray());

            Assert.AreEqual(2, result.ResultsCount);
            Assert.AreEqual("Test", result.ResultsList[0].Name);
            Assert.AreEqual("http://search.com", result.ResultsList[0].Url);

            Assert.AreEqual("Test of URI", result.ResultsList[1].Name);
            Assert.AreEqual("http://search.com", result.ResultsList[1].Url);
        }