public void Delete_HttpReturnsIn200Range_ReturnsRestResponse()
        {
            // Arrange
            DeleteRestResourceContext deleteResourceContext = new DeleteRestResourceContext();

            HttpRequest httpRequest = new HttpRequest()
            {
                HttpMethod = HttpMethod.DELETE,
                ResourcePath = deleteResourceContext.ResourcePath,
                ContentType = "text/plain",
            };

            RestResponse expectedResponse = new RestResponse()
            {
                StatusCode = HttpStatusCode.OK
            };

            HttpResponse httpResponse = new HttpResponse()
            {
                StatusCode = HttpStatusCode.OK
            };

            mockHttpClient.Setup(hc => hc.Submit(httpRequest)).Returns(httpResponse);

            // Act
            RestResponse actualResponse = restClient.Delete(deleteResourceContext);

            // Assert
            Assert.IsNotNull(actualResponse);
            Assert.AreEqual(expectedResponse.StatusCode, actualResponse.StatusCode);
        }
        public void Create_WithHttpWebResponse_ReturnsHttpResponse()
        {
            // Arrange
            string expectedContent = "content";

            HttpResponse expectedResponse = new HttpResponse()
            {
                Content = expectedContent,
                ContentType = "application/xml",
                StatusCode = HttpStatusCode.OK
            };

            MemoryStream stream = new MemoryStream(Encoding.ASCII.GetBytes(expectedContent));

            mockHttpWebResponse.Setup(ws => ws.GetResponseStream()).Returns(stream);

            mockHttpWebResponse.SetupGet(ws => ws.StatusCode).Returns(HttpStatusCode.OK);
            mockHttpWebResponse.SetupGet(ws => ws.ContentType).Returns("application/xml");
            mockHttpWebResponse.SetupGet(ws => ws.ContentEncoding).Returns(Encoding.UTF8.WebName);

            // Act
            HttpResponse actualResponse = helper.Create(mockHttpWebResponse.Object);

            // Assert
            Assert.IsNotNull(actualResponse);
            Assert.AreEqual(expectedResponse.StatusCode, actualResponse.StatusCode);
            Assert.AreEqual(expectedResponse.ContentType, actualResponse.ContentType);
            Assert.AreEqual(expectedResponse.ContentLength, actualResponse.ContentLength);
            Assert.AreEqual(expectedResponse.Content, actualResponse.Content);
        }
        public void Submit_GetResponseThrowsWebException_ReturnsHttpResponse()
        {
            // Arrange
            HttpRequest request = new HttpRequest()
            {
                ResourcePath = "http://tempuri.org",
                HttpMethod = HttpMethod.POST,
                Content = "content",
                ContentEncoding = Encoding.UTF8,
                ContentType = "application/xml"
            };

            HttpResponse expectedResponse = new HttpResponse()
            {
                StatusCode = HttpStatusCode.InternalServerError
            };

            WebException expectedException = new WebException();

            Mock<IHttpWebRequestAdapter> mockWebRequest = mocks.Create<IHttpWebRequestAdapter>();
            Mock<IHttpWebResponseAdapter> mockWebResponse = mocks.Create<IHttpWebResponseAdapter>();

            mockHttpRequestHelper
                .Setup(rh => rh.Create(request, client.Uri))
                .Returns(mockWebRequest.Object);

            mockHttpRequestHelper
                .Setup(rh => rh.AddCredentials(mockWebRequest.Object, mockEsendexCredentials.Object));

            mockHttpRequestHelper
                .Setup(rh => rh.AddProxy(mockWebRequest.Object, mockEsendexCredentials.Object.WebProxy));

            mockHttpRequestHelper
                .Setup(rh => rh.AddContent(mockWebRequest.Object, request));

            mockWebRequest
                .Setup(wr => wr.GetResponse())
                .Throws(expectedException);

            mockHttpResponseHelper
                .Setup(rh => rh.Create(expectedException))
                .Returns(expectedResponse);

            // Act
            HttpResponse actualResponse = client.Submit(request);

            // Assert
            Assert.IsNotNull(actualResponse);
            Assert.AreEqual(expectedResponse.StatusCode, actualResponse.StatusCode);
        }
        public void Submit_WithHttpRequest_ReturnsHttpResponse()
        {
            // Arrange
            var request = new HttpRequest
            {
                ResourcePath = "resource",
                HttpMethod = HttpMethod.POST,
                Content = "content",
                ContentEncoding = Encoding.UTF8,
                ContentType = "application/xml"
            };

            var expectedResponse = new HttpResponse
            {
                StatusCode = HttpStatusCode.OK,
                Content = "content"
            };

            var mockWebRequest = new Mock<IHttpWebRequestAdapter>();
            httpRequestHelper
                .Setup(rh => rh.Create(request, client.Uri, It.IsAny<Version>()))
                .Returns(mockWebRequest.Object);
            httpRequestHelper
                .Setup(rh => rh.AddCredentials(mockWebRequest.Object, mockEsendexCredentials.Object));
            httpRequestHelper
                .Setup(rh => rh.AddProxy(mockWebRequest.Object, mockEsendexCredentials.Object.WebProxy));
            httpRequestHelper
                .Setup(rh => rh.AddContent(mockWebRequest.Object, request));

            var mockWebResponse = new Mock<IHttpWebResponseAdapter>();
            mockWebRequest
                .Setup(wr => wr.GetResponse())
                .Returns(mockWebResponse.Object);

            httpResponseHelper
                .Setup(rh => rh.Create(mockWebResponse.Object))
                .Returns(expectedResponse);

            // Act
            var actualResponse = client.Submit(request);

            // Assert
            Assert.IsNotNull(actualResponse);
            Assert.AreEqual(expectedResponse.StatusCode, actualResponse.StatusCode);
        }
        public void Put_HttpReturnsIn200Range_ReturnsRestResponse()
        {
            // Arrange
            string content = "content";

            PutRestResourceContext putResourceContext = new PutRestResourceContext(content);

            HttpRequest httpRequest = new HttpRequest()
            {
                HttpMethod = HttpMethod.PUT,
                ResourcePath = putResourceContext.ResourcePath,
                Content = putResourceContext.Content,
                ContentType = "application/xml",
                ContentEncoding = Encoding.UTF8
            };

            RestResponse expectedResponse = new RestResponse()
            {
                StatusCode = HttpStatusCode.OK
            };

            HttpResponse httpResponse = new HttpResponse()
            {
                StatusCode = HttpStatusCode.OK
            };

            mockHttpClient.Setup(hc => hc.Submit(httpRequest)).Returns(httpResponse);

            // Act
            RestResponse actualResponse = restClient.Put(putResourceContext);

            // Assert
            Assert.IsNotNull(actualResponse);
            Assert.AreEqual(expectedResponse.StatusCode, actualResponse.StatusCode);
        }
        public void Get_HttpReturnsIn200Range_ReturnsRestResponse()
        {
            // Arrange
            string expectedContent = "content";

            GetRestResourceContext getResourceContext = new GetRestResourceContext();

            HttpRequest httpRequest = new HttpRequest()
            {
                HttpMethod = HttpMethod.GET,
                ResourcePath = getResourceContext.ResourcePath,
                ContentType = "text/plain"
            };

            RestResponse expectedResponse = new RestResponse()
            {
                StatusCode = HttpStatusCode.OK,
                Content = expectedContent
            };

            HttpResponse httpResponse = new HttpResponse()
            {
                StatusCode = HttpStatusCode.OK,
                Content = expectedContent,
                ContentType = "application/xml",
                ContentEncoding = Encoding.UTF8.WebName
            };

            mockHttpClient.Setup(hc => hc.Submit(httpRequest)).Returns(httpResponse);

            // Act
            RestResponse actualResponse = restClient.Get(getResourceContext);

            // Assert
            Assert.IsNotNull(actualResponse);
            Assert.AreEqual(expectedResponse.StatusCode, actualResponse.StatusCode);
        }
        public void Post_HttpReturnsIn200Range_ReturnsRestResponse()
        {
            // Arrange
            var content = "content";

            var postResourceContext = new PostRestResourceContext(content);

            var httpRequest = new HttpRequest
            {
                HttpMethod = HttpMethod.POST,
                ResourcePath = postResourceContext.ResourcePath,
                Content = postResourceContext.Content,
                ContentType = "application/xml",
                ContentEncoding = Encoding.UTF8
            };

            var expectedResponse = new RestResponse
            {
                StatusCode = HttpStatusCode.OK
            };

            var httpResponse = new HttpResponse
            {
                StatusCode = HttpStatusCode.OK
            };

            mockHttpClient.Setup(hc => hc.Submit(httpRequest))
                          .Returns(httpResponse);

            // Act
            var actualResponse = restClient.Post(postResourceContext);

            // Assert
            Assert.IsNotNull(actualResponse);
            Assert.AreEqual(expectedResponse.StatusCode, actualResponse.StatusCode);
        }