Skip to content

Commit 94b0b70

Browse files
authored
Merge pull request #160 from agin719/cos-v4-dev
add ut
2 parents 48d7d86 + 5fe8d16 commit 94b0b70

File tree

8 files changed

+775
-25
lines changed

8 files changed

+775
-25
lines changed

ci_media_test.go

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
package cos
22

33
import (
4+
"bytes"
45
"context"
6+
"crypto/rand"
7+
"encoding/xml"
8+
"fmt"
9+
"io/ioutil"
510
"net/http"
11+
"reflect"
612
"testing"
713
)
814

@@ -168,3 +174,134 @@ func TestCIService_DescribeMediaProcessBuckets(t *testing.T) {
168174
t.Fatalf("CI.DescribeMediaProcessBuckets returned error: %v", err)
169175
}
170176
}
177+
178+
func TestCIService_GetMediaInfo(t *testing.T) {
179+
setup()
180+
defer teardown()
181+
182+
res_xml := `<Response>
183+
<MediaInfo>
184+
<Format>
185+
<Bitrate>1014.950000</Bitrate>
186+
<Duration>10.125000</Duration>
187+
<FormatLongName>QuickTime / MOV</FormatLongName>
188+
<FormatName>mov,mp4,m4a,3gp,3g2,mj2</FormatName>
189+
<NumProgram>0</NumProgram>
190+
<NumStream>2</NumStream>
191+
<Size>1284547</Size>
192+
<StartTime>0.000000</StartTime>
193+
</Format>
194+
<Stream>
195+
<Audio>
196+
<Bitrate>70.451000</Bitrate>
197+
<Channel>1</Channel>
198+
<ChannelLayout>mono</ChannelLayout>
199+
<CodecLongName>AAC (Advanced Audio Coding)</CodecLongName>
200+
<CodecName>aac</CodecName>
201+
<CodecTag>0x6134706d</CodecTag>
202+
<CodecTagString>mp4a</CodecTagString>
203+
<CodecTimeBase>1/44100</CodecTimeBase>
204+
<Duration>0.440294</Duration>
205+
<Index>1</Index>
206+
<Language>und</Language>
207+
<SampleFmt>fltp</SampleFmt>
208+
<SampleRate>44100</SampleRate>
209+
<StartTime>0.000000</StartTime>
210+
<Timebase>1/44100</Timebase>
211+
</Audio>
212+
<Subtitle/>
213+
<Video>
214+
<AvgFps>24/1</AvgFps>
215+
<Bitrate>938.164000</Bitrate>
216+
<CodecLongName>H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10</CodecLongName>
217+
<CodecName>h264</CodecName>
218+
<CodecTag>0x31637661</CodecTag>
219+
<CodecTagString>avc1</CodecTagString>
220+
<CodecTimeBase>1/12288</CodecTimeBase>
221+
<Dar>40:53</Dar>
222+
<Duration>0.124416</Duration>
223+
<Fps>24.500000</Fps>
224+
<HasBFrame>2</HasBFrame>
225+
<Height>1280</Height>
226+
<Index>0</Index>
227+
<Language>und</Language>
228+
<Level>32</Level>
229+
<NumFrames>243</NumFrames>
230+
<PixFormat>yuv420p</PixFormat>
231+
<Profile>High</Profile>
232+
<RefFrames>1</RefFrames>
233+
<Sar>25600:25599</Sar>
234+
<StartTime>0.000000</StartTime>
235+
<Timebase>1/12288</Timebase>
236+
<Width>966</Width>
237+
</Video>
238+
</Stream>
239+
</MediaInfo>
240+
</Response>`
241+
242+
mux.HandleFunc("/test", func(w http.ResponseWriter, r *http.Request) {
243+
testMethod(t, r, http.MethodGet)
244+
v := values{
245+
"ci-process": "videoinfo",
246+
}
247+
testFormValues(t, r, v)
248+
fmt.Fprint(w, res_xml)
249+
})
250+
251+
res, _, err := client.CI.GetMediaInfo(context.Background(), "test", nil)
252+
if err != nil {
253+
t.Fatalf("CI.GetMediaInfo returned error: %v", err)
254+
}
255+
want := &GetMediaInfoResult{}
256+
err = xml.Unmarshal([]byte(res_xml), want)
257+
if err != nil {
258+
t.Errorf("Bucket.GetMediaInfo Unmarshal returned error %+v", err)
259+
}
260+
if !reflect.DeepEqual(res, want) {
261+
t.Errorf("Bucket.GetMediaInfo returned %+v, want %+v", res, want)
262+
}
263+
}
264+
265+
func TestCIService_GetSnapshot(t *testing.T) {
266+
setup()
267+
defer teardown()
268+
269+
opt := &GetSnapshotOptions{
270+
Time: 1,
271+
Height: 100,
272+
Width: 100,
273+
Format: "jpg",
274+
Rotate: "auto",
275+
Mode: "exactframe",
276+
}
277+
data := make([]byte, 1234*2)
278+
rand.Read(data)
279+
280+
mux.HandleFunc("/test", func(w http.ResponseWriter, r *http.Request) {
281+
testMethod(t, r, http.MethodGet)
282+
v := values{
283+
"ci-process": "snapshot",
284+
"time": "1",
285+
"format": "jpg",
286+
"width": "100",
287+
"height": "100",
288+
"rotate": "auto",
289+
"mode": "exactframe",
290+
}
291+
testFormValues(t, r, v)
292+
w.Write(data)
293+
})
294+
295+
resp, err := client.CI.GetSnapshot(context.Background(), "test", opt)
296+
if err != nil {
297+
t.Fatalf("CI.GetSnapshot returned error: %v", err)
298+
}
299+
defer resp.Body.Close()
300+
bs, err := ioutil.ReadAll(resp.Body)
301+
if err != nil {
302+
t.Fatalf("CI.GetSnapshot ReadAll returned error: %v", err)
303+
}
304+
if bytes.Compare(bs, data) != 0 {
305+
t.Errorf("Bucket.GetSnapshot Compare failed")
306+
}
307+
}

ci_test.go

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,63 @@ func TestCIService_ImageRecognition(t *testing.T) {
198198
}
199199
}
200200

201+
func TestCIService_ImageAuditing(t *testing.T) {
202+
setup()
203+
defer teardown()
204+
name := "test.jpg"
205+
opt := &ImageRecognitionOptions{
206+
CIProcess: "sensitive-content-recognition",
207+
DetectType: "porn",
208+
MaxFrames: 1,
209+
}
210+
mux.HandleFunc("/test.jpg", func(w http.ResponseWriter, r *http.Request) {
211+
testMethod(t, r, "GET")
212+
vs := values{
213+
"ci-process": "sensitive-content-recognition",
214+
"detect-type": "porn",
215+
"max-frames": "1",
216+
}
217+
testFormValues(t, r, vs)
218+
fmt.Fprint(w, `<RecognitionResult>
219+
<Result>1</Result>
220+
<Label>Porn</Label>
221+
<SubLabel>SexBehavior</SubLabel>
222+
<Score>90</Score>
223+
<PornInfo>
224+
<Code>0</Code>
225+
<Msg>OK</Msg>
226+
<HitFlag>1</HitFlag>
227+
<Label>xxx</Label>
228+
<SubLabel>SexBehavior</SubLabel>
229+
<Score>100</Score>
230+
</PornInfo>
231+
</RecognitionResult>`)
232+
})
233+
234+
want := &ImageRecognitionResult{
235+
XMLName: xml.Name{Local: "RecognitionResult"},
236+
Result: 1,
237+
Label: "Porn",
238+
SubLabel: "SexBehavior",
239+
Score: 90,
240+
PornInfo: &RecognitionInfo{
241+
Code: 0,
242+
Msg: "OK",
243+
HitFlag: 1,
244+
Label: "xxx",
245+
SubLabel: "SexBehavior",
246+
Score: 100,
247+
},
248+
}
249+
250+
res, _, err := client.CI.ImageAuditing(context.Background(), name, opt)
251+
if err != nil {
252+
t.Fatalf("CI.ImageAuditing error: %v", err)
253+
}
254+
if !reflect.DeepEqual(res, want) {
255+
t.Errorf("CI.ImageAuditing failed, return:%+v, want:%+v", res, want)
256+
}
257+
}
201258
func TestCIService_PutVideoAuditingJob(t *testing.T) {
202259
setup()
203260
defer teardown()
@@ -291,6 +348,88 @@ func TestCIService_GetAudioAuditingJob(t *testing.T) {
291348
}
292349
}
293350

351+
func TestCIService_PutTextAuditingJob(t *testing.T) {
352+
setup()
353+
defer teardown()
354+
wantBody := `<Request><Input><Object>a.txt</Object></Input><Conf><DetectType>Porn,Terrorism,Politics,Ads,Illegal,Abuse</DetectType><Callback>http://callback.com/</Callback><BizType>b81d45f94b91a683255e9a95******</BizType></Conf></Request>`
355+
356+
mux.HandleFunc("/text/auditing", func(writer http.ResponseWriter, r *http.Request) {
357+
testMethod(t, r, http.MethodPost)
358+
testHeader(t, r, "Content-Type", "application/xml")
359+
testBody(t, r, wantBody)
360+
})
361+
362+
opt := &PutTextAuditingJobOptions{
363+
InputObject: "a.txt",
364+
Conf: &TextAuditingJobConf{
365+
DetectType: "Porn,Terrorism,Politics,Ads,Illegal,Abuse",
366+
Callback: "http://callback.com/",
367+
BizType: "b81d45f94b91a683255e9a95******",
368+
},
369+
}
370+
371+
_, _, err := client.CI.PutTextAuditingJob(context.Background(), opt)
372+
if err != nil {
373+
t.Fatalf("CI.PutTextAuditingJob returned error: %v", err)
374+
}
375+
}
376+
377+
func TestCIService_GetTextAuditingJob(t *testing.T) {
378+
setup()
379+
defer teardown()
380+
jobID := "vab1ca9fc8a3ed11ea834c525400863904"
381+
382+
mux.HandleFunc("/text/auditing"+"/"+jobID, func(w http.ResponseWriter, r *http.Request) {
383+
testMethod(t, r, http.MethodGet)
384+
})
385+
386+
_, _, err := client.CI.GetTextAuditingJob(context.Background(), jobID)
387+
if err != nil {
388+
t.Fatalf("CI.GetTextAuditingJob returned error: %v", err)
389+
}
390+
}
391+
392+
func TestCIService_PutDocumentAuditingJob(t *testing.T) {
393+
setup()
394+
defer teardown()
395+
wantBody := `<Request><Input><Url>http://www.example.com/doctest.doc</Url><Type>doc</Type></Input><Conf><DetectType>Porn,Terrorism,Politics,Ads</DetectType><Callback>http://www.example.com/</Callback><BizType>b81d45f94b91a683255e9a9506f4****</BizType></Conf></Request>`
396+
397+
mux.HandleFunc("/document/auditing", func(writer http.ResponseWriter, r *http.Request) {
398+
testMethod(t, r, http.MethodPost)
399+
testHeader(t, r, "Content-Type", "application/xml")
400+
testBody(t, r, wantBody)
401+
})
402+
403+
opt := &PutDocumentAuditingJobOptions{
404+
InputUrl: "http://www.example.com/doctest.doc",
405+
InputType: "doc",
406+
Conf: &DocumentAuditingJobConf{
407+
DetectType: "Porn,Terrorism,Politics,Ads",
408+
Callback: "http://www.example.com/",
409+
BizType: "b81d45f94b91a683255e9a9506f4****",
410+
},
411+
}
412+
413+
_, _, err := client.CI.PutDocumentAuditingJob(context.Background(), opt)
414+
if err != nil {
415+
t.Fatalf("CI.PutDocumentAuditingJob returned error: %v", err)
416+
}
417+
}
418+
419+
func TestCIService_GetDocumentAuditingJob(t *testing.T) {
420+
setup()
421+
defer teardown()
422+
jobID := "vab1ca9fc8a3ed11ea834c525400863904"
423+
424+
mux.HandleFunc("/document/auditing"+"/"+jobID, func(w http.ResponseWriter, r *http.Request) {
425+
testMethod(t, r, http.MethodGet)
426+
})
427+
428+
_, _, err := client.CI.GetDocumentAuditingJob(context.Background(), jobID)
429+
if err != nil {
430+
t.Fatalf("CI.GetDocumentAuditingJob returned error: %v", err)
431+
}
432+
}
294433
func TestCIService_Put(t *testing.T) {
295434
setup()
296435
defer teardown()

example/object/list_uploads.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,8 @@ func main() {
8282
uploadPart(c, name, uploadID, blockSize, i)
8383
}
8484
opt := &cos.ObjectListUploadsOptions{
85-
Prefix: cos.EncodeURIComponent("test/test_list_parts"),
86-
MaxUploads: 100,
85+
Prefix: "test/test_list_parts",
86+
MaxUploads: 1,
8787
}
8888
v, _, err := c.Object.ListUploads(context.Background(), opt)
8989
if err != nil {

helper.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -316,15 +316,18 @@ func FormatRangeOptions(opt *RangeOptions) string {
316316
}
317317
return ""
318318
}
319-
320319
func GetRangeOptions(opt *ObjectGetOptions) (*RangeOptions, error) {
321320
if opt == nil || opt.Range == "" {
322321
return nil, nil
323322
}
323+
return GetRange(opt.Range)
324+
}
325+
326+
func GetRange(rangeStr string) (*RangeOptions, error) {
324327
// bytes=M-N
325-
slices := strings.Split(opt.Range, "=")
328+
slices := strings.Split(rangeStr, "=")
326329
if len(slices) != 2 || slices[0] != "bytes" {
327-
return nil, fmt.Errorf("Invalid Parameter Range: %v", opt.Range)
330+
return nil, fmt.Errorf("Invalid Parameter Range: %v", rangeStr)
328331
}
329332
// byte=M-N, X-Y
330333
fSlice := strings.Split(slices[1], ",")
@@ -334,21 +337,21 @@ func GetRangeOptions(opt *ObjectGetOptions) (*RangeOptions, error) {
334337
var ropt RangeOptions
335338
sted := strings.Split(rstr, "-")
336339
if len(sted) != 2 {
337-
return nil, fmt.Errorf("Invalid Parameter Range: %v", opt.Range)
340+
return nil, fmt.Errorf("Invalid Parameter Range: %v", rangeStr)
338341
}
339342
// M
340343
if len(sted[0]) > 0 {
341344
ropt.Start, err = strconv.ParseInt(sted[0], 10, 64)
342345
if err != nil {
343-
return nil, fmt.Errorf("Invalid Parameter Range: %v,err: %v", opt.Range, err)
346+
return nil, fmt.Errorf("Invalid Parameter Range: %v,err: %v", rangeStr, err)
344347
}
345348
ropt.HasStart = true
346349
}
347350
// N
348351
if len(sted[1]) > 0 {
349352
ropt.End, err = strconv.ParseInt(sted[1], 10, 64)
350353
if err != nil || ropt.End == 0 {
351-
return nil, fmt.Errorf("Invalid Parameter Range: %v,err: %v", opt.Range, err)
354+
return nil, fmt.Errorf("Invalid Parameter Range: %v,err: %v", rangeStr, err)
352355
}
353356
ropt.HasEnd = true
354357
}

0 commit comments

Comments
 (0)