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,70 @@ 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
+ 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
110
132
Config config = new Config ( ) ;
111
- config . Zone = Zone . ZONE_CN_East ;
112
133
config . UseHttps = true ;
113
134
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 ) ;
118
138
Console . WriteLine ( "form upload result: " + result . ToString ( ) ) ;
119
139
Assert . AreEqual ( ( int ) HttpCode . OK , result . Code ) ;
120
140
System . IO . File . Delete ( filePath ) ;
121
141
142
+ // get persist task info
122
143
Dictionary < string , object > dict = JsonConvert . DeserializeObject < Dictionary < string , object > > ( result . Text . ToString ( ) ) ;
123
144
Assert . IsTrue ( dict . ContainsKey ( "persistentId" ) ) ;
124
145
OperationManager manager = new OperationManager ( mac , config ) ;
125
146
PrefopResult prefopRet = manager . Prefop ( dict [ "persistentId" ] . ToString ( ) ) ;
147
+
148
+ // assert the result
126
149
if ( prefopRet . Code != ( int ) HttpCode . OK )
127
150
{
128
151
Assert . Fail ( "prefop error: " + prefopRet . ToString ( ) ) ;
129
152
}
130
- Assert . AreEqual ( 1 , prefopRet . Result . Type ) ;
153
+
131
154
Assert . IsNotNull ( prefopRet . Result . CreationDate ) ;
132
155
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
+ }
133
168
}
134
169
}
135
170
}
0 commit comments