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,71 @@ 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+ string fops = null ;
118+ if ( ! string . IsNullOrEmpty ( workflowId ) )
119+ {
120+ putPolicy . PersistentWorkflowTemplateId = workflowId ;
121+ }
122+ else
123+ {
124+ string saveEntry = Base64 . UrlSafeBase64Encode ( String . Join (
125+ ":" ,
126+ bucketName ,
127+ persistentKeyBuilder . ToString ( )
128+ ) ) ;
129+ putPolicy . PersistentOps = "avinfo|saveas/" + saveEntry ;
130+ }
131+
132+ // upload
110133 Config config = new Config ( ) ;
111- config . Zone = Zone . ZONE_CN_East ;
112134 config . UseHttps = true ;
113135 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 ) ;
136+ string token = Auth . CreateUploadToken ( mac , putPolicy . ToJsonString ( ) ) ;
137+ FormUploader uploader = new FormUploader ( config ) ;
138+ HttpResult result = uploader . UploadFile ( filePath , key , token , null ) ;
118139 Console . WriteLine ( "form upload result: " + result . ToString ( ) ) ;
119140 Assert . AreEqual ( ( int ) HttpCode . OK , result . Code ) ;
120141 System . IO . File . Delete ( filePath ) ;
121142
143+ // get persist task info
122144 Dictionary < string , object > dict = JsonConvert . DeserializeObject < Dictionary < string , object > > ( result . Text . ToString ( ) ) ;
123145 Assert . IsTrue ( dict . ContainsKey ( "persistentId" ) ) ;
124146 OperationManager manager = new OperationManager ( mac , config ) ;
125147 PrefopResult prefopRet = manager . Prefop ( dict [ "persistentId" ] . ToString ( ) ) ;
148+
149+ // assert the result
126150 if ( prefopRet . Code != ( int ) HttpCode . OK )
127151 {
128152 Assert . Fail ( "prefop error: " + prefopRet . ToString ( ) ) ;
129153 }
130- Assert . AreEqual ( 1 , prefopRet . Result . Type ) ;
154+
131155 Assert . IsNotNull ( prefopRet . Result . CreationDate ) ;
132156 Assert . IsNotEmpty ( prefopRet . Result . CreationDate ) ;
157+
158+ if ( type == 1 )
159+ {
160+ Assert . AreEqual ( 1 , prefopRet . Result . Type ) ;
161+ }
162+
163+ if ( ! string . IsNullOrEmpty ( workflowId ) )
164+ {
165+ Assert . IsNotNull ( prefopRet . Result . TaskFrom ) ;
166+ Assert . IsNotEmpty ( prefopRet . Result . TaskFrom ) ;
167+ Assert . IsTrue ( prefopRet . Result . TaskFrom . Contains ( workflowId ) ) ;
168+ }
133169 }
134170 }
135171}
0 commit comments