public void CreateClient_RestClientContainerNotFoundByName_ThrowsInvalidOperationException(string name) { var serviceCollection = new ServiceCollection(); serviceCollection.AddRestClient("myclient1", "http://www.dalsoft.co.uk"); var exception = Assert.Throws <InvalidOperationException>(() => { var restClientFactory = new RestClientFactory(serviceCollection.BuildServiceProvider()); restClientFactory.CreateClient(name); }); Assert.That(exception.Message, Is.EqualTo($"No registered RestClient named: {name}")); }
public void CreateClient_MoreThanOneRestClientContainerFoundByName_ThrowsInvalidOperationException() { const string myClient = "myclient1"; var serviceCollection = new ServiceCollection(); serviceCollection.AddRestClient(myClient, "http://www.dalsoft.co.uk"); serviceCollection.AddRestClient(myClient, "http://www.dalsoft.co.uk"); var exception = Assert.Throws <InvalidOperationException>(() => { var restClientFactory = new RestClientFactory(serviceCollection.BuildServiceProvider()); restClientFactory.CreateClient(myClient); }); Assert.That(exception.Message, Is.EqualTo($"More than one registered RestClient named: {myClient}")); }
protected virtual async Task <TokenResponse> GetToken() { var restFactory = new RestClientFactory(); var restClient = restFactory.CreateClient(_authorityDetails.ClientUrl); var authRequest = restFactory.CreateRequest("token", Method.POST); authRequest.AddHeader("Content-Type", "application/x-www-form-urlencoded"); authRequest.AddHeader("Accept", "application/json"); authRequest.AddParameter("grant_type", "client_credentials"); authRequest.AddParameter("resource", _authorityDetails.ResourceUrl); restClient.Authenticator = new HttpBasicAuthenticator(_authorityDetails.ClientId, _authorityDetails.ClientSecret); var response = await restClient.ExecuteAsync <TokenResponse>(authRequest); var token = response?.Data; return(token); }
public void CreateClient_should_create_instace_of_RestClient() { var restClientFactory = new RestClientFactory(); Assert.IsInstanceOf <RestClient>(restClientFactory.CreateClient()); }