Aliyun.OSS.Test.TestClass.ObjectTestClass.ObjectSignedUriTest.PutPreSignedUriDefaultNegativeTest C# (CSharp) Method

PutPreSignedUriDefaultNegativeTest() private method

private PutPreSignedUriDefaultNegativeTest ( ) : void
return void
        public void PutPreSignedUriDefaultNegativeTest()
        {
            var testStr = FileUtils.GenerateOneKb();
            var bytes = Encoding.ASCII.GetBytes(testStr);

            HttpWebRequest req = null;
            HttpWebResponse res = null;

            var now = DateTime.Now;
            //set expiration time to 5 seconds before
            var expireDate = now.AddSeconds(-5);
            var targetObject = OssTestUtils.GetObjectKey(_className);
            var uri = _ossClient.GeneratePresignedUri(_bucketName, targetObject, expireDate, SignHttpMethod.Put);
            try
            {
                req = WebRequest.Create(uri) as HttpWebRequest;
                req.Method = "PUT";
                using (var stream = req.GetRequestStream())
                {
                    stream.Write(bytes, 0, bytes.Length);
                }
                res = req.GetResponse() as HttpWebResponse;
                Assert.Fail("response fail for expired URI");
            }
            catch (WebException e)
            {
                Assert.IsTrue(e.Message.Contains("403"),
                    string.Format("Unexpected exception: {0}", e.Message));
            }
            finally
            {
                if (req != null) req.Abort();
                if (res != null) res.Close();
            }
        }