Skip to content

Commit

Permalink
Merge pull request #160 from agin719/cos-v4-dev
Browse files Browse the repository at this point in the history
add ut
  • Loading branch information
agin719 authored Oct 28, 2021
2 parents 48d7d86 + 5fe8d16 commit 94b0b70
Show file tree
Hide file tree
Showing 8 changed files with 775 additions and 25 deletions.
137 changes: 137 additions & 0 deletions ci_media_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
package cos

import (
"bytes"
"context"
"crypto/rand"
"encoding/xml"
"fmt"
"io/ioutil"
"net/http"
"reflect"
"testing"
)

Expand Down Expand Up @@ -168,3 +174,134 @@ func TestCIService_DescribeMediaProcessBuckets(t *testing.T) {
t.Fatalf("CI.DescribeMediaProcessBuckets returned error: %v", err)
}
}

func TestCIService_GetMediaInfo(t *testing.T) {
setup()
defer teardown()

res_xml := `<Response>
<MediaInfo>
<Format>
<Bitrate>1014.950000</Bitrate>
<Duration>10.125000</Duration>
<FormatLongName>QuickTime / MOV</FormatLongName>
<FormatName>mov,mp4,m4a,3gp,3g2,mj2</FormatName>
<NumProgram>0</NumProgram>
<NumStream>2</NumStream>
<Size>1284547</Size>
<StartTime>0.000000</StartTime>
</Format>
<Stream>
<Audio>
<Bitrate>70.451000</Bitrate>
<Channel>1</Channel>
<ChannelLayout>mono</ChannelLayout>
<CodecLongName>AAC (Advanced Audio Coding)</CodecLongName>
<CodecName>aac</CodecName>
<CodecTag>0x6134706d</CodecTag>
<CodecTagString>mp4a</CodecTagString>
<CodecTimeBase>1/44100</CodecTimeBase>
<Duration>0.440294</Duration>
<Index>1</Index>
<Language>und</Language>
<SampleFmt>fltp</SampleFmt>
<SampleRate>44100</SampleRate>
<StartTime>0.000000</StartTime>
<Timebase>1/44100</Timebase>
</Audio>
<Subtitle/>
<Video>
<AvgFps>24/1</AvgFps>
<Bitrate>938.164000</Bitrate>
<CodecLongName>H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10</CodecLongName>
<CodecName>h264</CodecName>
<CodecTag>0x31637661</CodecTag>
<CodecTagString>avc1</CodecTagString>
<CodecTimeBase>1/12288</CodecTimeBase>
<Dar>40:53</Dar>
<Duration>0.124416</Duration>
<Fps>24.500000</Fps>
<HasBFrame>2</HasBFrame>
<Height>1280</Height>
<Index>0</Index>
<Language>und</Language>
<Level>32</Level>
<NumFrames>243</NumFrames>
<PixFormat>yuv420p</PixFormat>
<Profile>High</Profile>
<RefFrames>1</RefFrames>
<Sar>25600:25599</Sar>
<StartTime>0.000000</StartTime>
<Timebase>1/12288</Timebase>
<Width>966</Width>
</Video>
</Stream>
</MediaInfo>
</Response>`

mux.HandleFunc("/test", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
v := values{
"ci-process": "videoinfo",
}
testFormValues(t, r, v)
fmt.Fprint(w, res_xml)
})

res, _, err := client.CI.GetMediaInfo(context.Background(), "test", nil)
if err != nil {
t.Fatalf("CI.GetMediaInfo returned error: %v", err)
}
want := &GetMediaInfoResult{}
err = xml.Unmarshal([]byte(res_xml), want)
if err != nil {
t.Errorf("Bucket.GetMediaInfo Unmarshal returned error %+v", err)
}
if !reflect.DeepEqual(res, want) {
t.Errorf("Bucket.GetMediaInfo returned %+v, want %+v", res, want)
}
}

func TestCIService_GetSnapshot(t *testing.T) {
setup()
defer teardown()

opt := &GetSnapshotOptions{
Time: 1,
Height: 100,
Width: 100,
Format: "jpg",
Rotate: "auto",
Mode: "exactframe",
}
data := make([]byte, 1234*2)
rand.Read(data)

mux.HandleFunc("/test", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
v := values{
"ci-process": "snapshot",
"time": "1",
"format": "jpg",
"width": "100",
"height": "100",
"rotate": "auto",
"mode": "exactframe",
}
testFormValues(t, r, v)
w.Write(data)
})

resp, err := client.CI.GetSnapshot(context.Background(), "test", opt)
if err != nil {
t.Fatalf("CI.GetSnapshot returned error: %v", err)
}
defer resp.Body.Close()
bs, err := ioutil.ReadAll(resp.Body)
if err != nil {
t.Fatalf("CI.GetSnapshot ReadAll returned error: %v", err)
}
if bytes.Compare(bs, data) != 0 {
t.Errorf("Bucket.GetSnapshot Compare failed")
}
}
139 changes: 139 additions & 0 deletions ci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,63 @@ func TestCIService_ImageRecognition(t *testing.T) {
}
}

func TestCIService_ImageAuditing(t *testing.T) {
setup()
defer teardown()
name := "test.jpg"
opt := &ImageRecognitionOptions{
CIProcess: "sensitive-content-recognition",
DetectType: "porn",
MaxFrames: 1,
}
mux.HandleFunc("/test.jpg", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
vs := values{
"ci-process": "sensitive-content-recognition",
"detect-type": "porn",
"max-frames": "1",
}
testFormValues(t, r, vs)
fmt.Fprint(w, `<RecognitionResult>
<Result>1</Result>
<Label>Porn</Label>
<SubLabel>SexBehavior</SubLabel>
<Score>90</Score>
<PornInfo>
<Code>0</Code>
<Msg>OK</Msg>
<HitFlag>1</HitFlag>
<Label>xxx</Label>
<SubLabel>SexBehavior</SubLabel>
<Score>100</Score>
</PornInfo>
</RecognitionResult>`)
})

want := &ImageRecognitionResult{
XMLName: xml.Name{Local: "RecognitionResult"},
Result: 1,
Label: "Porn",
SubLabel: "SexBehavior",
Score: 90,
PornInfo: &RecognitionInfo{
Code: 0,
Msg: "OK",
HitFlag: 1,
Label: "xxx",
SubLabel: "SexBehavior",
Score: 100,
},
}

res, _, err := client.CI.ImageAuditing(context.Background(), name, opt)
if err != nil {
t.Fatalf("CI.ImageAuditing error: %v", err)
}
if !reflect.DeepEqual(res, want) {
t.Errorf("CI.ImageAuditing failed, return:%+v, want:%+v", res, want)
}
}
func TestCIService_PutVideoAuditingJob(t *testing.T) {
setup()
defer teardown()
Expand Down Expand Up @@ -291,6 +348,88 @@ func TestCIService_GetAudioAuditingJob(t *testing.T) {
}
}

func TestCIService_PutTextAuditingJob(t *testing.T) {
setup()
defer teardown()
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>`

mux.HandleFunc("/text/auditing", func(writer http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodPost)
testHeader(t, r, "Content-Type", "application/xml")
testBody(t, r, wantBody)
})

opt := &PutTextAuditingJobOptions{
InputObject: "a.txt",
Conf: &TextAuditingJobConf{
DetectType: "Porn,Terrorism,Politics,Ads,Illegal,Abuse",
Callback: "http://callback.com/",
BizType: "b81d45f94b91a683255e9a95******",
},
}

_, _, err := client.CI.PutTextAuditingJob(context.Background(), opt)
if err != nil {
t.Fatalf("CI.PutTextAuditingJob returned error: %v", err)
}
}

func TestCIService_GetTextAuditingJob(t *testing.T) {
setup()
defer teardown()
jobID := "vab1ca9fc8a3ed11ea834c525400863904"

mux.HandleFunc("/text/auditing"+"/"+jobID, func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
})

_, _, err := client.CI.GetTextAuditingJob(context.Background(), jobID)
if err != nil {
t.Fatalf("CI.GetTextAuditingJob returned error: %v", err)
}
}

func TestCIService_PutDocumentAuditingJob(t *testing.T) {
setup()
defer teardown()
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>`

mux.HandleFunc("/document/auditing", func(writer http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodPost)
testHeader(t, r, "Content-Type", "application/xml")
testBody(t, r, wantBody)
})

opt := &PutDocumentAuditingJobOptions{
InputUrl: "http://www.example.com/doctest.doc",
InputType: "doc",
Conf: &DocumentAuditingJobConf{
DetectType: "Porn,Terrorism,Politics,Ads",
Callback: "http://www.example.com/",
BizType: "b81d45f94b91a683255e9a9506f4****",
},
}

_, _, err := client.CI.PutDocumentAuditingJob(context.Background(), opt)
if err != nil {
t.Fatalf("CI.PutDocumentAuditingJob returned error: %v", err)
}
}

func TestCIService_GetDocumentAuditingJob(t *testing.T) {
setup()
defer teardown()
jobID := "vab1ca9fc8a3ed11ea834c525400863904"

mux.HandleFunc("/document/auditing"+"/"+jobID, func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
})

_, _, err := client.CI.GetDocumentAuditingJob(context.Background(), jobID)
if err != nil {
t.Fatalf("CI.GetDocumentAuditingJob returned error: %v", err)
}
}
func TestCIService_Put(t *testing.T) {
setup()
defer teardown()
Expand Down
4 changes: 2 additions & 2 deletions example/object/list_uploads.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ func main() {
uploadPart(c, name, uploadID, blockSize, i)
}
opt := &cos.ObjectListUploadsOptions{
Prefix: cos.EncodeURIComponent("test/test_list_parts"),
MaxUploads: 100,
Prefix: "test/test_list_parts",
MaxUploads: 1,
}
v, _, err := c.Object.ListUploads(context.Background(), opt)
if err != nil {
Expand Down
15 changes: 9 additions & 6 deletions helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -316,15 +316,18 @@ func FormatRangeOptions(opt *RangeOptions) string {
}
return ""
}

func GetRangeOptions(opt *ObjectGetOptions) (*RangeOptions, error) {
if opt == nil || opt.Range == "" {
return nil, nil
}
return GetRange(opt.Range)
}

func GetRange(rangeStr string) (*RangeOptions, error) {
// bytes=M-N
slices := strings.Split(opt.Range, "=")
slices := strings.Split(rangeStr, "=")
if len(slices) != 2 || slices[0] != "bytes" {
return nil, fmt.Errorf("Invalid Parameter Range: %v", opt.Range)
return nil, fmt.Errorf("Invalid Parameter Range: %v", rangeStr)
}
// byte=M-N, X-Y
fSlice := strings.Split(slices[1], ",")
Expand All @@ -334,21 +337,21 @@ func GetRangeOptions(opt *ObjectGetOptions) (*RangeOptions, error) {
var ropt RangeOptions
sted := strings.Split(rstr, "-")
if len(sted) != 2 {
return nil, fmt.Errorf("Invalid Parameter Range: %v", opt.Range)
return nil, fmt.Errorf("Invalid Parameter Range: %v", rangeStr)
}
// M
if len(sted[0]) > 0 {
ropt.Start, err = strconv.ParseInt(sted[0], 10, 64)
if err != nil {
return nil, fmt.Errorf("Invalid Parameter Range: %v,err: %v", opt.Range, err)
return nil, fmt.Errorf("Invalid Parameter Range: %v,err: %v", rangeStr, err)
}
ropt.HasStart = true
}
// N
if len(sted[1]) > 0 {
ropt.End, err = strconv.ParseInt(sted[1], 10, 64)
if err != nil || ropt.End == 0 {
return nil, fmt.Errorf("Invalid Parameter Range: %v,err: %v", opt.Range, err)
return nil, fmt.Errorf("Invalid Parameter Range: %v,err: %v", rangeStr, err)
}
ropt.HasEnd = true
}
Expand Down
Loading

0 comments on commit 94b0b70

Please sign in to comment.