public AblySimpleRestClient(AblyOptions options, IAblyHttpClient httpClient)
 {
     _options        = options;
     _httpClient     = httpClient;
     _protocol       = _options.UseBinaryProtocol == false ? Protocol.Json : Protocol.MsgPack;
     _messageHandler = new MessageHandler(_protocol);
 }
Esempio n. 2
0
        public void ClientWithExistingTokenReusesItForMakingRequests()
        {
            var options = new AblyOptions
            {
                ClientId          = "test",
                Key               = "best",
                UseBinaryProtocol = false
            };
            var rest  = new RestClient(options);
            var token = new TokenDetails("123")
            {
                Expires = DateTimeOffset.UtcNow.AddHours(1)
            };

            rest.CurrentToken = token;

            rest.ExecuteHttpRequest = request =>
            {
                //Assert
                request.Headers["Authorization"].Should().Contain(token.Token.ToBase64());
                return(new AblyResponse()
                {
                    TextResponse = "[{}]"
                });
            };

            rest.Stats();
            rest.Stats();
            rest.Stats();
        }
Esempio n. 3
0
        public void ClientWithExpiredTokenAutomaticallyCreatesANewOne()
        {
            Config.Now = () => DateTimeOffset.UtcNow;
            var newTokenRequested = false;
            var options           = new AblyOptions
            {
                AuthCallback = (x) => {
                    Console.WriteLine("Getting new token.");
                    newTokenRequested = true; return(new TokenDetails("new.token")
                    {
                        Expires = DateTimeOffset.UtcNow.AddDays(1)
                    });
                },
                UseBinaryProtocol = false
            };
            var rest = new RestClient(options);

            rest.ExecuteHttpRequest = request =>
            {
                Console.WriteLine("Getting an AblyResponse.");
                return(new AblyResponse()
                {
                    TextResponse = "[{}]"
                });
            };
            rest.CurrentToken = new TokenDetails()
            {
                Expires = DateTimeOffset.UtcNow.AddDays(-2)
            };

            Console.WriteLine("Current time:" + Config.Now());
            rest.Stats();
            newTokenRequested.Should().BeTrue();
            rest.CurrentToken.Token.Should().Be("new.token");
        }
        private static string GetHost(AblyOptions options)
        {
            if (options.Host.IsNotEmpty())
            {
                return(options.Host);
            }

            return(Config.DefaultHost);
        }
Esempio n. 5
0
        public void Init_WithKeyNoClientIdAndAuthTokenId_SetsCurrentTokenWithSuppliedId()
        {
            AblyOptions options = new AblyOptions {
                Key = ValidKey, ClientId = "123", Token = "222"
            };
            var client = new RestClient(options);

            Assert.Equal(options.Token, client.CurrentToken.Token);
        }
Esempio n. 6
0
        public AblyOptions CreateOptions(string key)
        {
            var opts = new AblyOptions()
            {
                Key = key
            };

            FillInOptions(opts);
            return(opts);
        }
        private RestClient GetAbly()
        {
            var testData = TestsSetup.TestData;

            var options = new AblyOptions
            {
                Key = testData.keys.First().keyStr,
                UseBinaryProtocol = _protocol == Protocol.MsgPack,
                Environment       = AblyEnvironment.Sandbox
            };
            var ably = new RestClient(options);

            return(ably);
        }
Esempio n. 8
0
        public void Init_WithAuthUrl_CallsTheUrlOnFirstRequest()
        {
            bool called  = false;
            var  options = new AblyOptions
            {
                AuthUrl           = "http://testUrl",
                UseBinaryProtocol = false
            };

            var rest = new RestClient(options);

            rest.ExecuteHttpRequest = request =>
            {
                if (request.Url.Contains(options.AuthUrl))
                {
                    called = true;
                    return(new AblyResponse()
                    {
                        TextResponse = "{}"
                    });
                }

                if (request.Url.Contains("requestToken"))
                {
                    return(new AblyResponse {
                        TextResponse = "{ \"access_token\": { \"expires\": \"" + DateTimeOffset.UtcNow.AddHours(1).ToUnixTimeInMilliseconds() + "\"}}"
                    });
                }

                return(new AblyResponse()
                {
                    TextResponse = "[{}]"
                });
            };

            rest.Stats();

            Assert.True(called, "Rest with Callback needs to request token using callback");
        }
Esempio n. 9
0
        public void Init_WithCallback_ExecutesCallbackOnFirstRequest()
        {
            bool called  = false;
            var  options = new AblyOptions
            {
                AuthCallback      = (x) => { called = true; return(new TokenDetails()
                    {
                        Expires = DateTimeOffset.UtcNow.AddHours(1)
                    }); },
                UseBinaryProtocol = false
            };

            var rest = new RestClient(options);

            rest.ExecuteHttpRequest = delegate { return(new AblyResponse()
                {
                    TextResponse = "[{}]"
                }); };

            rest.Stats();

            Assert.True(called, "Rest with Callback needs to request token using callback");
        }
Esempio n. 10
0
 public AblySimpleRestClient(AblyOptions options)
     : this(options, new AblyHttpClient(GetHost(options), options.Port, options.Tls, options.Environment))
 {
 }
Esempio n. 11
0
 public void FillInOptions(AblyOptions opts)
 {
     opts.Host = restHost;
     opts.Port = restPort;
     opts.Tls  = tls;
 }