/// <summary> /// Create a connection to the Nfield server on the specified <paramref name="nfieldServerUri"/>. /// </summary> /// <param name="nfieldServerUri">The url for the Nfield API service</param> /// <param name="httpClient">The http client to use for the connection</param> public static INfieldConnectionV2 Create(Uri nfieldServerUri, HttpClient httpClient) { var connection = new NfieldConnection(httpClient); var url = nfieldServerUri.ToString().TrimEnd('/') + "/"; connection.NfieldServerUri = new Uri(url); return(connection); }
public void CreateCreatesAnNfieldConnection() { var expectedConnection = new NfieldConnection(); var expectedUri = new Uri("http://fake/"); DependencyResolver.Register(type => expectedConnection, type => null); var actualConnection = NfieldConnectionFactory.Create(expectedUri); Assert.Same(expectedConnection, actualConnection); Assert.Equal(expectedUri, actualConnection.NfieldServerUri); }
public void TestGetService_ServiceDoesNotExist_ReturnsNull() { var mockedResolver = new Mock <IDependencyResolver>(); DependencyResolver.Register(mockedResolver.Object); mockedResolver .Setup(resolver => resolver.Resolve(typeof(INfieldConnectionClientObject))) .Returns(null); var target = new NfieldConnection(); var result = target.GetService <INfieldConnectionClientObject>(); Assert.Null(result); }
public void TestGetService_ServiceExistsAndImplementsINfieldConnectionClientObject_CallsInitializeConnectionOnService() { var mockedNfieldConnectionClientObject = new Mock <INfieldConnectionClientObject>(); var mockedResolver = new Mock <IDependencyResolver>(); DependencyResolver.Register(mockedResolver.Object); mockedResolver .Setup(resolver => resolver.Resolve(typeof(INfieldConnectionClientObject))) .Returns(mockedNfieldConnectionClientObject.Object); var target = new NfieldConnection(); var result = target.GetService <INfieldConnectionClientObject>(); mockedNfieldConnectionClientObject.Verify(client => client.InitializeNfieldConnection(target)); }
public void TestGetService_ServiceExists_ReturnsService() { var stubbedNfieldConnectionClientObject = new Mock <INfieldConnectionClientObject>().Object; var mockedResolver = new Mock <IDependencyResolver>(); DependencyResolver.Register(mockedResolver.Object); mockedResolver .Setup(resolver => resolver.Resolve(typeof(INfieldConnectionClientObject))) .Returns(stubbedNfieldConnectionClientObject); var target = new NfieldConnection(); var result = target.GetService <INfieldConnectionClientObject>(); Assert.Equal(result, stubbedNfieldConnectionClientObject); }
public void TestSignInAsync_CredentialsAreIncorrect_ReturnsFalse() { var mockedHttpClient = new Mock <INfieldHttpClient>(); var mockedResolver = new Mock <IDependencyResolver>(); DependencyResolver.Register(mockedResolver.Object); mockedResolver .Setup(resolver => resolver.Resolve(typeof(INfieldHttpClient))) .Returns(mockedHttpClient.Object); mockedHttpClient .Setup(httpClient => httpClient.PostAsync(It.IsAny <string>(), It.IsAny <HttpContent>())) .Returns(CreateTask(HttpStatusCode.BadRequest)); var target = new NfieldConnection(); var result = target.SignInAsync("", "", "").Result; Assert.False(result); }
public void TestDispose_HasClient_CallsDisposeOnClient() { var mockedHttpClient = new Mock <INfieldHttpClient>(); var mockedResolver = new Mock <IDependencyResolver>(); DependencyResolver.Register(mockedResolver.Object); mockedResolver .Setup(resolver => resolver.Resolve(typeof(INfieldHttpClient))) .Returns(mockedHttpClient.Object); mockedHttpClient .Setup(client => client.PostAsync(It.IsAny <string>(), It.IsAny <HttpContent>())) .Returns(CreateTask(HttpStatusCode.OK)); var target = new NfieldConnection(); var result = target.SignInAsync("", "", "").Result; target.Dispose(); mockedHttpClient.Verify(client => client.Dispose()); }
public void TestSignInAsync_CredentialsAreCorrect_ReturnsTrue() { Uri ServerUri = new Uri(@"http://localhost/"); const string Domain = "Domain"; const string Username = "******"; const string Password = "******"; var mockedHttpClient = new Mock <INfieldHttpClient>(); var mockedResolver = new Mock <IDependencyResolver>(); DependencyResolver.Register(mockedResolver.Object); mockedResolver .Setup(resolver => resolver.Resolve(typeof(INfieldHttpClient))) .Returns(mockedHttpClient.Object); var content = new FormUrlEncodedContent(new Dictionary <string, string> { { "Domain", Domain }, { "Username", Username }, { "Password", Password } }); mockedHttpClient .Setup(httpClient => httpClient.PostAsync(It.IsAny <string>(), It.IsAny <HttpContent>())) .Returns(CreateTask(HttpStatusCode.BadRequest)); mockedHttpClient .Setup(httpClient => httpClient.PostAsync(ServerUri + "SignIn", It.Is <HttpContent>(c => CheckContent(c, content)))) .Returns(CreateTask(HttpStatusCode.OK)); var target = new NfieldConnection(); target.NfieldServerUri = ServerUri; var result = target.SignInAsync(Domain, Username, Password).Result; Assert.True(result); }
public void TestGetService_RequestedServiceTypeIsNull_ThrowsArgumentNullException() { var target = new NfieldConnection(); Assert.Throws(typeof(ArgumentNullException), () => target.GetService(null)); }