22using Qiniu . Http ;
33using System ;
44using System . Collections . Generic ;
5+ using System . Text ;
56using Newtonsoft . Json ;
67using Qiniu . Util ;
78using Qiniu . Tests ;
@@ -82,13 +83,14 @@ public void UploadFileV2Test()
8283 System . IO . File . Delete ( filePath ) ;
8384 }
8485
85- [ Test ]
86- public void UploadFileWithPersistTypeTest ( )
86+ [ TestCaseSource ( typeof ( OperationManagerTests ) , nameof ( OperationManagerTests . PfopOptionsTestCases ) ) ]
87+ public void UploadFileWithPersistOptionsTest ( int type , string workflowId )
8788 {
8889 Mac mac = new Mac ( AccessKey , SecretKey ) ;
89- Random rand = new Random ( ) ;
90- string key = string . Format ( "UploadFileTest_{0}.dat" , rand . Next ( ) ) ;
90+ string bucketName = Bucket ;
91+ string key = "test-pfop/upload-file" ;
9192
93+ // generate file to upload
9294 string tempPath = System . IO . Path . GetTempPath ( ) ;
9395 int rnd = new Random ( ) . Next ( 1 , 100000 ) ;
9496 string filePath = tempPath + "resumeFile" + rnd . ToString ( ) ;
@@ -99,37 +101,70 @@ public void UploadFileWithPersistTypeTest()
99101 sw . Close ( ) ;
100102 stream . Close ( ) ;
101103
104+ // generate put policy
102105 PutPolicy putPolicy = new PutPolicy ( ) ;
103- putPolicy . Scope = Bucket + ":" + key ;
106+ putPolicy . Scope = string . Join ( ":" , bucketName , key ) ;
104107 putPolicy . SetExpires ( 3600 ) ;
105108 putPolicy . DeleteAfterDays = 1 ;
106- string saveEntry = Base64 . UrlSafeBase64Encode ( Bucket + ":pfop-test_avinfo" ) ;
107- putPolicy . PersistentOps = "avinfo|saveas/" + saveEntry ;
108- putPolicy . PersistentType = 1 ;
109- string token = Auth . CreateUploadToken ( mac , putPolicy . ToJsonString ( ) ) ;
109+
110+ StringBuilder persistentKeyBuilder = new StringBuilder ( "test-pfop/test-pfop-by-upload" ) ;
111+ if ( type > 0 )
112+ {
113+ persistentKeyBuilder . Append ( "type_" + type ) ;
114+ putPolicy . PersistentType = type ;
115+ }
116+
117+ if ( ! string . IsNullOrEmpty ( workflowId ) )
118+ {
119+ putPolicy . PersistentWorkflowTemplateId = workflowId ;
120+ }
121+ else
122+ {
123+ string saveEntry = Base64 . UrlSafeBase64Encode ( String . Join (
124+ ":" ,
125+ bucketName ,
126+ persistentKeyBuilder . ToString ( )
127+ ) ) ;
128+ putPolicy . PersistentOps = "avinfo|saveas/" + saveEntry ;
129+ }
130+
131+ // upload
110132 Config config = new Config ( ) ;
111- config . Zone = Zone . ZONE_CN_East ;
112133 config . UseHttps = true ;
113134 config . UseCdnDomains = true ;
114- FormUploader target = new FormUploader ( config ) ;
115- PutExtra extra = new PutExtra ( ) ;
116- extra . Version = "v2" ;
117- HttpResult result = target . UploadFile ( filePath , key , token , extra ) ;
135+ string token = Auth . CreateUploadToken ( mac , putPolicy . ToJsonString ( ) ) ;
136+ FormUploader uploader = new FormUploader ( config ) ;
137+ HttpResult result = uploader . UploadFile ( filePath , key , token , null ) ;
118138 Console . WriteLine ( "form upload result: " + result . ToString ( ) ) ;
119139 Assert . AreEqual ( ( int ) HttpCode . OK , result . Code ) ;
120140 System . IO . File . Delete ( filePath ) ;
121141
142+ // get persist task info
122143 Dictionary < string , object > dict = JsonConvert . DeserializeObject < Dictionary < string , object > > ( result . Text . ToString ( ) ) ;
123144 Assert . IsTrue ( dict . ContainsKey ( "persistentId" ) ) ;
124145 OperationManager manager = new OperationManager ( mac , config ) ;
125146 PrefopResult prefopRet = manager . Prefop ( dict [ "persistentId" ] . ToString ( ) ) ;
147+
148+ // assert the result
126149 if ( prefopRet . Code != ( int ) HttpCode . OK )
127150 {
128151 Assert . Fail ( "prefop error: " + prefopRet . ToString ( ) ) ;
129152 }
130- Assert . AreEqual ( 1 , prefopRet . Result . Type ) ;
153+
131154 Assert . IsNotNull ( prefopRet . Result . CreationDate ) ;
132155 Assert . IsNotEmpty ( prefopRet . Result . CreationDate ) ;
156+
157+ if ( type == 1 )
158+ {
159+ Assert . AreEqual ( 1 , prefopRet . Result . Type ) ;
160+ }
161+
162+ if ( ! string . IsNullOrEmpty ( workflowId ) )
163+ {
164+ Assert . IsNotNull ( prefopRet . Result . TaskFrom ) ;
165+ Assert . IsNotEmpty ( prefopRet . Result . TaskFrom ) ;
166+ Assert . IsTrue ( prefopRet . Result . TaskFrom . Contains ( workflowId ) ) ;
167+ }
133168 }
134169 }
135170}
0 commit comments