2
2
using Qiniu . Http ;
3
3
using System ;
4
4
using System . Collections . Generic ;
5
+ using System . Text ;
5
6
using Newtonsoft . Json ;
6
7
using Qiniu . Util ;
7
8
using Qiniu . Tests ;
@@ -82,13 +83,14 @@ public void UploadFileV2Test()
82
83
System . IO . File . Delete ( filePath ) ;
83
84
}
84
85
85
- [ Test ]
86
- public void UploadFileWithPersistTypeTest ( )
86
+ [ TestCaseSource ( typeof ( OperationManagerTests ) , nameof ( OperationManagerTests . PfopOptionsTestCases ) ) ]
87
+ public void UploadFileWithPersistOptionsTest ( int type , string workflowId )
87
88
{
88
89
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" ;
91
92
93
+ // generate file to upload
92
94
string tempPath = System . IO . Path . GetTempPath ( ) ;
93
95
int rnd = new Random ( ) . Next ( 1 , 100000 ) ;
94
96
string filePath = tempPath + "resumeFile" + rnd . ToString ( ) ;
@@ -99,37 +101,71 @@ public void UploadFileWithPersistTypeTest()
99
101
sw . Close ( ) ;
100
102
stream . Close ( ) ;
101
103
104
+ // generate put policy
102
105
PutPolicy putPolicy = new PutPolicy ( ) ;
103
- putPolicy . Scope = Bucket + ":" + key ;
106
+ putPolicy . Scope = string . Join ( ":" , bucketName , key ) ;
104
107
putPolicy . SetExpires ( 3600 ) ;
105
108
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
110
133
Config config = new Config ( ) ;
111
- config . Zone = Zone . ZONE_CN_East ;
112
134
config . UseHttps = true ;
113
135
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 ) ;
118
139
Console . WriteLine ( "form upload result: " + result . ToString ( ) ) ;
119
140
Assert . AreEqual ( ( int ) HttpCode . OK , result . Code ) ;
120
141
System . IO . File . Delete ( filePath ) ;
121
142
143
+ // get persist task info
122
144
Dictionary < string , object > dict = JsonConvert . DeserializeObject < Dictionary < string , object > > ( result . Text . ToString ( ) ) ;
123
145
Assert . IsTrue ( dict . ContainsKey ( "persistentId" ) ) ;
124
146
OperationManager manager = new OperationManager ( mac , config ) ;
125
147
PrefopResult prefopRet = manager . Prefop ( dict [ "persistentId" ] . ToString ( ) ) ;
148
+
149
+ // assert the result
126
150
if ( prefopRet . Code != ( int ) HttpCode . OK )
127
151
{
128
152
Assert . Fail ( "prefop error: " + prefopRet . ToString ( ) ) ;
129
153
}
130
- Assert . AreEqual ( 1 , prefopRet . Result . Type ) ;
154
+
131
155
Assert . IsNotNull ( prefopRet . Result . CreationDate ) ;
132
156
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
+ }
133
169
}
134
170
}
135
171
}
0 commit comments