コード例 #1
0
        /// <exception cref="System.IO.IOException"/>
        public virtual void TestResponseCode()
        {
            WebHdfsFileSystem webhdfs = (WebHdfsFileSystem)fs;
            Path root = new Path("/");
            Path dir  = new Path("/test/testUrl");

            NUnit.Framework.Assert.IsTrue(webhdfs.Mkdirs(dir));
            Path file = new Path("/test/file");
            FSDataOutputStream @out = webhdfs.Create(file);

            @out.Write(1);
            @out.Close();
            {
                //test GETHOMEDIRECTORY
                Uri url = webhdfs.ToUrl(GetOpParam.OP.Gethomedirectory, root);
                HttpURLConnection            conn = (HttpURLConnection)url.OpenConnection();
                IDictionary <object, object> m    = WebHdfsTestUtil.ConnectAndGetJson(conn, HttpServletResponse
                                                                                      .ScOk);
                NUnit.Framework.Assert.AreEqual(WebHdfsFileSystem.GetHomeDirectoryString(ugi), m[
                                                    typeof(Path).Name]);
                conn.Disconnect();
            }
            {
                //test GETHOMEDIRECTORY with unauthorized doAs
                Uri url = webhdfs.ToUrl(GetOpParam.OP.Gethomedirectory, root, new DoAsParam(ugi.GetShortUserName
                                                                                                () + "proxy"));
                HttpURLConnection conn = (HttpURLConnection)url.OpenConnection();
                conn.Connect();
                NUnit.Framework.Assert.AreEqual(HttpServletResponse.ScForbidden, conn.GetResponseCode
                                                    ());
                conn.Disconnect();
            }
            {
                //test set owner with empty parameters
                Uri url = webhdfs.ToUrl(PutOpParam.OP.Setowner, dir);
                HttpURLConnection conn = (HttpURLConnection)url.OpenConnection();
                conn.Connect();
                NUnit.Framework.Assert.AreEqual(HttpServletResponse.ScBadRequest, conn.GetResponseCode
                                                    ());
                conn.Disconnect();
            }
            {
                //test set replication on a directory
                HttpOpParam.OP    op   = PutOpParam.OP.Setreplication;
                Uri               url  = webhdfs.ToUrl(op, dir);
                HttpURLConnection conn = (HttpURLConnection)url.OpenConnection();
                conn.SetRequestMethod(op.GetType().ToString());
                conn.Connect();
                NUnit.Framework.Assert.AreEqual(HttpServletResponse.ScOk, conn.GetResponseCode());
                NUnit.Framework.Assert.IsFalse(webhdfs.SetReplication(dir, (short)1));
                conn.Disconnect();
            }
            {
                //test get file status for a non-exist file.
                Path p   = new Path(dir, "non-exist");
                Uri  url = webhdfs.ToUrl(GetOpParam.OP.Getfilestatus, p);
                HttpURLConnection conn = (HttpURLConnection)url.OpenConnection();
                conn.Connect();
                NUnit.Framework.Assert.AreEqual(HttpServletResponse.ScNotFound, conn.GetResponseCode
                                                    ());
                conn.Disconnect();
            }
            {
                //test set permission with empty parameters
                HttpOpParam.OP    op   = PutOpParam.OP.Setpermission;
                Uri               url  = webhdfs.ToUrl(op, dir);
                HttpURLConnection conn = (HttpURLConnection)url.OpenConnection();
                conn.SetRequestMethod(op.GetType().ToString());
                conn.Connect();
                NUnit.Framework.Assert.AreEqual(HttpServletResponse.ScOk, conn.GetResponseCode());
                NUnit.Framework.Assert.AreEqual(0, conn.GetContentLength());
                NUnit.Framework.Assert.AreEqual(MediaType.ApplicationOctetStream, conn.GetContentType
                                                    ());
                NUnit.Framework.Assert.AreEqual((short)0x1ed, webhdfs.GetFileStatus(dir).GetPermission
                                                    ().ToShort());
                conn.Disconnect();
            }
            {
                //test append.
                AppendTestUtil.TestAppend(fs, new Path(dir, "append"));
            }
            {
                //test NamenodeAddressParam not set.
                HttpOpParam.OP    op   = PutOpParam.OP.Create;
                Uri               url  = webhdfs.ToUrl(op, dir);
                HttpURLConnection conn = (HttpURLConnection)url.OpenConnection();
                conn.SetRequestMethod(op.GetType().ToString());
                conn.SetDoOutput(false);
                conn.SetInstanceFollowRedirects(false);
                conn.Connect();
                string redirect = conn.GetHeaderField("Location");
                conn.Disconnect();
                //remove NamenodeAddressParam
                WebHdfsFileSystem.Log.Info("redirect = " + redirect);
                int    i        = redirect.IndexOf(NamenodeAddressParam.Name);
                int    j        = redirect.IndexOf("&", i);
                string modified = Sharpen.Runtime.Substring(redirect, 0, i - 1) + Sharpen.Runtime.Substring
                                      (redirect, j);
                WebHdfsFileSystem.Log.Info("modified = " + modified);
                //connect to datanode
                conn = (HttpURLConnection) new Uri(modified).OpenConnection();
                conn.SetRequestMethod(op.GetType().ToString());
                conn.SetDoOutput(op.GetDoOutput());
                conn.Connect();
                NUnit.Framework.Assert.AreEqual(HttpServletResponse.ScBadRequest, conn.GetResponseCode
                                                    ());
            }
            {
                //test jsonParse with non-json type.
                HttpOpParam.OP    op   = GetOpParam.OP.Open;
                Uri               url  = webhdfs.ToUrl(op, file);
                HttpURLConnection conn = (HttpURLConnection)url.OpenConnection();
                conn.SetRequestMethod(op.GetType().ToString());
                conn.Connect();
                try
                {
                    WebHdfsFileSystem.JsonParse(conn, false);
                    Fail();
                }
                catch (IOException ioe)
                {
                    WebHdfsFileSystem.Log.Info("GOOD", ioe);
                }
                conn.Disconnect();
            }
            {
                //test create with path containing spaces
                HttpOpParam.OP    op   = PutOpParam.OP.Create;
                Path              path = new Path("/test/path with spaces");
                Uri               url  = webhdfs.ToUrl(op, path);
                HttpURLConnection conn = (HttpURLConnection)url.OpenConnection();
                conn.SetRequestMethod(op.GetType().ToString());
                conn.SetDoOutput(false);
                conn.SetInstanceFollowRedirects(false);
                string redirect;
                try
                {
                    conn.Connect();
                    NUnit.Framework.Assert.AreEqual(HttpServletResponse.ScTemporaryRedirect, conn.GetResponseCode
                                                        ());
                    redirect = conn.GetHeaderField("Location");
                }
                finally
                {
                    conn.Disconnect();
                }
                conn = (HttpURLConnection) new Uri(redirect).OpenConnection();
                conn.SetRequestMethod(op.GetType().ToString());
                conn.SetDoOutput(op.GetDoOutput());
                try
                {
                    conn.Connect();
                    NUnit.Framework.Assert.AreEqual(HttpServletResponse.ScCreated, conn.GetResponseCode
                                                        ());
                }
                finally
                {
                    conn.Disconnect();
                }
            }
        }