/// <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);
        }
示例#2
0
        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);
        }
示例#3
0
        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);
        }
示例#4
0
        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));
        }
示例#5
0
        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);
        }
示例#6
0
        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);
        }
示例#7
0
        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());
        }
示例#8
0
        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);
        }
示例#9
0
        public void TestGetService_RequestedServiceTypeIsNull_ThrowsArgumentNullException()
        {
            var target = new NfieldConnection();

            Assert.Throws(typeof(ArgumentNullException), () => target.GetService(null));
        }