示例#1
0
        public static string Discover <TController>(TestServerAction action, object tokenValues)
            where TController : class
        {
            //at this moment only the first route is considered..

            var testServerTokens = AddTokens <TController>(action, tokenValues);

            var controllerTemplate = new AttributeControllerRouteTemplates()
                                     .GetTemplates <TController>(action, testServerTokens)
                                     .FirstOrDefault();

            var verbsTemplate = new AttributeVerbsTemplate()
                                .GetTemplates <TController>(action, testServerTokens)
                                .FirstOrDefault();

            var routeTemplate = new AttributeRouteTemplates()
                                .GetTemplates <TController>(action, testServerTokens)
                                .FirstOrDefault();

            var queryStringTemplate = new QueryStringTemplate()
                                      .GetTemplates <TController>(action, testServerTokens)
                                      .FirstOrDefault();

            if (verbsTemplate != null)
            {
                return($"{controllerTemplate}/{verbsTemplate}{queryStringTemplate}");
            }
            else if (routeTemplate != null)
            {
                return($"{controllerTemplate}/{routeTemplate}{queryStringTemplate}");
            }

            return($"{controllerTemplate}{queryStringTemplate}");
        }
示例#2
0
        /// <summary>
        /// 从请求中解析出路由值
        /// </summary>
        /// <param name="request">请求数据</param>
        /// <returns>路由值</returns>
        public IReadOnlyDictionary <string, string> GetRouteValues(RouteRequestData request)
        {
            if (Scheme != null && stringComparer.Equals(Scheme, request.Scheme) == false)
            {
                return(null);
            }


            var routeValues = new Dictionary <string, string>(stringComparer);

            if (HostTemplate != null)
            {
                var values = HostTemplate.GetRouteValues(request.Host);
                if (values == null)
                {
                    return(null);
                }

                foreach (var pair in values)
                {
                    routeValues.Add(pair.key, pair.value);
                }
            }


            {
                var values = PathTemplate.GetRouteValues(request.Path);
                if (values == null)
                {
                    return(null);
                }

                foreach (var pair in values)
                {
                    routeValues.Add(pair.Key, pair.Value);
                }
            }


            if (QueryStringTemplate != null)
            {
                var values = QueryStringTemplate.GetRouteValues(request.Path);
                if (values == null)
                {
                    return(null);
                }

                foreach (var pair in values)
                {
                    routeValues.Add(pair.Key, pair.Value);
                }
            }


            return(routeValues);
        }
示例#3
0
        public async System.Threading.Tasks.Task <RawResponse> GetResponse(JObject source)
        {
            string        fullUrl     = BaseApiUrl.TrimEnd(new char[] { '/', ' ' });
            StringBuilder bodyBuilder = new StringBuilder();
            JToken        requestJson = source.SelectToken("$.body");

            if (requestJson != null)
            {
                #region 准备调用所需要的各类输入参数
                if (!string.IsNullOrWhiteSpace(RouteDataTemplate))
                {
                    string[] routeParameters = RouteDataTemplate.Trim().Split(
                        '/', StringSplitOptions.RemoveEmptyEntries);
                    foreach (string route in routeParameters)
                    {
                        fullUrl = fullUrl + "/" + System.Web.HttpUtility.UrlEncode(
                            requestJson.SelectToken("$." + EsbPostBodySections.ROUTE +
                                                    "." + route).Value <string>());
                    }
                }
                if (!string.IsNullOrWhiteSpace(QueryStringTemplate))
                {
                    string[] queryParameters = QueryStringTemplate.Trim()
                                               .TrimStart('?').Split('&', StringSplitOptions.RemoveEmptyEntries);
                    fullUrl = fullUrl + "?";
                    for (int i = 0; i < queryParameters.Length; i++)
                    {
                        string query = queryParameters[i].Trim().TrimEnd('=');
                        fullUrl = fullUrl + query + "=" + System.Web.HttpUtility.UrlEncode(
                            requestJson.SelectToken("$." + EsbPostBodySections.QUERY +
                                                    "." + query).Value <string>());
                    }
                    fullUrl.TrimEnd('&');
                }
                if (JsonBodyTemplate != null)
                {
                    //向api提交请求的RequestBody中JSON优先
                    using (JsonWriter writer = new JsonTextWriter(new StringWriter(bodyBuilder)))
                    {
                        writer.Formatting = Formatting.Indented;
                        using (JTokenReader jtReader = new JTokenReader(requestJson.SelectToken("$." + EsbPostBodySections.JSON)))
                        {
                            writer.WriteToken(jtReader);
                        }
                        writer.Close();
                    }
                }
                else if (!string.IsNullOrWhiteSpace(FormBodyTemplate))
                {
                    //如果没有JSON但是有FORM
                    string[] formParameters = FormBodyTemplate.Trim()
                                              .TrimStart('?').Split('&', StringSplitOptions.RemoveEmptyEntries);
                    for (int i = 0; i < formParameters.Length; i++)
                    {
                        string parameter = formParameters[i].Trim().TrimEnd('=');
                        bodyBuilder.Append(parameter);
                        bodyBuilder.Append('=');
                        bodyBuilder.Append(System.Web.HttpUtility.UrlEncode(
                                               requestJson.SelectToken("$." + EsbPostBodySections.FORM +
                                                                       "." + parameter).Value <string>()));
                        bodyBuilder.Append('&');
                    }
                    if (bodyBuilder.Length > 0)
                    {
                        bodyBuilder.Remove(bodyBuilder.Length - 1, 1);
                    }
                }
                #endregion
            }

            #region 准备请求
            WebRequest apiRequest;
            if (fullUrl.StartsWith("https", StringComparison.OrdinalIgnoreCase))
            {
                apiRequest = HttpWebRequest.CreateHttp(fullUrl);
                ((HttpWebRequest)apiRequest).ServerCertificateValidationCallback
                    = (s, c, e, t) => true;
            }
            else
            {
                apiRequest = WebRequest.Create(fullUrl);
            }
            apiRequest.Method = Method.ToString();
            foreach (var header in Headers)
            {
                apiRequest.Headers.Add(header.Key, header.Value);
            }
            byte[] buffer = Encoding.UTF8.GetBytes(bodyBuilder.ToString());
            apiRequest.ContentLength = buffer.Length;
            using (Stream apiStream = apiRequest.GetRequestStream())
            {
                apiStream.Write(buffer, 0, buffer.Length);
                apiStream.Close();
            }
            #endregion

            using (WebResponse webResponse = await apiRequest.GetResponseAsync())
            {
                return(new RawResponse(webResponse, fullUrl));
            }
        }
示例#4
0
        public string GetPostSample()
        {
            StringBuilder bodyBuilder = new StringBuilder();

            using (JsonWriter writer = new JsonTextWriter(new StringWriter(bodyBuilder)))
            {
                writer.Formatting = Formatting.Indented;
                writer.WriteStartObject();
                if (!string.IsNullOrWhiteSpace(RouteDataTemplate))
                {
                    StringHelper.WriteRouteQueryFormJson(writer, EsbPostBodySections.ROUTE, RouteDataTemplate.Trim(), '/', "路由值");
                }
                if (!string.IsNullOrWhiteSpace(QueryStringTemplate))
                {
                    StringHelper.WriteRouteQueryFormJson(writer, EsbPostBodySections.QUERY, QueryStringTemplate.Trim().TrimStart('?'), '&', "查询字符串值");
                }
                if (JsonBodyTemplate != null)
                {
                    //向api提交请求的RequestBody中JSON优先
                    writer.WritePropertyName(EsbPostBodySections.JSON);
                    JsonBodyTemplate.WriteJsonBody(writer);
                }
                else if (!string.IsNullOrWhiteSpace(FormBodyTemplate))
                {
                    //如果没有JSON但是有FORM
                    StringHelper.WriteRouteQueryFormJson(writer, EsbPostBodySections.FORM, FormBodyTemplate.Trim().TrimStart('?'), '&', "表单数据值");
                }
                writer.WriteEndObject();
                writer.Flush();
            }
            return(bodyBuilder.ToString());
        }