public IMiddlewareOptionsBuilder UseResponse(byte[] responseBytes,
                                                     ResponseContentType contentType,
                                                     Encoding encoding,
                                                     int code503RetryInterval = DefaultValues.DEFAULT_503_RETRY_INTERVAL)
        {
            if (responseBytes == null)
            {
                throw new ArgumentNullException(nameof(responseBytes));
            }

            MaintenanceResponse response
                = new MaintenanceResponse
                {
                ContentBytes         = responseBytes,
                ContentEncoding      = encoding,
                ContentType          = contentType,
                Code503RetryInterval = code503RetryInterval
                };

            _options.Add(new ResponseOption
            {
                Value = response
            });

            return(this);
        }
Пример #2
0
        private WebResponse GetResponse(string relativeUri, ResponseContentType contentType)
        {
            var uri     = GetUri(relativeUri);
            var request = WebRequest.Create(uri);

            SetupHttpBasicAuth(request);
            SetupContentType(request, contentType);
            return(request.GetResponse());
        }
        public IMiddlewareOptionsBuilder UseResponse(string response,
                                                     ResponseContentType contentType,
                                                     Encoding encoding,
                                                     int code503RetryInterval = DefaultValues.DEFAULT_503_RETRY_INTERVAL)
        {
            if (string.IsNullOrEmpty(response))
            {
                throw new ArgumentNullException(nameof(response));
            }

            return(UseResponse(encoding.GetBytes(response), contentType, encoding, code503RetryInterval));
        }
Пример #4
0
        public void GetContentTypeString_WithInalidEnumValue_ShouldThrow()
        {
            ResponseContentType invalidEnumValue = (ResponseContentType)(-1);
            MaintenanceResponse response         = new MaintenanceResponse
            {
                ContentType = invalidEnumValue
            };

            Func <string> testFunc = () => response.GetContentTypeString();

            testFunc.ShouldThrow(typeof(InvalidOperationException));
        }
Пример #5
0
        public void UseResponseStringOverload_WithInvalidData_ShouldThrow(string response,
                                                                          ResponseContentType contentType,
                                                                          int codePage,
                                                                          Type expectedException)
        {
            Encoding encoding   = Encoding.GetEncoding(codePage);
            var      builder    = new MiddlewareOptionsBuilder(_dirMapperSvc);
            Action   testAction = () =>
            {
                builder.UseResponse(response, contentType, encoding);
            };


            testAction.ShouldThrow(expectedException);
        }
Пример #6
0
        public static async Task <T> RequestService <T>(BaseApiRequest request, ResponseContentType contentType = ResponseContentType.Json) where T : BaseApiResponse
        {
            var url        = request.Endpoint;
            var httpClient = new HttpClient();

            Console.WriteLine($"Begin {request.RequestMethod}: {url}");

            HttpResponseMessage response = null;

            if (request.RequestMethod == HttpMethod.Get)
            {
                response = await httpClient.GetAsync(url);
            }
            else
            {
                response = await httpClient.PostAsync(url, request.RequestContent);
            }

            var apiResult = await response.Content.ReadAsStringAsync();

            Console.WriteLine("ApiResult:");
            Console.WriteLine(apiResult);

            var result = default(T);

            switch (contentType)
            {
            case ResponseContentType.Xml:
                using (StringReader reader = new StringReader(apiResult))
                {
                    var serializer = new XmlSerializer(typeof(T), new XmlRootAttribute("xml"));
                    result = (T)serializer.Deserialize(reader);
                }
                break;

            case ResponseContentType.Json:
                result = JsonConvert.DeserializeObject <T>(apiResult);
                break;

            default:
                break;
            }

            Console.WriteLine($"Finish {request.RequestMethod}: {url} response:[{result.ErrCode}] {result.ErrMsg}");
            return(result);
        }
Пример #7
0
        private static void SetupContentType(WebRequest request, ResponseContentType contentType)
        {
            switch (contentType)
            {
            case ResponseContentType.PlainText:
                request.ContentType = "text/plain";
                break;

            case ResponseContentType.Xml:
                request.ContentType = "application/xml";
                break;

            case ResponseContentType.Json:
                request.ContentType = "application/json";
                break;
            }
        }
Пример #8
0
    static int pushNetStream(IntPtr L)
    {
        int count = LuaDLL.lua_gettop(L);

        if (count == 3)
        {
            NetReader           reader = (NetReader)LuaScriptMgr.GetNetObjectSelf(L, 1, "NetReader");
            byte[]              objs0  = LuaScriptMgr.GetArrayNumber <byte>(L, 1);
            NetworkType         arg1   = (NetworkType)LuaScriptMgr.GetNetObject(L, 2, typeof(NetworkType));
            ResponseContentType arg2   = (ResponseContentType)LuaScriptMgr.GetNetObject(L, 2, typeof(ResponseContentType));
            bool o = reader.pushNetStream(objs0, arg1, arg2);
            LuaScriptMgr.Push(L, o);
            return(1);
        }

        return(0);
    }
Пример #9
0
    static int SetUrl(IntPtr L)
    {
        int count = LuaDLL.lua_gettop(L);

        if (count == 1 && LuaScriptMgr.CheckTypes(L, 1, typeof(string)))
        {
            string o = LuaScriptMgr.GetString(L, 1);
            NetWriter.SetUrl(o);
        }
        else if (count == 3)
        {
            string str = LuaScriptMgr.GetString(L, 1);
            ResponseContentType responseType = (ResponseContentType)LuaScriptMgr.GetNetObject(L, 2, typeof(ResponseContentType));
            bool arg3 = LuaScriptMgr.GetBoolean(L, 3);
            NetWriter.SetUrl(str, responseType, arg3);
        }

        return(0);
    }
Пример #10
0
        public void UseResponseStringOverload_WithValidData_ValueShouldEqualInput(string response,
                                                                                  ResponseContentType contentType,
                                                                                  int codePage)
        {
            Encoding encoding = Encoding.GetEncoding(codePage);
            var      builder  = new MiddlewareOptionsBuilder(_dirMapperSvc);


            builder.UseResponse(response, contentType, encoding);


            var option = builder
                         .GetOptions()
                         .GetSingleOrDefault <ResponseOption>();

            option.Value.ShouldNotBeNull();
            option.Value.ContentBytes.ShouldBe(encoding.GetBytes(response));
            option.Value.ContentType.ShouldBe(contentType);
            option.Value.ContentEncoding.ShouldBe(encoding);
        }
Пример #11
0
    static int SetUrl(IntPtr L)
    {
        try
        {
            int count = LuaDLL.lua_gettop(L);

            if (count == 1)
            {
                string arg0 = ToLua.CheckString(L, 1);
                NetWriter.SetUrl(arg0);
                return(0);
            }
            else if (count == 2)
            {
                string arg0 = ToLua.CheckString(L, 1);
                ResponseContentType arg1 = (ResponseContentType)ToLua.CheckObject(L, 2, typeof(ResponseContentType));
                NetWriter.SetUrl(arg0, arg1);
                return(0);
            }
            else if (count == 3)
            {
                string arg0 = ToLua.CheckString(L, 1);
                ResponseContentType arg1 = (ResponseContentType)ToLua.CheckObject(L, 2, typeof(ResponseContentType));
                bool arg2 = LuaDLL.luaL_checkboolean(L, 3);
                NetWriter.SetUrl(arg0, arg1, arg2);
                return(0);
            }
            else
            {
                return(LuaDLL.luaL_throw(L, "invalid arguments to method: NetWriter.SetUrl"));
            }
        }
        catch (Exception e)
        {
            return(LuaDLL.toluaL_exception(L, e));
        }
    }
Пример #12
0
 /// <summary>
 /// 设置字节流,并解开包的头部信息
 /// </summary>
 /// <param name="buffer"></param>
 /// <param name="type"></param>
 /// <param name="respContentType"></param>
 /// <returns></returns>
 public bool pushNetStream(byte[] buffer, NetworkType type, ResponseContentType respContentType)
 {
     if (respContentType == ResponseContentType.Json)
     {
         string jsonData = Encoding.UTF8.GetString(buffer);
         Debug.Log("response json:" + jsonData);
         if (!_formater.TryParse(jsonData, type, out _head, out Data))
         {
             Debug.LogError(" Failed: NetReader's pushNetStream parse error.");
             return(false);
         }
         SetBuffer(new byte[0]);
         Debug.Log("parse json ok." + _head.Description);
         return(true);
     }
     byte[] data;
     if (!_formater.TryParse(buffer, out _head, out data))
     {
         Debug.LogError(" Failed: NetReader's pushNetStream parse head error: buffer Length " + buffer.Length);
         return(false);
     }
     SetBuffer(data);
     return(true);
 }
Пример #13
0
 /// <inheritdoc />
 public HttpInputDescriptor ResponseContentType(ResponseContentType responseContentType) =>
 Assign(a => a.ResponseContentType = responseContentType);
Пример #14
0
        public void GetContentTypeString_WithValidEnumValue_ShouldReturnValidContentType(ResponseContentType contentType, string contentTypeString)
        {
            MaintenanceResponse response = new MaintenanceResponse
            {
                ContentType = contentType
            };

            string returned = response.GetContentTypeString();

            returned
            .ShouldBe(contentTypeString);
        }
Пример #15
0
 public void SetContentType(ResponseContentType type)
 {
     ContentType = AutoCSer.Net.Http.ContentTypeAttribute.Get(type);
 }
Пример #16
0
        public void LoadFromString_WithVariousContentTypes_ContentTypeInValueShouldMatchInput(string input, ResponseContentType contentType)
        {
            var option = new ResponseOption();

            option.LoadFromString(input);

            option.Value.ContentType
            .ShouldBe(contentType);
        }
Пример #17
0
 public static void SetUrl(string szUrl, ResponseContentType respContentType, bool isGet = false)
 {
     s_strUrl = szUrl;
     IsGet = isGet;
     _respContentType = respContentType;
 }
Пример #18
0
        internal static byte[] Get(ResponseContentType type)
        {
            switch (type)
            {
            case ResponseContentType.Html: return(Html);

            case ResponseContentType.Js: return(Js);

            case ResponseContentType.Mp3: return(Mp3);

            case ResponseContentType.Mp4: return(Mp4);

            case ResponseContentType.Rmvb: return(Rmvb);

            case ResponseContentType.Doc: return(Doc);

            case ResponseContentType.Woff: return(Woff);

            case ResponseContentType.Gif: return(Gif);

            case ResponseContentType.Swf: return(Swf);

            case ResponseContentType.Pdf: return(Pdf);

            case ResponseContentType.Otf: return(Otf);

            case ResponseContentType.Jpeg: return(Jpeg);

            case ResponseContentType.Jpg: return(Jpg);

            case ResponseContentType.Png: return(Png);

            case ResponseContentType.Mpg: return(Mpg);

            case ResponseContentType.Svg: return(Svg);

            case ResponseContentType.Avi: return(Avi);

            case ResponseContentType.Apk: return(Apk);

            case ResponseContentType.Xml: return(Xml);

            case ResponseContentType.Rm: return(Rm);

            case ResponseContentType.Ico: return(Ico);

            case ResponseContentType.Zip: return(Zip);

            case ResponseContentType.Bmp: return(Bmp);

            case ResponseContentType.Rar: return(Rar);

            case ResponseContentType.Cur: return(Cur);

            case ResponseContentType.Css: return(Css);

            case ResponseContentType.Xls: return(Xls);

            case ResponseContentType.Txt: return(Txt);

            case ResponseContentType.Eot: return(Eot);

            case ResponseContentType.Wav: return(Wav);

            case ResponseContentType.Wmv: return(Wmv);

            case ResponseContentType.Docx: return(Docx);

            case ResponseContentType.Xlsx: return(Xlsx);

            case ResponseContentType._7z: return(_7z);
            }
            return(null);
        }
Пример #19
0
 private static void SetupContentType(WebRequest request, ResponseContentType contentType)
 {
     switch(contentType)
     {
         case ResponseContentType.PlainText:
             request.ContentType = "text/plain";
             break;
         case ResponseContentType.Xml:
             request.ContentType = "application/xml";
             break;
         case ResponseContentType.Json:
             request.ContentType = "application/json";
             break;
     }
 }
Пример #20
0
 public static void SetUrl(string szUrl, ResponseContentType respContentType, bool isGet = false)
 {
     s_strUrl         = szUrl;
     IsGet            = isGet;
     _respContentType = respContentType;
 }
Пример #21
0
 public void SetContent <T>(T responseContent)
 {
     _graph         = responseContent;
     _graphRootType = typeof(T);
     _contentType   = ResponseContentType.SerializedObject;
 }
Пример #22
0
 public void SetContent(string content)
 {
     _contentType = ResponseContentType.Raw;
     _rawContent  = content;
 }
Пример #23
0
 private WebResponse GetResponse(string relativeUri, ResponseContentType contentType)
 {
     var uri = GetUri(relativeUri);
     var request = WebRequest.Create(uri);
     SetupHttpBasicAuth(request);
     SetupContentType(request, contentType);
     return request.GetResponse();
 }