Beispiel #1
0
        public string Invoke(HttpMethod httpMethod, string requestUriSuffix, Dictionary <string, string> headers, string body)
        {
            Contract.Requires(!string.IsNullOrWhiteSpace(requestUriSuffix));
            Contract.Requires(null == headers || (null != headers && !headers.ContainsKey(Constants.HttpHeaders.AUTHORIZATION)));
            Contract.Requires(null != UserInformation, Constants.Messages.CLIENT_NOT_LOGGED_IN);

            if (null == headers)
            {
                headers = new Dictionary <string, string>();
            }

            headers.Add(Constants.HttpHeaders.AUTHORIZATION, UserInformation.SecurityToken);

            var restCallExecutor = new RestCallExecutor();
            var requestUri       = new Uri(BaseUri, requestUriSuffix);

            string result;

            try
            {
                result = restCallExecutor.Invoke(httpMethod, requestUri.AbsoluteUri, headers, body ?? "");
            }
            catch (System.Net.Http.HttpRequestException ex)
            {
                var sessionResponse = GetToken(UserInformation.Username, UserInformation.Password, UserInformation.SerialNumber);
                Contract.Assert(null != sessionResponse);
                result = restCallExecutor.Invoke(httpMethod, requestUri.AbsoluteUri, headers, body ?? "");
            }

            Contract.Assert(!string.IsNullOrWhiteSpace(result));

            return(result);
        }
        public void InvokeWithWhitespaceUriThrowsContractException1()
        {
            // Arrange
            var whitespaceUri = " ";
            RestCallExecutor restCallExecutor = new RestCallExecutor();

            // Act
            restCallExecutor.Invoke(whitespaceUri);

            // Assert
        }
        public void InvokeWithNullUriThrowsContractException3()
        {
            // Arrange
            string           nullUri          = null;
            RestCallExecutor restCallExecutor = new RestCallExecutor();

            // Act
            restCallExecutor.Invoke(HttpMethod.Head, nullUri, null, null);

            // Assert
        }
        public void InvokeWithNullUriThrowsContractException()
        {
            // Arrange
            string           nullUri          = null;
            RestCallExecutor restCallExecutor = new RestCallExecutor();

            // Act
            restCallExecutor.Invoke(nullUri);

            // Assert
        }
        public void InvokeWithWhitespaceUriThrowsArgumentException2()
        {
            // Arrange
            var whitespaceUri = " ";
            RestCallExecutor restCallExecutor = new RestCallExecutor();

            // Act
            restCallExecutor.Invoke(whitespaceUri, null);

            // Assert
        }
        public void InvokeWithNullUriThrowsArgumentException2()
        {
            // Arrange
            string           nullUri          = null;
            RestCallExecutor restCallExecutor = new RestCallExecutor();

            // Act
            restCallExecutor.Invoke(nullUri, null);

            // Assert
        }
        public void InvokeWithInvalidUriThrowsUriFormatException2()
        {
            // Arrange
            var invalidUri = "abc";
            RestCallExecutor restCallExecutor = new RestCallExecutor();

            // Act
            restCallExecutor.Invoke(invalidUri, null);

            // Assert
        }
        public void InvokeWithWhitespaceUriThrowsContractException3()
        {
            // Arrange
            var whitespaceUri = " ";
            RestCallExecutor restCallExecutor = new RestCallExecutor();

            // Act
            restCallExecutor.Invoke(HttpMethod.Head, whitespaceUri, null, null);

            // Assert
        }
        public void RestCallExecutorConstructorSetsProperties()
        {
            // Arrange

            // Act
            var restCallExecutor = new RestCallExecutor();

            // Assert
            Assert.AreEqual(true, restCallExecutor.EnsureSuccessStatusCode);
            Assert.AreEqual(ContentType.ApplicationJson, restCallExecutor.ContentType);
            Assert.AreEqual(90, restCallExecutor.Timeout);
        }
        public void RestCallExecutorConstructorSetsEnsureSuccessCodePropertyAccordingConstructorParameter()
        {
            // Arrange

            // Act
            var restCallExecutor = new RestCallExecutor(false);

            // Assert
            Assert.AreEqual(false, restCallExecutor.EnsureSuccessStatusCode);
            Assert.AreEqual(ContentType.ApplicationJson, restCallExecutor.ContentType);
            Assert.AreEqual(90, restCallExecutor.Timeout);
        }
        public EntityController(IAuthenticationProvider authenticationProvider)
        {
            if (null != authenticationProvider)
            {
                _headers = new Dictionary<String, String>();
                _headers.Add(AUTHORIZATION_HEADER_KEY, authenticationProvider.GetAuthValue());
                _authType = authenticationProvider.GetAuthScheme();
            }

            Debug.Write("Initializing RestCallExecutor");
            _restCallExecutor = new RestCallExecutor(true);
            _restCallExecutor.AuthScheme = _authType;
        }
Beispiel #12
0
        public EntityController(IAuthenticationProvider authenticationProvider)
        {
            if (null != authenticationProvider)
            {
                _headers = new Dictionary <String, String>();
                _headers.Add(AUTHORIZATION_HEADER_KEY, authenticationProvider.GetAuthValue());
                _authType = authenticationProvider.GetAuthScheme();
            }

            Debug.Write("Initializing RestCallExecutor");
            _restCallExecutor            = new RestCallExecutor(true);
            _restCallExecutor.AuthScheme = _authType;
        }
        public void InvokeSetsAuthorizationHeaderAccordingHeadersDictionaryWhenAuthSchemeIsNotSet()
        {
            // Arrange
            var mockedResponseMessage = Mock.Create <HttpResponseMessage>();
            var mockedRequestHeaders  = Mock.Create <HttpRequestHeaders>();

            Mock.Arrange(() => HttpClient.DefaultRequestHeaders)
            .IgnoreInstance()
            .Returns(mockedRequestHeaders)
            .Occurs(4);

            Mock.ArrangeSet(() => mockedRequestHeaders.Authorization = new AuthenticationHeaderValue(BEARER_AUTH_SCHEME, SAMPLE_BEARER_TOKEN))
            .OccursNever();

            Mock.Arrange(() => mockedRequestHeaders.TryAddWithoutValidation(AUTHORIZATION_HEADER_KEY, SAMPLE_BEARER_TOKEN))
            .OccursOnce();

            Mock.Arrange(() => mockedRequestHeaders.Add(USER_AGENT_KEY, TEST_USER_AGENT))
            .OccursOnce();

            Mock.Arrange(() => mockedRequestHeaders.TryAddWithoutValidation(ACCEPT_HEADER_KEY, ACCEPT_HEADER_VALUE))
            .OccursOnce();

            Mock.Arrange(() => HttpClient.GetAsync(Arg.Is(new Uri(URI))).Result)
            .IgnoreInstance()
            .Returns(mockedResponseMessage)
            .OccursOnce();

            Mock.Arrange(() => mockedResponseMessage.EnsureSuccessStatusCode())
            .OccursOnce();

            Mock.Arrange(() => mockedResponseMessage.Content.ReadAsStringAsync().Result)
            .Returns(SAMPLE_RESPONSE_BODY)
            .OccursOnce();

            RestCallExecutor restCallExecutor = new RestCallExecutor();
            var headers = CreateSampleHeaders();

            headers.Add(AUTHORIZATION_HEADER_KEY, SAMPLE_BEARER_TOKEN);

            // Act
            var result = restCallExecutor.Invoke(HttpMethod.Get, URI, headers, null);

            // Assert
            Assert.AreEqual(SAMPLE_RESPONSE_BODY, result);

            Mock.Assert(HttpClient);
            Mock.Assert(mockedRequestHeaders);
            Mock.AssertSet(() => mockedRequestHeaders.Authorization = Arg.Is(new AuthenticationHeaderValue(BEARER_AUTH_SCHEME, SAMPLE_BEARER_TOKEN)));
            Mock.Assert(mockedResponseMessage);
        }
        public void InvokeSetsLastReponseHeadersAccordingHeadersFromResponse()
        {
            // Arrange
            var mockedResponseMessage = Mock.Create <HttpResponseMessage>();
            var mockedRequestHeaders  = Mock.Create <HttpRequestHeaders>();

            var mockedResponseHeaders = Mock.Create <HttpResponseHeaders>();

            Mock.Arrange(() => HttpClient.DefaultRequestHeaders)
            .IgnoreInstance()
            .Returns(mockedRequestHeaders)
            .Occurs(3);

            Mock.Arrange(() => mockedRequestHeaders.Add(USER_AGENT_KEY, TEST_USER_AGENT))
            .OccursOnce();

            Mock.Arrange(() => mockedRequestHeaders.TryAddWithoutValidation(ACCEPT_HEADER_KEY, ACCEPT_HEADER_VALUE))
            .OccursOnce();

            Mock.Arrange(() => HttpClient.GetAsync(Arg.Is(new Uri(URI))).Result)
            .IgnoreInstance()
            .Returns(mockedResponseMessage)
            .OccursOnce();

            Mock.Arrange(() => mockedResponseMessage.EnsureSuccessStatusCode())
            .OccursOnce();

            Mock.Arrange(() => mockedResponseMessage.Headers)
            .Returns(mockedResponseHeaders)
            .OccursOnce();

            Mock.Arrange(() => mockedResponseMessage.Content.ReadAsStringAsync().Result)
            .Returns(SAMPLE_RESPONSE_BODY)
            .OccursOnce();

            RestCallExecutor restCallExecutor = new RestCallExecutor();

            // Act
            var result = restCallExecutor.Invoke(HttpMethod.Get, URI, CreateSampleHeaders(), null);

            // Assert
            Assert.AreEqual(SAMPLE_RESPONSE_BODY, result);
            Assert.AreEqual(mockedResponseHeaders, restCallExecutor.GetResponseHeaders());

            Mock.Assert(HttpClient);
            Mock.Assert(mockedRequestHeaders);
            Mock.Assert(mockedResponseMessage);
        }
        public void TestInitialize()
        {
            Sut = new Client(TestConstants.OSRAM_LIGHTIFY_BASE_URI)
            {
                UserInformation = new UserInformation()
                {
                    UserId        = TestConstants.USER_ID,
                    Username      = TestConstants.USERNAME,
                    Password      = TestConstants.PASSWORD,
                    SerialNumber  = TestConstants.SERIAL_NUMBER,
                    SecurityToken = TestConstants.SECURITY_TOKEN
                }
            };

            RestCallExecutor = Mock.Create <RestCallExecutor>();
        }
        public void InvokePostOverwritesDefaultContentTypeHeaderIfCustomContentTypeHeaderProvided()
        {
            // Arrange
            var mockedResponseMessage = Mock.Create <HttpResponseMessage>();
            var mockedRequestHeaders  = Mock.Create <HttpRequestHeaders>();

            Mock.Arrange(() => HttpClient.DefaultRequestHeaders)
            .IgnoreInstance()
            .Returns(mockedRequestHeaders)
            .Occurs(4);

            Mock.Arrange(() => mockedRequestHeaders.Add(USER_AGENT_KEY, TEST_USER_AGENT))
            .OccursOnce();

            Mock.Arrange(() => mockedRequestHeaders.TryAddWithoutValidation(ACCEPT_HEADER_KEY, ACCEPT_HEADER_VALUE))
            .OccursOnce();

            var content = new StringContent(SAMPLE_REQUEST_BODY);

            Mock.Arrange(() => HttpClient.PostAsync(Arg.Is(new Uri(URI)), Arg.Is(content)).Result)
            .IgnoreInstance()
            .Returns(mockedResponseMessage)
            .OccursOnce();

            Mock.Arrange(() => mockedResponseMessage.EnsureSuccessStatusCode())
            .OccursOnce();

            Mock.Arrange(() => mockedResponseMessage.Content.ReadAsStringAsync().Result)
            .Returns(SAMPLE_RESPONSE_BODY)
            .OccursOnce();

            RestCallExecutor restCallExecutor = new RestCallExecutor();
            var headers = CreateSampleHeaders();

            headers.Add(CONTENT_TYPE_KEY, CONTENT_TYPE_VALUE);

            // Act
            var result = restCallExecutor.Invoke(HttpMethod.Post, URI, headers, SAMPLE_RESPONSE_BODY);

            // Assert
            Assert.AreEqual(SAMPLE_RESPONSE_BODY, result);

            Mock.Assert(HttpClient);
            Mock.Assert(mockedRequestHeaders);
            Mock.Assert(mockedResponseMessage);
        }
        public void InvokePutExecutesPutRequestOnUriWithProvidedHeadersAndBodyEnsuresSuccessStatusCodeAndReturnsResponseContent()
        {
            // Arrange
            var mockedResponseMessage = Mock.Create <HttpResponseMessage>();
            var mockedRequestHeaders  = Mock.Create <HttpRequestHeaders>();

            Mock.Arrange(() => HttpClient.DefaultRequestHeaders)
            .IgnoreInstance()
            .Returns(mockedRequestHeaders)
            .Occurs(3);

            Mock.Arrange(() => mockedRequestHeaders.Add(USER_AGENT_KEY, TEST_USER_AGENT))
            .OccursOnce();

            Mock.Arrange(() => mockedRequestHeaders.TryAddWithoutValidation(ACCEPT_HEADER_KEY, ACCEPT_HEADER_VALUE))
            .OccursOnce();

            HttpContent content = new StringContent(SAMPLE_REQUEST_BODY);

            Mock.Arrange(() => HttpClient.PutAsync(Arg.Is(new Uri(URI)), Arg.Is(content)).Result)
            .IgnoreInstance()
            .Returns(mockedResponseMessage)
            .OccursOnce();

            Mock.Arrange(() => mockedResponseMessage.EnsureSuccessStatusCode())
            .OccursOnce();

            Mock.Arrange(() => mockedResponseMessage.Content.ReadAsStringAsync().Result)
            .Returns(SAMPLE_RESPONSE_BODY)
            .OccursOnce();

            RestCallExecutor restCallExecutor = new RestCallExecutor();

            // Act
            var result = restCallExecutor.Invoke(HttpMethod.Put, URI, CreateSampleHeaders(), SAMPLE_REQUEST_BODY);

            // Assert
            Assert.AreEqual(SAMPLE_RESPONSE_BODY, result);
            Assert.AreEqual(ContentType.ApplicationJson, restCallExecutor.ContentType);

            Mock.Assert(HttpClient);
            Mock.Assert(mockedRequestHeaders);
            Mock.Assert(mockedResponseMessage);
        }
Beispiel #18
0
        public string GetToken(Uri baseUri, string username, string password, string serialNumber)
        {
            Contract.Requires(null != baseUri);
            Contract.Requires(baseUri.IsAbsoluteUri);
            Contract.Requires(!string.IsNullOrWhiteSpace(username));
            Contract.Requires(!string.IsNullOrWhiteSpace(password));
            Contract.Requires(!string.IsNullOrWhiteSpace(serialNumber));
            Contract.Ensures(!string.IsNullOrWhiteSpace(Contract.Result <string>()));

            this.BaseUri = baseUri;

            var sut = new SessionRequest
            {
                Username     = username,
                Password     = password,
                SerialNumber = serialNumber
            };

            var body       = sut.SerializeObject();
            var client     = new RestCallExecutor();
            var requestUri = new Uri(baseUri, Constants.ApiOperation.SESSION);
            var result     = client.Invoke(HttpMethod.Post, requestUri.AbsoluteUri, null, body);

            Contract.Assert(!string.IsNullOrWhiteSpace(result));

            var response = BaseDto.DeserializeObject <SessionResponse>(result);

            UserInformation = new UserInformation()
            {
                UserId        = response.UserId,
                Username      = username,
                Password      = password,
                SerialNumber  = serialNumber,
                SecurityToken = response.SecurityToken
            };

            return(response.SecurityToken);
        }
        public void InvokeGetExecutesGetRequestOnUriWithProvidedHeadersNotEnsuringSuccessStatusCodeReturnsResponseContent()
        {
            // Arrange
            var mockedResponseMessage = Mock.Create <HttpResponseMessage>();
            var mockedRequestHeaders  = Mock.Create <HttpRequestHeaders>();

            Mock.Arrange(() => HttpClient.DefaultRequestHeaders)
            .IgnoreInstance()
            .Returns(mockedRequestHeaders)
            .Occurs(3);

            Mock.Arrange(() => mockedRequestHeaders.Add(USER_AGENT_KEY, TEST_USER_AGENT))
            .OccursOnce();

            Mock.Arrange(() => mockedRequestHeaders.TryAddWithoutValidation(ACCEPT_HEADER_KEY, ACCEPT_HEADER_VALUE))
            .OccursOnce();

            Mock.Arrange(() => HttpClient.GetAsync(Arg.Is(new Uri(URI))).Result)
            .IgnoreInstance()
            .Returns(mockedResponseMessage)
            .OccursOnce();

            Mock.Arrange(() => mockedResponseMessage.Content.ReadAsStringAsync().Result)
            .Returns(SAMPLE_RESPONSE_BODY)
            .OccursOnce();

            RestCallExecutor restCallExecutor = new RestCallExecutor(false);

            // Act
            var result = restCallExecutor.Invoke(URI, CreateSampleHeaders());

            Assert.AreEqual(SAMPLE_RESPONSE_BODY, result);

            // Assert
            Mock.Assert(HttpClient);
            Mock.Assert(mockedRequestHeaders);
            Mock.Assert(mockedResponseMessage);
        }
        public void InvokeSetsDefaultUserAgentHeaderIfNoCustomUserAgentHeaderProvided()
        {
            // Arrange
            var mockedResponseMessage = Mock.Create <HttpResponseMessage>();
            var mockedRequestHeaders  = Mock.Create <HttpRequestHeaders>();

            Mock.Arrange(() => HttpClient.DefaultRequestHeaders)
            .IgnoreInstance()
            .Returns(mockedRequestHeaders)
            .Occurs(2);

            Mock.Arrange(() => mockedRequestHeaders.Add(USER_AGENT_KEY, "RestCallExecutor"))
            .OccursOnce();

            Mock.Arrange(() => HttpClient.GetAsync(Arg.Is(new Uri(URI))).Result)
            .IgnoreInstance()
            .Returns(mockedResponseMessage)
            .OccursOnce();

            Mock.Arrange(() => mockedResponseMessage.EnsureSuccessStatusCode())
            .OccursOnce();

            Mock.Arrange(() => mockedResponseMessage.Content.ReadAsStringAsync().Result)
            .Returns(SAMPLE_RESPONSE_BODY)
            .OccursOnce();

            RestCallExecutor restCallExecutor = new RestCallExecutor();
            var result = restCallExecutor.Invoke(URI);

            // Act
            Assert.AreEqual(SAMPLE_RESPONSE_BODY, result);

            // Assert
            Mock.Assert(HttpClient);
            Mock.Assert(mockedRequestHeaders);
            Mock.Assert(mockedResponseMessage);
        }
        public void InvokeExecutesThrowsHttpRequestExceptionIfEnsureSuccessStatusCodeFails()
        {
            // Arrange
            var mockedResponseMessage = Mock.Create <HttpResponseMessage>();
            var mockedRequestHeaders  = Mock.Create <HttpRequestHeaders>();

            Mock.Arrange(() => HttpClient.DefaultRequestHeaders)
            .IgnoreInstance()
            .Returns(mockedRequestHeaders)
            .Occurs(3);

            Mock.Arrange(() => mockedRequestHeaders.Add(USER_AGENT_KEY, TEST_USER_AGENT))
            .OccursOnce();

            Mock.Arrange(() => mockedRequestHeaders.TryAddWithoutValidation(ACCEPT_HEADER_KEY, ACCEPT_HEADER_VALUE))
            .OccursOnce();

            Mock.Arrange(() => HttpClient.GetAsync(Arg.Is(new Uri(URI))).Result)
            .IgnoreInstance()
            .Returns(mockedResponseMessage)
            .OccursOnce();

            Mock.Arrange(() => mockedResponseMessage.EnsureSuccessStatusCode())
            .Throws <HttpRequestException>()
            .OccursOnce();

            RestCallExecutor restCallExecutor = new RestCallExecutor();

            // Act
            restCallExecutor.Invoke(URI, CreateSampleHeaders());

            // Assert
            Mock.Assert(HttpClient);
            Mock.Assert(mockedRequestHeaders);
            Mock.Assert(mockedResponseMessage);
        }
Beispiel #22
0
 public HttpCalloutExecutor()
 {
     _restCallExecutor = new RestCallExecutor(true);
 }
 public HttpCalloutExecutor()
 {
    _restCallExecutor = new RestCallExecutor(true);
 }