mirror of
https://github.com/GopeedLab/gopeed.git
synced 2026-11-05 16:15:43 +00:00
115 lines
2.0 KiB
Go
115 lines
2.0 KiB
Go
package fetcher
|
|
|
|
import (
|
|
"encoding/json"
|
|
"testing"
|
|
)
|
|
|
|
func TestStatsMarshalRawSnapshotWithoutRuntime(t *testing.T) {
|
|
stats := &Stats{
|
|
Snapshot: json.RawMessage(`{"value":1}`),
|
|
}
|
|
|
|
data, err := json.Marshal(stats)
|
|
if err != nil {
|
|
t.Fatalf("Marshal() error = %v", err)
|
|
}
|
|
if got, want := string(data), `{"snapshot":{"value":1},"runtime":null}`; got != want {
|
|
t.Fatalf("Marshal() = %s, want %s", got, want)
|
|
}
|
|
}
|
|
|
|
func TestSchemeFilter_Match(t *testing.T) {
|
|
type fields struct {
|
|
Type FilterType
|
|
Pattern string
|
|
}
|
|
type args struct {
|
|
uri string
|
|
}
|
|
tests := []struct {
|
|
name string
|
|
fields fields
|
|
args args
|
|
want bool
|
|
}{
|
|
{
|
|
name: "url match",
|
|
fields: fields{
|
|
Type: FilterTypeUrl,
|
|
Pattern: "https",
|
|
},
|
|
args: args{
|
|
uri: "https://github.com",
|
|
},
|
|
want: true,
|
|
},
|
|
{
|
|
name: "url not match",
|
|
fields: fields{
|
|
Type: FilterTypeUrl,
|
|
Pattern: "https",
|
|
},
|
|
args: args{
|
|
uri: "ftp://github.com",
|
|
},
|
|
want: false,
|
|
},
|
|
{
|
|
name: "file match",
|
|
fields: fields{
|
|
Type: FilterTypeFile,
|
|
Pattern: "torrent",
|
|
},
|
|
args: args{
|
|
uri: "d:/temp/test.torrent",
|
|
},
|
|
want: true,
|
|
},
|
|
{
|
|
name: "file not match",
|
|
fields: fields{
|
|
Type: FilterTypeFile,
|
|
Pattern: "torrent",
|
|
},
|
|
args: args{
|
|
uri: "d:/temp/test.txt",
|
|
},
|
|
want: false,
|
|
},
|
|
{
|
|
name: "base64 match",
|
|
fields: fields{
|
|
Type: FilterTypeBase64,
|
|
Pattern: "application/x-bittorrent",
|
|
},
|
|
args: args{
|
|
uri: "data:application/x-bittorrent;base64,xxx",
|
|
},
|
|
want: true,
|
|
},
|
|
{
|
|
name: "base64 not match",
|
|
fields: fields{
|
|
Type: FilterTypeBase64,
|
|
Pattern: "application/x-bittorrent",
|
|
},
|
|
args: args{
|
|
uri: "data:application/javascript;base64,xxx",
|
|
},
|
|
want: false,
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
s := &SchemeFilter{
|
|
Type: tt.fields.Type,
|
|
Pattern: tt.fields.Pattern,
|
|
}
|
|
if got := s.Match(tt.args.uri); got != tt.want {
|
|
t.Errorf("Match() = %v, want %v", got, tt.want)
|
|
}
|
|
})
|
|
}
|
|
}
|