Пример #1
0
        public static RouteCollection ShouldRoute(this RouteCollection routes, string url, object expectations)
        {
            var httpRequestMock = MockRepository.GenerateMock <HttpRequestBase>();

            httpRequestMock.Stub(x => x.AppRelativeCurrentExecutionFilePath).Return(url);
            httpRequestMock.Stub(x => x.PathInfo).Return("");
            httpRequestMock.Stub(x => x.HttpMethod).Return("GET");

            var httpContextMock = MockRepository.GenerateMock <HttpContextBase>();

            httpContextMock.Stub(x => x.Request).Return(httpRequestMock);

            RouteData routeData = routes.GetRouteData(httpContextMock);

            Assert.IsNotNull(routeData, "The route was not found: " + url);

            IDictionary <string, object> checkItems = _converter.Convert(expectations);

            checkItems.Each(item =>
            {
                Assert.IsTrue(string.Equals(item.Value.ToString(), routeData.Values[item.Key].ToString(), StringComparison.OrdinalIgnoreCase),
                              "Expected '{0}', not '{1}' for '{2}'.".FormatWith(item.Value, routeData.Values[item.Key], item.Key));
            });

            return(routes);
        }
Пример #2
0
        private static void TransferDataObjectToLogEventProperties(LogEventInfo log, object logProperties)
        {
            if (logProperties == null || logProperties is string)
            {
                return;
            }

            var properties = logProperties as IDictionary ??
                             ObjectToDictionaryConverter.Convert(logProperties);

            foreach (var key in properties.Keys)
            {
                log.Properties.Add(key, properties[key]);
            }
        }
Пример #3
0
        /// <summary>
        /// Makes a 'GET' request to the server using a URI
        /// </summary>
        /// <typeparam name="T">The type of object the response should be deserialized ot</typeparam>
        /// <returns>An object with response data</returns>
        private async Task <GitHubResponse <T> > Get <T>(GitHubRequest githubRequest) where T : new()
        {
            var url = new StringBuilder().Append(githubRequest.Url);

            if (githubRequest.Args != null)
            {
                url.Append(ToQueryString(ObjectToDictionaryConverter.Convert(githubRequest.Args).ToArray()));
            }
            var absoluteUrl = url.ToString();

            // If there is no cache, just directly execute and parse. Nothing more
            using (var request = new HttpRequestMessage(HttpMethod.Get, absoluteUrl))
            {
                using (var requestResponse = await ExecuteRequest(request).ConfigureAwait(false))
                {
                    return(await ParseResponse <T>(requestResponse).ConfigureAwait(false));
                }
            }
        }
Пример #4
0
        public void SimplePassTest()
        {
            PdfConverterGlobalSettings settings = new PdfConverterGlobalSettings();

            settings.Collate       = false;
            settings.ColorMode     = ColorMode.Grayscale;
            settings.Copies        = 5;
            settings.DocumentTitle = "Bojangles";
            settings.Margin        = new PdfPageMargin()
            {
                Bottom = "2cm",
                Top    = "4cm",
                Left   = "4cm",
                Right  = "2cm"
            };
            settings.Orientation    = Orientation.Portrait;
            settings.UseCompression = false;

            Dictionary <string, string> strObj = ObjectToDictionaryConverter.Convert(settings);

            var Collate        = strObj["collate"];
            var _ColorMode     = strObj["colorMode"];
            var Copies         = strObj["copies"];
            var DocumentTitle  = strObj["documentTitle"];
            var MarginBottom   = strObj["margin.bottom"];
            var MarginTop      = strObj["margin.top"];
            var MarginLeft     = strObj["margin.left"];
            var MarginRight    = strObj["margin.right"];
            var _Orientation   = strObj["orientation"];
            var UseCompression = strObj["useCompression"];

            Assert.AreEqual("false", Collate);
            Assert.AreEqual("Grayscale", _ColorMode);
            Assert.AreEqual("5", Copies);
            Assert.AreEqual("Bojangles", DocumentTitle);
            Assert.AreEqual("2cm", MarginBottom);
            Assert.AreEqual("4cm", MarginTop);
            Assert.AreEqual("4cm", MarginLeft);
            Assert.AreEqual("2cm", MarginRight);
            Assert.AreEqual("Portrait", _Orientation);
            Assert.AreEqual("false", UseCompression);
        }
Пример #5
0
        private IValueNode ParseValue(object?value, ISet <object> set)
        {
            if (value is null)
            {
                return(NullValueNode.Default);
            }

            switch (value)
            {
            case string s:
                return(new StringValueNode(s));

            case short s:
                return(new IntValueNode(s));

            case int i:
                return(new IntValueNode(i));

            case long l:
                return(new IntValueNode(l));

            case float f:
                return(new FloatValueNode(f));

            case double d:
                return(new FloatValueNode(d));

            case decimal d:
                return(new FloatValueNode(d));

            case bool b:
                return(new BooleanValueNode(b));
            }

            Type type = value.GetType();

            if (type.IsValueType && _converter.TryConvert(
                    type, typeof(string), value, out object converted) &&
                converted is string c)
            {
                return(new StringValueNode(c));
            }

            if (set.Add(value))
            {
                if (value is IReadOnlyDictionary <string, object> dict)
                {
                    var fields = new List <ObjectFieldNode>();
                    foreach (KeyValuePair <string, object> field in dict)
                    {
                        fields.Add(new ObjectFieldNode(
                                       field.Key,
                                       ParseValue(field.Value, set)));
                    }
                    return(new ObjectValueNode(fields));
                }

                if (value is IReadOnlyList <object> list)
                {
                    var valueList = new List <IValueNode>();
                    foreach (object element in list)
                    {
                        valueList.Add(ParseValue(element, set));
                    }
                    return(new ListValueNode(valueList));
                }

                return(ParseValue(_objectToDictConverter.Convert(value), set));
            }

            // TODO : resources
            throw new SerializationException(
                      "Cycle in object graph detected.",
                      this);
        }
Пример #6
0
        protected byte[] ConvertHtmlToPdf(PdfConverterGlobalSettings globalSettings, PdfConverterObjectSettings objectSettings, byte[] data = null)
        {
            byte[] resultBuffer = null;

            lock (_converterRoot)
            {
                try
                {
                    Dictionary <string, string> sGlobalSettings = ObjectToDictionaryConverter.Convert(globalSettings);
                    Dictionary <string, string> sObjectSettings = ObjectToDictionaryConverter.Convert(objectSettings);

                    IntPtr globalSettingsPtr = WkHtmlToPdfImports.wkhtmltopdf_create_global_settings();
                    GlobalSettingsPtr = globalSettingsPtr;
                    foreach (var globalSetting in sGlobalSettings)
                    {
                        WkHtmlToPdfImports.wkhtmltopdf_set_global_setting(globalSettingsPtr, globalSetting.Key, globalSetting.Value);
                    }

                    IntPtr objectSettingsPtr = WkHtmlToPdfImports.wkhtmltopdf_create_object_settings();
                    ObjectSettingsPtr = objectSettingsPtr;
                    foreach (var objectSetting in sObjectSettings)
                    {
                        WkHtmlToPdfImports.wkhtmltopdf_set_object_setting(objectSettingsPtr, objectSetting.Key, objectSetting.Value);
                    }

                    IntPtr converterPtr = WkHtmlToPdfImports.wkhtmltopdf_create_converter(globalSettingsPtr);
                    ConverterPtr = converterPtr;

                    //Set Callbacks
                    WkHtmlToPdfImports.wkhtmltopdf_set_progress_changed_callback(converterPtr, ProgressChangedCallback);
                    WkHtmlToPdfImports.wkhtmltopdf_set_phase_changed_callback(converterPtr, PhaseChangedCallback);
                    WkHtmlToPdfImports.wkhtmltopdf_set_error_callback(converterPtr, ErrorCallback);
                    WkHtmlToPdfImports.wkhtmltopdf_set_warning_callback(converterPtr, WarningCallback);

                    WkHtmlToPdfImports.wkhtmltopdf_add_object(converterPtr, objectSettingsPtr, data);

                    if (!WkHtmlToPdfImports.wkhtmltopdf_convert(converterPtr))
                    {
                        int errorCode = WkHtmlToPdfImports.wkhtmltopdf_http_error_code(converterPtr);
                        throw new WkHtmlToPdfException(errorCode);
                    }

                    IntPtr dataPtr   = IntPtr.Zero;
                    int    resultLen = WkHtmlToPdfImports.wkhtmltopdf_get_output(converterPtr, out dataPtr);
                    if (resultLen > 0)
                    {
                        resultBuffer = new byte[resultLen];
                        Marshal.Copy(dataPtr, resultBuffer, 0, resultLen);
                    }
                }
                finally
                {
                    if (GlobalSettingsPtr != IntPtr.Zero)
                    {
                        WkHtmlToPdfImports.wkhtmltopdf_destroy_global_settings(GlobalSettingsPtr);
                        GlobalSettingsPtr = IntPtr.Zero;
                    }
                    if (ObjectSettingsPtr != IntPtr.Zero)
                    {
                        WkHtmlToPdfImports.wkhtmltopdf_destroy_object_settings(ObjectSettingsPtr);
                        ObjectSettingsPtr = IntPtr.Zero;
                    }
                    if (ConverterPtr != IntPtr.Zero)
                    {
                        WkHtmlToPdfImports.wkhtmltopdf_destroy_converter(ConverterPtr);
                        ConverterPtr = IntPtr.Zero;
                    }
                }
            }

            return(resultBuffer);
        }
Пример #7
0
        public static IScope BeginScope(this ILogger logger, string scopeName, object logProps = null)
        {
            var properties = ObjectToDictionaryConverter.Convert(logProps);

            return(new Scope(logger, scopeName, properties));
        }
Пример #8
0
 /// <summary>
 /// Updates an issue
 /// </summary>
 /// <param name="issue">The issue model</param>
 /// <returns></returns>
 public IssueModel Update(CreateIssueModel issue)
 {
     return(Update(ObjectToDictionaryConverter.Convert(issue)));
 }
Пример #9
0
        /// <summary>
        /// Makes a 'GET' request to the server using a URI
        /// </summary>
        /// <typeparam name="T">The type of object the response should be deserialized ot</typeparam>
        /// <returns>An object with response data</returns>
        private async Task <GitHubResponse <T> > Get <T>(GitHubRequest githubRequest) where T : new()
        {
            var url = new StringBuilder().Append(githubRequest.Url);

            if (githubRequest.Args != null)
            {
                url.Append(ToQueryString(ObjectToDictionaryConverter.Convert(githubRequest.Args).ToArray()));
            }
            var absoluteUrl = url.ToString();

            // If there is no cache, just directly execute and parse. Nothing more
            if (Cache == null)
            {
                using (var request = new HttpRequestMessage(HttpMethod.Get, absoluteUrl))
                {
                    using (var requestResponse = await ExecuteRequest(request).ConfigureAwait(false))
                    {
                        return(await ParseResponse <T>(requestResponse).ConfigureAwait(false));
                    }
                }
            }

            // Attempt to get the cached response
            GitHubResponse <T> cachedResponse = null;

            if (githubRequest.RequestFromCache || githubRequest.CheckIfModified)
            {
                try
                {
                    cachedResponse = Cache.Get <GitHubResponse <T> >(absoluteUrl);
                    if (githubRequest.RequestFromCache && cachedResponse != null)
                    {
                        cachedResponse.WasCached = true;
                        return(cachedResponse);
                    }
                }
                catch
                {
                }
            }

            using (var request = new HttpRequestMessage(HttpMethod.Get, absoluteUrl))
            {
                var etag = (githubRequest.CheckIfModified && cachedResponse != null) ? cachedResponse.ETag : null;
                if (etag != null)
                {
                    request.Headers.Add("If-None-Match", string.Format("\"{0}\"", etag));
                }

                using (var response = await ExecuteRequest(request).ConfigureAwait(false))
                {
                    var parsedResponse = await ParseResponse <T>(response).ConfigureAwait(false);

                    if (githubRequest.CacheResponse)
                    {
                        Cache.Set(absoluteUrl, parsedResponse);
                    }

                    return(parsedResponse);
                }
            }
        }
Пример #10
0
 /// <summary>
 /// Updates a comment
 /// </summary>
 /// <param name="comment">The issue model</param>
 /// <returns></returns>
 public CommentModel Update(CommentModel comment)
 {
     return(Update(ObjectToDictionaryConverter.Convert(comment)));
 }