コード例 #1
0
        public void TesNestedAdd()
        {
            RequestMap map = new RequestMap();

            map.Add("key1.key1", "value1");

            Assert.AreEqual(1, map.Count);
            //checl is it finds the key
            Assert.IsTrue(map.ContainsKey("key1"));
            //check if it find the nested keys
            Assert.IsTrue(map.ContainsKey("key1.key1"));
            // check if it find returns the correct value
            Assert.AreEqual("value1", map ["key1.key1"]);
            // check if the first level map contains only one value
            Assert.AreNotSame(1, ((Dictionary <String, Object>)map["key1"]).Count);


            map.Add("key1.key2", "value2");

            Assert.AreEqual(1, map.Count);
            //checl is it finds the key
            Assert.IsTrue(map.ContainsKey("key1"));
            //check if it find the nested keys
            Assert.IsTrue(map.ContainsKey("key1.key2"));
            // check if it find returns the correct value
            Assert.AreEqual("value2", map ["key1.key2"]);
            // check if the first level map contains only one value
            Assert.AreNotSame(2, ((Dictionary <String, Object>)map["key1"]).Count);
        }
コード例 #2
0
        public void TestAddIndexed()
        {
            RequestMap map = new RequestMap();

            map.Add("map[].name", "name1");
            map.Add("map[].name", "name2");
            map.Add("map[].name", "name3");

            Assert.AreEqual(1, map.Count);

            Assert.AreNotSame(1, ((List <Dictionary <String, Object> >)map["map"]).Count);
            //checl is it finds the key
            Assert.IsTrue(map.ContainsKey("map"));
            Assert.IsTrue(map.ContainsKey("map[0]"));
            Assert.IsTrue(map.ContainsKey("map[1]"));
            Assert.IsTrue(map.ContainsKey("map[2]"));
            Assert.IsFalse(map.ContainsKey("map[3]"));


            Assert.AreEqual("name1", map ["map[0].name"]);
            Assert.AreEqual("name2", map ["map[1].name"]);
            Assert.AreEqual("name3", map ["map[2].name"]);

            // check if the first level map contains only one value
            Assert.AreNotSame(1, ((Dictionary <String, Object>)map ["map[0]"]).Count);
            Assert.AreNotSame(1, ((Dictionary <String, Object>)map ["map[1]"]).Count);
            Assert.AreNotSame(1, ((Dictionary <String, Object>)map ["map[2]"]).Count);
        }
コード例 #3
0
        public void TestConvertValueToMap()
        {
            RequestMap map = new RequestMap();

            map.Add("level1", "value1");
            Assert.Throws <InvalidCastException> (() => { map.Add("level1.level2", "level2"); });
        }
コード例 #4
0
        public void testActionCreatePostEqual200()
        {
            RequestMap inputMap = new RequestMap();

            inputMap.Add("id", 1);
            inputMap.Add("title", "My Title");
            inputMap.Add("body", "Some Long text of Body");
            Post createdItem = Post.Create(new RequestMap(inputMap));

            Assert.AreEqual(createdItem.Get("id"), 1);
            Assert.AreEqual(createdItem.Get("title"), "My Title");
            Assert.AreEqual(createdItem.Get("body"), "some body text");
            Assert.AreEqual(createdItem.Get("userId"), 1);
        }
コード例 #5
0
        public void testActionDeletePostEqual200()
        {
            RequestMap inputMap = new RequestMap();

            inputMap.Add("id", 1);
            inputMap.Add("title", "My Title");
            inputMap.Add("body", "Some Long text of Body");
            Post createdItem = Post.Create(new RequestMap(inputMap));



            Post deletedItem = createdItem.Delete();

            Assert.AreEqual(0, deletedItem.Count);
        }
コード例 #6
0
        public void TestAddIndexedWithOffset()
        {
            RequestMap map = new RequestMap();



            Assert.DoesNotThrow(() => { map.Add("map[1].name", "name1"); });
        }
コード例 #7
0
        public void TestAddIndexedWithOffset()
        {
            RequestMap map = new RequestMap();



            Assert.Throws <ArgumentOutOfRangeException> (() => { map.Add("map[1].name", "name1"); });
        }
コード例 #8
0
        public void TestAddIndexedWithOffset()
        {
            Assert.Inconclusive("No longer relavant");
            RequestMap map = new RequestMap();



            Assert.Throws <ArgumentOutOfRangeException> (() => { map.Add("map[1].name", "name1"); });
        }
コード例 #9
0
        public void TestAdd4List()
        {
            RequestMap map = new RequestMap();

            map.Add("key1", new List <String>()
            {
                "value1", "value2", "value3"
            });

            Assert.IsTrue(map.ContainsKey("key1"));
            Assert.AreEqual(3, ((List <String>)map ["key1"]).Count);
        }
コード例 #10
0
        public void TestAdd()
        {
            RequestMap map = new RequestMap();

            map.Add("key1", "value1");

            Assert.AreEqual(1, map.Count);
            Assert.IsTrue(map.ContainsKey("key1"));
            Assert.AreEqual("value1", map ["key1"]);

            //test negative
            Assert.IsFalse(map.ContainsKey("key2"));
        }
コード例 #11
0
        public void TestReplaceIndexed()
        {
            RequestMap map = new RequestMap();

            map.Add("map[].name", "name1");

            Assert.AreEqual(1, map.Count);

            Assert.AreNotSame(1, ((List <Dictionary <String, Object> >)map["map"]).Count);
            //checl is it finds the key
            Assert.AreEqual("name1", map ["map[0].name"]);

            // check if the first level map contains only one value
            Assert.AreNotSame(1, ((Dictionary <String, Object>)map ["map[0]"]).Count);


            map.Add("map[0].name", "name1_replaced");
            map.Add("map[0].surname", "surname");

            Assert.AreNotSame(2, ((List <Dictionary <String, Object> >)map["map"]).Count);

            Assert.AreEqual("name1_replaced", map ["map[0].name"]);
            Assert.AreEqual("surname", map ["map[0].surname"]);
        }
コード例 #12
0
        public void test_utf_8()
        {
            RequestMap parameters = new RequestMap();

            parameters.Add("JSONEcho.string", "мảŝťễřÇāŕď Ľẵвš ạאָđ мãśţēяĈẫřđ ĀקÏ ŕồçҝş...");



            JSONEcho response = JSONEcho.Create(parameters);

            String     bodyJson  = (String)response.Get("body");
            RequestMap response2 = new RequestMap(bodyJson);

            Assert.AreEqual("мảŝťễřÇāŕď Ľẵвš ạאָđ мãśţēяĈẫřđ ĀקÏ ŕồçҝş...", response2.Get("JSONEcho.string"));
        }
コード例 #13
0
        public void TestNestedAdd()
        {
            RequestMap map = new RequestMap();

            map.Add("key1.key2", "value1");

            Assert.AreEqual(1, map.Count);
            Assert.IsTrue(map.ContainsKey("key1"));
            Assert.IsTrue(map.ContainsKey("key1.key2"));
            Assert.AreEqual("value1", map ["key1.key2"]);
            Assert.AreNotSame(1, ((Dictionary <String, Object>)map["key1"]).Count);

            //test negative
            Assert.IsFalse(map.ContainsKey("key2"));
            Assert.IsFalse(map.ContainsKey("key2.key1"));
        }
コード例 #14
0
    public static void DownLoad(object param)
    {
        ThreadParam      tParam = param as ThreadParam;
        ManualResetEvent close  = tParam.close;
        AutoResetEvent   Wait   = tParam.waitfor;

        try
        {
            while (bContinue)
            {
                Wait.WaitOne();
                HttpRequest req    = null;
                string      strURI = null;
                while (bContinue)
                {
                    req    = null;
                    strURI = null;
                    lock (RequestMap)
                    {
                        if (RequestMap.Count == 0)
                        {
                            break;
                        }

                        bool bFindOrder = false;
                        foreach (KeyValuePair <string, HttpRequest> each in RequestMap)
                        {
                            if (req == null)
                            {
                                req = each.Value;
                            }
                            if (strURI == null)
                            {
                                strURI = each.Key;
                            }

                            if (each.Value != null)
                            {
                                if (each.Value.order != RequestStatus.Pause)
                                {
                                    req        = each.Value;
                                    strURI     = each.Key;
                                    bFindOrder = true;
                                    break;
                                }
                                else
                                {
                                    continue;
                                }
                            }
                        }

                        if (strURI != null)
                        {
                            //may be is retry
                            lock (DownloadMap)
                            {
                                if (!DownloadMap.ContainsKey(strURI))
                                {
                                    DownloadMap.Add(strURI, req);
                                }
                            }
                            RequestMap.Remove(strURI);
                        }
                    }

                    Socket sClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
                    //http1.1 download start
                    string strHost         = "";
                    string strPort         = "";
                    string strRelativePath = "";

                    if (!ParseURI(strURI, ref strHost, ref strPort, ref strRelativePath))
                    {
                        req.error = "404";
                        req.callback(ref req);
                        break;
                    }
                    else
                    {
                        EndPoint server = new IPEndPoint(IPAddress.Parse(strHost), System.Convert.ToInt32(strPort));
                        try
                        {
                            connectDone.Reset();
                            sClient.BeginConnect(server, new AsyncCallback(ConnectCallback), sClient);
                            connectDone.WaitOne(2000, false);
                            if (!sClient.Connected)
                            {
                                Log.LogInfo("connect time out");
                                //repush to download again
                                lock (RequestMap)
                                {
                                    if (!RequestMap.ContainsKey(strURI))
                                    {
                                        req.error = null;
                                        req.order = 0;
                                        RequestMap.Add(strURI, req);
                                    }
                                }
                                continue;
                            }
                        }
                        catch
                        {
                            req.error = "connect failed";
                            req.callback(ref req);

                            //repush to download again
                            lock (RequestMap)
                            {
                                if (!RequestMap.ContainsKey(strURI))
                                {
                                    req.error = null;
                                    req.order = 0;
                                    RequestMap.Add(strURI, req);
                                }
                            }
                            continue;
                        }

                        if (!sClient.Connected)
                        {
                            req.error = "connect failed";
                            req.callback(ref req);

                            //repush to download again
                            lock (RequestMap)
                            {
                                if (!RequestMap.ContainsKey(strURI))
                                {
                                    req.error = null;
                                    req.order = 0;
                                    RequestMap.Add(strURI, req);
                                }
                            }
                            continue;
                        }
                        string strSend = string.Format(strHttpVer, strRelativePath, strHost, strPort, req.cbstart);
                        Log.LogInfo("send packet:" + strSend);
                        byte[] bySend = System.Text.Encoding.UTF8.GetBytes(string.Format(strHttpVer, strRelativePath, strHost, strPort, req.cbstart));
                        sClient.Send(bySend);
                        int nByteRecved = 0;
                        int nNewLine    = 0;
                        //recv http head
                        MemoryStream ms         = new MemoryStream();
                        byte[]       byteRecved = new byte[1];
                        while (true)
                        {
                            if (!bContinue)
                            {
                                break;
                            }
                            try
                            {
                                nByteRecved = sClient.Receive(byteRecved, 1, 0);
                            }
                            catch (Exception exp)
                            {
                                break;
                            }
                            if (nByteRecved <= 0)
                            {
                                break;
                            }
                            ms.Write(byteRecved, 0, 1);
                            if (byteRecved[0] == '\n')
                            {
                                nNewLine++;
                                if (System.Text.Encoding.UTF8.GetString(ms.GetBuffer()).Contains("\r\n\r\n"))
                                {
                                    break;
                                }
                            }
                        }
                        if (!sClient.Connected || !bContinue)
                        {
                            req.error = "recv interrupt";
                            req.callback(ref req);
                            lock (RequestMap)
                            {
                                if (!RequestMap.ContainsKey(strURI))
                                {
                                    req.error = null;
                                    req.order = 0;
                                    RequestMap.Add(strURI, req);
                                }
                            }
                            continue;
                        }
                        nByteRecved = 0;
                        string strHead = System.Text.Encoding.UTF8.GetString(ms.GetBuffer());
                        Log.LogInfo("http recv:" + strHead);
                        string strHeadLower = strHead.ToLower();
                        //check http1.1 return code
                        int      nReturnCode    = 0;
                        long     nContentLength = 0;
                        int      nRangeStart    = 0;
                        int      nRangeStop     = 0;
                        string[] strResponse    = new string[nNewLine];
                        char[]   split          = { '\n' };
                        strResponse = strHeadLower.Split(split);
                        for (int i = 0; i < strResponse.Length; ++i)
                        {
                            if (strResponse[i].Contains("http/"))
                            {
                                string strStatus = strResponse[i];
                                nReturnCode = System.Convert.ToInt32(strStatus.Substring(9, 3));
                                Log.LogInfo("http result:" + nReturnCode.ToString());
                            }
                            else if (strResponse[i].Contains("content-length:"))
                            {
                                string   strLength = strResponse[i];
                                string[] strSplit  = strLength.Split(new char[] { ' ' }, 2);
                                nContentLength = System.Convert.ToInt64(strSplit[1]);
                                if (req.cbstart == 0)
                                {
                                    req.cbsize = nContentLength;
                                    req.callback(ref req);
                                }
                            }
                            else if (strResponse[i].Contains("tranfer-encoding:chunked"))
                            {
                                Log.LogInfo("error !!! can not read chunked data");
                            }

                            if (nReturnCode != 0 && nContentLength != 0)
                            {
                                break;
                            }
                        }

                        ms.Close();
                        ms = null;

                        if (nReturnCode != 206 && nReturnCode != 200)
                        {
                            req.error = nReturnCode.ToString();
                            req.callback(ref req);
                            lock (RequestMap)
                            {
                                if (!RequestMap.ContainsKey(strURI))
                                {
                                    req.error = null;
                                    req.order = 0;
                                    RequestMap.Add(strURI, req);
                                }
                            }

                            sClient.Close();
                            continue;
                        }

                        FileStream fs = File.Open(req.strLocalPath, FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite);
                        if (req.cbstart != 0)
                        {
                            fs.Seek(req.cbstart, SeekOrigin.Begin);
                        }

                        //calc kbps

                        //total recved / total time => kbps
                        long   nTotalRecved = req.cbstart;
                        int    nLoop        = 0;
                        byte[] memory       = new byte[10 * 1024];
                        while (true)
                        {
                            long nTickBegin = DateTime.Now.Ticks;
                            if (!bContinue)
                            {
                                break;
                            }
                            try
                            {
                                nByteRecved = sClient.Receive(memory, 10 * 1024, SocketFlags.None);
                            }
                            catch (Exception exp)
                            {
                                break;
                            }
                            if (nByteRecved <= 0)
                            {
                                break;
                            }
                            nLoop++;

                            //Loader.LogErr("recv bytes:" + nByteRecved.ToString());
                            fs.Write(memory, 0, nByteRecved);
                            fs.Flush();
                            nTotalRecved  += nByteRecved;
                            req.cboffset   = nTotalRecved;
                            req.loadBytes += nByteRecved;
                            if (nTotalRecved == nContentLength)
                            {
                                break;
                            }
                            if (nLoop >= 10)
                            {
                                req.callback(ref req);
                                nLoop = 0;
                            }
                        }

                        sClient.Close();
                        //Loader.LogErr("file transfer result:" + nByteRecved.ToString());
                        req.cboffset = fs.Seek(0, SeekOrigin.Current);
                        fs.Flush();
                        fs.Close();
                        req.callback(ref req);
                        req.bDone = true;
                    }
                }
            }
            if (close != null)
            {
                Log.LogInfo("thread quit signal open");
                close.Set();
                close = null;
            }
        }
        catch (Exception exp)
        {
            Debug.LogError(exp.Message + "|" + exp.StackTrace + " download thread crashed");
        }
        finally
        {
            if (close != null)
            {
                close.Set();
                close = null;
            }
        }
    }