public void ThrowExceptionWhenNonHttpsSchemeUsedWithBasicAuthentication()
        {
            var serviceAgentSettings = new ServiceAgentSettings { };
            var settings = new ServiceSettings { AuthScheme = AuthScheme.Basic, BasicAuthUserName = "******", BasicAuthPassword = "******", Scheme = HttpSchema.Http, Host = "test.be", Path = "api" };
            var clientFactory = new HttpClientFactory(CreateServiceProvider(settings));

            Assert.Throws<ServiceAgentException>(() => clientFactory.CreateClient(serviceAgentSettings, settings));
        }
        public void CreateClientWithOAuthClientCredentials()
        {
            var serviceAgentSettings = new ServiceAgentSettings { };
            var settings = new ServiceSettings { AuthScheme = AuthScheme.OAuthClientCredentials, OAuthClientId = "clientId", OAuthClientSecret = "clientSecret", Host = "test.be", Path = "api" };
            var clientFactory = new HttpClientFactory(CreateServiceProvider(settings));

            var client = clientFactory.CreateClient(serviceAgentSettings, settings);

            Assert.NotNull(client);
            Assert.Equal(AuthScheme.Bearer, client.DefaultRequestHeaders.Authorization.Scheme);
            Assert.Equal("AccessToken", client.DefaultRequestHeaders.Authorization.Parameter);
        }
        public void CreateClientWithBasicAuthentication()
        {
            var serviceAgentSettings = new ServiceAgentSettings { };
            var settings = new ServiceSettings { AuthScheme = AuthScheme.Basic, BasicAuthUserName = "******", BasicAuthPassword = "******", Host = "test.be", Path = "api" };
            var clientFactory = new HttpClientFactory(CreateServiceProvider(settings));

            var client = clientFactory.CreateClient(serviceAgentSettings, settings);

            Assert.NotNull(client);
            Assert.Equal(AuthScheme.Basic, client.DefaultRequestHeaders.Authorization.Scheme);
            Assert.Equal("QWxhZGRpbjpPcGVuU2VzYW1l", client.DefaultRequestHeaders.Authorization.Parameter);
        }
        public void CreateDefaultClient()
        {
            var serviceAgentSettings = new ServiceAgentSettings();
            var settings = new ServiceSettings { Scheme = HttpSchema.Http, Host = "test.be", Path = "api" };
            var clientFactory = new HttpClientFactory(CreateServiceProvider(settings));

            var client = clientFactory.CreateClient(serviceAgentSettings, settings);

            Assert.NotNull(client);
            Assert.Equal("http://test.be/api/", client.BaseAddress.AbsoluteUri);
            Assert.Equal("application/json", client.DefaultRequestHeaders.Accept.Single().MediaType);
            Assert.Null(client.DefaultRequestHeaders.Authorization);
        }
        public void Should_return_ok_response_from_local_machine_on_get()
        {
            using (var c = new HttpClientFactory().Create())
            {
                var response = c.GetAsync("registry?serverName=YourComputerName").Result;
                var key = new JavaScriptSerializer().Deserialize<RegistryKeyModel>(response.Content.ReadAsStringAsync().Result);

                Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK));

                foreach (var subKey in key.SubKeys)
                {
                    Console.WriteLine(subKey);
                }
            }
        }
        public void Should_create_new_key_on_post()
        {
            var serverName = "YourComputerName";
            var subKey = "";
            var newKey = "justatest";

            using(var c = new HttpClientFactory().Create())
            {
                var model = new
                    {
                        serverName,
                        subKey,
                        newKey
                    };

                var responseMessage = c.PostAsJsonAsync("Registry", model).Result;

                Assert.That(responseMessage.StatusCode, Is.EqualTo(HttpStatusCode.Created));
            }
        }
        public void AfterClientCreatedGetsRaised()
        {
            var serviceAgentSettings = new ServiceAgentSettings();
            var settings = new ServiceSettings { AuthScheme = AuthScheme.Bearer, Scheme = HttpSchema.Http, Host = "test.be", Path = "api" };
            var clientFactory = new HttpClientFactory(CreateServiceProvider(settings));
            HttpClient passedClient = null;
            clientFactory.AfterClientCreated += (sp, c) => passedClient = c;

            clientFactory.CreateClient(serviceAgentSettings, settings);

            Assert.NotNull(passedClient);
        }
        public void DoesntThrowExceptionWhenNonHttpsSchemeUsedWithBasicAuthenticationInDevelopmentEnvironment()
        {
            var serviceAgentSettings = new ServiceAgentSettings { };
            var settings = new ServiceSettings { AuthScheme = AuthScheme.Basic, BasicAuthUserName = "******", BasicAuthPassword = "******", Scheme = HttpSchema.Http, Host = "test.be", Path = "api" };
            var clientFactory = new HttpClientFactory(CreateServiceProvider(settings, isDevelopmentEnvironment: true));

            clientFactory.CreateClient(serviceAgentSettings, settings);
        }
        public void CreateClientWithHeaders()
        {
            var serviceAgentSettings = new ServiceAgentSettings();
            var headers = new Dictionary<string, string>()
            {
                { "api-key", "localapikey" },
                 { "X-Custom-Header", "customvalue" },
            };
            var settings = new ServiceSettings { Headers = headers, Scheme = HttpSchema.Http, Host = "test.be", Path = "api" };
            var clientFactory = new HttpClientFactory(CreateServiceProvider(settings));

            var client = clientFactory.CreateClient(serviceAgentSettings, settings);

            Assert.NotNull(client);
            Assert.Equal("localapikey", client.DefaultRequestHeaders.First(h => h.Key == "api-key").Value.First());
            Assert.Equal("customvalue", client.DefaultRequestHeaders.First(h => h.Key == "X-Custom-Header").Value.First());
        }