コード例 #1
0
ファイル: ChannelTest.cs プロジェクト: huankiat/excalibur
        public void GetAllChannelsTest()
        {
            using (ShimsContext.Create())
            {
                var responseStream = new FileStream("Fixtures/v1.channels.index.response", FileMode.Open);
                var responseShim = new ShimHttpWebResponse()
                {
                     GetResponseStream = () => responseStream
                };
                String actualMethod = "";
                var requestShim = new ShimHttpWebRequest()
                {
                    MethodSetString = (method) => { actualMethod = method; },
                    GetResponse = () => responseShim
                };

                String actualURL = "";
                ShimWebRequest.CreateString = (url) =>
                {
                    actualURL = url;
                    return requestShim;
                };

                Channel ch = new Channel();
                JArray json = ch.getAllBroadcastsChannels();

                StringAssert.AreEqualIgnoringCase("GET", actualMethod);
                StringAssert.AreEqualIgnoringCase("http://panoply-staging.herokuapp.com/api/channels.json", actualURL);
                Assert.AreEqual(2, json.Count);
            }
        }
コード例 #2
0
        private void InitializeApiTest()
        {
            _requestUrl = string.Empty;
            _webRequestMethod = string.Empty;
            _webRequest = new ShimHttpWebRequest {MethodSetString = m => _webRequestMethod = m};

            // Setup a shim for the call to create a web request so that when an API call references it
            // a dummy request is returned for unit testing purposes, and the requestURL is set
            // appropriately so that it can be checked in the assert stage of the test.
            ShimSkytapApi.CreateSkytapWebRequestCredentialsString = (c, url) =>
            {
                _requestUrl = url;
                return _webRequest;
            };
        }
コード例 #3
0
        public void SkytapApi_SendSkytapHttpRequest_Success()
        {
            using (ShimsContext.Create())
            {
                // Arrange
                var webHeaders = new ShimWebHeaderCollection();
                webHeaders.ToString = () => "TestString";

                var response = new ShimHttpWebResponse();
                response.StatusCodeGet = () => HttpStatusCode.OK;
                response.HeadersGet = () => webHeaders;
                response.GetResponseStream = () => new MemoryStream(System.Text.Encoding.UTF8.GetBytes("BodyString"));

                var request = new ShimHttpWebRequest();
                request.GetResponse = () => response;

                // This was experimental code that is left here for reference purposes. It shows how to
                // simulate an exception being thrown in a shimmed object's method.
                //
                //var webException = new ShimHttpException();
                //ShimHttpWebRequest.AllInstances.GetResponse = url => { throw (HttpException)webException; };

                // Act
                var responseString = SkytapApi.SendSkytapHttpRequest(request);

                // Assert
                Assert.IsFalse(string.IsNullOrEmpty(responseString));
            }
        }
コード例 #4
0
        public void SkytapApi_SendSkytapHttpRequest_BadHttpStatuses()
        {
            var badHttpStatuses = new[] {HttpStatusCode.NotFound, HttpStatusCode.ServiceUnavailable};

            using (ShimsContext.Create())
            {
                foreach (var httpStatus in badHttpStatuses)
                {
                    var response = new ShimHttpWebResponse();

                    var status = httpStatus; // required as accessing foreach variable may have different behavior with different compilers
                    response.StatusCodeGet = () => status;

                    var request = new ShimHttpWebRequest();
                    request.GetResponse = () => response;

                    try
                    {
                        // The following call should trigger an exception so the next line should never be hit
                        SkytapApi.SendSkytapHttpRequest(request);

                        Assert.Fail("Exception was expected; fail the test");
                    }
                    catch (Exception e)
                    {
                        StringAssert.Contains(e.Message, ((int)httpStatus).ToString(CultureInfo.InvariantCulture));
                    }
                }
            }
        }
コード例 #5
0
        private GeoCodeAddressResponseModel.WebServiceGeocodeQueryResultSet SendData(HttpStatusCode statusCode, string dataToSend, GeoCodeAddressModel.GeocodeAddressNonParsed addressData)
        {
            using (ShimsContext.Create())
            {
                var requestShim = new ShimHttpWebRequest();
                var responseShim = new ShimHttpWebResponse();

                ShimWebRequest.CreateHttpString = (url) => requestShim.Instance;
                requestShim.HeadersGet = () => new WebHeaderCollection();

                ExpectSerialize(dataToSend);
                GeoCodeAddressResponseModel.WebServiceGeocodeQueryResultSet serviceResponse = GeoCodeAddressModelResponseSample.TypedSample;
                ExpectDeSerialize(serviceResponse);

                using (Stream requestStream = BuildStream(), responseStream = BuildStream(dataToSend))
                {
                    requestShim.GetRequestStream = () => requestStream;
                    requestShim.GetResponse = () => responseShim.Instance;

                    responseShim.StatusCodeGet = () => statusCode;
                    responseShim.GetResponseStream = () => responseStream;

                    GeoCodeAddressResponseModel.WebServiceGeocodeQueryResultSet result;
                    Assert.IsTrue(Client.SendData(addressData, out result));
                    Assert.IsNotNull(result);

                    return result;
                }
            }
        }