public void AddContentLength_ToTrue_HeaderContainsContentLength()
        {
            GeckoMIMEInputStream stream = new GeckoMIMEInputStream();
            stream.AddContentLength = true;

            Assert.IsTrue(ReadGeckoMIMEInputStreamAsUTF8(stream).Contains("Content-Length"));
        }
        public void SetData_SimpleData_HeaderContainsSimpleData()
        {
            GeckoMIMEInputStream stream = new GeckoMIMEInputStream();
            string simpleData = "id=hello";
            stream.SetData(simpleData);

            Assert.IsTrue(ReadGeckoMIMEInputStreamAsUTF8(stream).Contains(simpleData));
        }
        public void AddHeader_AddValidHeaderEntry_HeaderContainsHeaderEntry()
        {
            GeckoMIMEInputStream stream = new GeckoMIMEInputStream();
            stream.AddHeader("Content-Type", "application/x-www-form-urlencoded");

            string header = ReadGeckoMIMEInputStreamAsUTF8(stream);
            Assert.IsTrue(header.Contains("Content-Type"));
            Assert.IsTrue(header.Contains("application/x-www-form-urlencoded"));
        }
        /// <summary>
        /// This is for unit tests only.
        /// </summary>
        /// <param name="stream"></param>
        /// <returns></returns>
        internal string ReadGeckoMIMEInputStreamAsUTF8(GeckoMIMEInputStream stream)
        {
            IntPtr buffer = Marshal.AllocCoTaskMem(2048);
            var count = stream.InputStream.Read(buffer, 2048);
            byte[] temp = new byte[2048];
            for (int i = 0; i < count; ++i)
            {
                temp[i] = Marshal.ReadByte(buffer, i);
            }

            return System.Text.UTF8Encoding.UTF8.GetString(temp).Trim();
        }
        /// <summary>
        ///  Navigates to the specified URL using the given load flags, referrer and post data
        ///  In order to find out when Navigate has finished attach a handler to NavigateFinishedNotifier.NavigateFinished.
        /// </summary>
        /// <param name="url">The url to navigate to.  If the url is empty or null, the browser does not navigate and the method returns false.</param>
        /// <param name="loadFlags">Flags which specify how the page is loaded.</param>
        /// <param name="referrer">The referring URL, or null.</param>
        /// <param name="postData">post data and headers, or null</param>
        /// <returns></returns>
        public bool Navigate(string url, GeckoLoadFlags loadFlags, string referrer, GeckoMIMEInputStream postData)
        {
            if (string.IsNullOrEmpty(url))
                return false;

            if (!IsHandleCreated)
                throw new InvalidOperationException("Cannot call Navigate() before the window handle is created.");

            // WebNav.LoadURI throws an exception if we try to open a file that doesn't exist...
            Uri created;
            if (Uri.TryCreate(url, UriKind.RelativeOrAbsolute, out created) && created.IsAbsoluteUri && created.IsFile)
            {
                if (!File.Exists(created.LocalPath) && !Directory.Exists(created.LocalPath))
                    return false;
            }

            nsIURI referrerUri = null;
            if (!string.IsNullOrEmpty(referrer))
            {
                referrerUri = Xpcom.GetService<nsIIOService>("@mozilla.org/network/io-service;1").NewURI(new nsAUTF8String(referrer), null, null);
            }

            WebNav.LoadURI(url, (uint)loadFlags, referrerUri, postData.InputStream, null);

            return true;
        }