예제 #1
0
        private static Mock <HttpWebResponse> CreateResponseMock(Config config, HttpResponseMock apiResponse)
        {
            var responseStream = CreateResponseStream(config, apiResponse);

            var response = new Mock <HttpWebResponse>(MockBehavior.Loose);

            response.Setup(c => c.GetResponseStream()).Returns(responseStream);
            response.Setup(c => c.StatusCode).Returns(((HttpStatusCode)(apiResponse.HttpCode ?? config.DefaultHttpCode)));
            response.Setup(c => c.ContentLength).Returns(apiResponse.ContentLength ?? config.DefaultContentLength);
            response.Setup(c => c.ContentType).Returns(apiResponse.ContentType ?? config.DefaultContentType);
            response.Setup(c => c.Method).Returns(apiResponse.Method ?? config.DefaultMethod);

            Uri uri = null;

            if (!string.IsNullOrEmpty(apiResponse.ResponseUri))
            {
                uri = new Uri(apiResponse.ResponseUri);
            }
            else if (!string.IsNullOrEmpty(config.DefaultResponseUri))
            {
                uri = new Uri(config.DefaultResponseUri);
            }

            response.Setup(c => c.ResponseUri).Returns(uri);
            response.Setup(c => c.StatusDescription).Returns(apiResponse.StatusDescription ?? config.DefaultStatusDescription);
            response.Setup(c => c.IsFromCache).Returns(apiResponse.IsFromCache ?? config.DefaultIsFromCache);
            response.Setup(c => c.IsMutuallyAuthenticated).Returns(apiResponse.IsMutuallyAuthenticated ?? config.DefaultIsMutuallyAuthenticated);
            response.Setup(c => c.Cookies).Returns(apiResponse.CookieCollection ?? config.CookieCollection);
            response.Setup(c => c.Headers).Returns(apiResponse.HeaderCollection ?? config.DefaultHeaderCollection);

            return(response);
        }
예제 #2
0
        internal static MemoryStream CreateResponseStream(Config config, HttpResponseMock apiResponse)
        {
            var responseStream = new MemoryStream();

            if (apiResponse.HasFile)
            {
                if (!File.Exists(apiResponse.File))
                {
                    throw new FileLoadException($"File {apiResponse.File} not exists");
                }

                using (var streamReader = new StreamReader(apiResponse.File))
                {
                    streamReader.BaseStream.CopyTo(responseStream);
                }
            }
            else if (apiResponse.HasFiles)
            {
                var file = apiResponse.GetNextFile();
                if (!File.Exists(file))
                {
                    throw new FileLoadException($"File {file} not exists");
                }

                using (var streamReader = new StreamReader(file))
                {
                    streamReader.BaseStream.CopyTo(responseStream);
                }
            }
            else
            {
                var expectedBytes = Encoding.UTF8.GetBytes(apiResponse.Response ?? config.DefaultResponse);
                responseStream.Write(expectedBytes, 0, expectedBytes.Length);
            }

            responseStream.Seek(0, SeekOrigin.Begin);
            return(responseStream);
        }