package server import ( "cfTest/cloudflareApi/zone" "reflect" "context" "github.com/samber/lo" "log" "testing" ) //func TestCreateRuleSet(t *testing.T) { // type args struct { // zoneID string // rulesetPhase string // ruleset cloudflare.Ruleset // } // // var ( // True = true // CONST604800 = uint(604800) // ) // // tests := []struct { // name string // args args // want interface{} // wantErr bool // }{ // { // name: "Test1", // args: args{ // zoneID: "e29551894461bf16e43a113e5ab09ed3", // rulesetPhase: "http_request_cache_settings", // ruleset: cloudflare.Ruleset{ // Rules: []cloudflare.RulesetRule{ // { // Expression: "(starts_with(http.request.uri.path, \"/\"))", // Enabled: true, // Description: "test1", // Action: "set_cache_settings", // ActionParameters: &cloudflare.RulesetRuleActionParameters{ // Cache: &True, // CacheKey: &cloudflare.RulesetRuleActionParametersCacheKey{ // CustomKey: &cloudflare.RulesetRuleActionParametersCustomKey{ // Query: &cloudflare.RulesetRuleActionParametersCustomKeyQuery{ // Exclude: &cloudflare.RulesetRuleActionParametersCustomKeyList{ // List: []string{ // "abc", // }, // All: false, // }, // }, // }, // }, // EdgeTTL: &cloudflare.RulesetRuleActionParametersEdgeTTL{ // Mode: "override_origin", // Default: &CONST604800, // }, // }, // }, // }, // }, // }, // want: nil, // wantErr: false, // }, // } // for _, tt := range tests { // t.Run(tt.name, func(t *testing.T) { // got, err := UpdateRuleSet("", "", tt.args.zoneID, tt.args.rulesetPhase, tt.args.ruleset) // if (err != nil) != tt.wantErr { // t.Errorf("UpdateRuleSet() error = %v, wantErr %v", err, tt.wantErr) // return // } // if !reflect.DeepEqual(got, tt.want) { // t.Errorf("UpdateRuleSet() got = %v, want %v", got, tt.want) // } // }) // } //} //func newUint32(a uint32) *uint32 { // return &a //} //func newStr(a string) *string { // return &a //} var ( apiKey = "2cc87f6213a4c40e5a90c8dced3c9c577eda5" apiEmail = "ll@centcloud.net" ) //func TestUpdatePageRule(t *testing.T) { // // type args struct { // apiKey string // apiEmail string // zoneID string // rule *zone.Rule // } // tests := []struct { // name string // args args // want *cloudflare.PageRule // wantErr bool // }{ // { // name: "测试1", // args: args{ // apiKey: apiKey, // apiEmail: apiEmail, // zoneID: "e29551894461bf16e43a113e5ab09ed3", // rule: &zone.Rule{ // Expression: "www.1011test5.com.cdn.sensordb.site/*\n", // Ttl: newUint32(18000), // QueryArgsType: newStr("ExcludeAll"), // QueryArgs: []string{"qwe12", "78u9hi"}, // OriginHost: newStr("www.baidu.com"), // OriginPath: nil, // OriginProtocol: newUint32(80), // RequestHeader: nil, // ResponseHeader: nil, // }, // }, // want: nil, // wantErr: false, // }, // // TODO: Add test cases. // } // for _, tt := range tests { // t.Run(tt.name, func(t *testing.T) { // got, err := UpdatePageRule(tt.args.apiKey, tt.args.apiEmail, tt.args.zoneID, tt.args.rule) // if (err != nil) != tt.wantErr { // t.Errorf("UpdatePageRule() error = %v, wantErr %v", err, tt.wantErr) // return // } // print("%v", got) // //if !reflect.DeepEqual(got, tt.want) { // // t.Errorf("UpdatePageRule() got = %v, want %v", got, tt.want) // //} // }) // } //} func TestZoneServer_GetRuleSet(t *testing.T) { type fields struct { UnimplementedZoneCloudflareServer zone.UnimplementedZoneCloudflareServer } type args struct { ctx context.Context requestCloudflare *zone.GetRuleSetRequestCloudflare } tests := []struct { name string fields fields args args want *zone.RuleList wantErr bool }{ { name: "测试1", fields: fields{ UnimplementedZoneCloudflareServer: zone.UnimplementedZoneCloudflareServer{}, }, args: args{ ctx: context.Background(), requestCloudflare: &zone.GetRuleSetRequestCloudflare{ ApiKey: apiKey, ApiEmail: apiEmail, ZoneId: "e29551894461bf16e43a113e5ab09ed3", Phase: zone.Phase_http_request_transform, }, }, want: nil, wantErr: false, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { s := &ZoneServer{ UnimplementedZoneCloudflareServer: tt.fields.UnimplementedZoneCloudflareServer, } got, err := s.GetRuleSet(tt.args.ctx, tt.args.requestCloudflare) if (err != nil) != tt.wantErr { t.Errorf("GetRuleSet() error = %v, wantErr %v", err, tt.wantErr) return } log.Print(got) //if !reflect.DeepEqual(got, tt.want) { // t.Errorf("GetRuleSet() got = %v, want %v", got, tt.want) //} }) } } func TestZoneServer_UpdateRuleSet(t *testing.T) { type fields struct { UnimplementedZoneCloudflareServer zone.UnimplementedZoneCloudflareServer } type args struct { ctx context.Context requestCloudflare *zone.UpdateRuleSetRequestCloudflare } tests := []struct { name string fields fields args args want *zone.RuleList wantErr bool }{ { name: "测试1", fields: fields{ UnimplementedZoneCloudflareServer: zone.UnimplementedZoneCloudflareServer{}, }, args: args{ ctx: nil, requestCloudflare: &zone.UpdateRuleSetRequestCloudflare{ ApiKey: apiKey, ApiEmail: apiEmail, ZoneId: "e29551894461bf16e43a113e5ab09ed3", Phase: zone.Phase_http_request_transform, Rules: []*zone.Rule{ { Expression: "(http.request.uri.path contains \"www.1011test5.com.cdn.sensordb.site/\")", OriginPath: lo.ToPtr[string]("www.baidu.com"), RequestHeader: nil, ResponseHeader: nil, }, }, }, }, want: nil, wantErr: false, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { s := &ZoneServer{ UnimplementedZoneCloudflareServer: tt.fields.UnimplementedZoneCloudflareServer, } got, err := s.UpdateRuleSet(tt.args.ctx, tt.args.requestCloudflare) if (err != nil) != tt.wantErr { t.Errorf("UpdateRuleSet() error = %v, wantErr %v", err, tt.wantErr) return } log.Print(got) //if !reflect.DeepEqual(got, tt.want) { // t.Errorf("UpdateRuleSet() got = %v, want %v", got, tt.want) //} }) } } func TestZoneServer_GetArgoSetting(t *testing.T) { type fields struct { UnimplementedZoneCloudflareServer zone.UnimplementedZoneCloudflareServer } type args struct { ctx context.Context requestCloudflare *zone.IdRequest } tests := []struct { name string fields fields args args want *zone.ArgoSetting wantErr bool }{ { name: "test1", fields: fields{ UnimplementedZoneCloudflareServer: zone.UnimplementedZoneCloudflareServer{}, }, args: args{ ctx: context.Background(), requestCloudflare: &zone.IdRequest{ ApiKey: apiKey, ApiEmail: apiEmail, ZoneId: "f7eefa29114db32ab14397bdd5048ec8", }, }, want: &zone.ArgoSetting{Enabled: true}, wantErr: false, }, { name: "test1", fields: fields{ UnimplementedZoneCloudflareServer: zone.UnimplementedZoneCloudflareServer{}, }, args: args{ ctx: context.Background(), requestCloudflare: &zone.IdRequest{ ApiKey: apiKey, ApiEmail: apiEmail, ZoneId: "f7eefa29114db32ab14397bdd5048ec8", }, }, want: &zone.ArgoSetting{Enabled: false}, wantErr: false, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { s := &ZoneServer{ UnimplementedZoneCloudflareServer: tt.fields.UnimplementedZoneCloudflareServer, } got, err := s.GetArgoSetting(tt.args.ctx, tt.args.requestCloudflare) if (err != nil) != tt.wantErr { t.Errorf("GetArgoSetting() error = %v, wantErr %v", err, tt.wantErr) return } if !reflect.DeepEqual(got, tt.want) { t.Errorf("GetArgoSetting() got = %v, want %v", got, tt.want) } }) } } func TestZoneServer_UpdateArgoSetting(t *testing.T) { type fields struct { UnimplementedZoneCloudflareServer zone.UnimplementedZoneCloudflareServer } type args struct { ctx context.Context requestCloudflare *zone.UpdateArgoRequestCloudflare } tests := []struct { name string fields fields args args want *zone.ArgoSetting wantErr bool }{ { name: "test1", fields: fields{ UnimplementedZoneCloudflareServer: zone.UnimplementedZoneCloudflareServer{}, }, args: args{ ctx: context.Background(), requestCloudflare: &zone.UpdateArgoRequestCloudflare{ ApiKey: apiKey, ApiEmail: apiEmail, ZoneId: "f7eefa29114db32ab14397bdd5048ec8", ArgoSetting: &zone.ArgoSetting{Enabled: false}, }, }, want: &zone.ArgoSetting{Enabled: false}, wantErr: false, }, { name: "test1", fields: fields{ UnimplementedZoneCloudflareServer: zone.UnimplementedZoneCloudflareServer{}, }, args: args{ ctx: context.Background(), requestCloudflare: &zone.UpdateArgoRequestCloudflare{ ApiKey: apiKey, ApiEmail: apiEmail, ZoneId: "f7eefa29114db32ab14397bdd5048ec8", ArgoSetting: &zone.ArgoSetting{Enabled: true}, }, }, want: &zone.ArgoSetting{Enabled: true}, wantErr: false, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { s := &ZoneServer{ UnimplementedZoneCloudflareServer: tt.fields.UnimplementedZoneCloudflareServer, } got, err := s.UpdateArgoSetting(tt.args.ctx, tt.args.requestCloudflare) if (err != nil) != tt.wantErr { t.Errorf("UpdateArgoSetting() error = %v, wantErr %v", err, tt.wantErr) return } if !reflect.DeepEqual(got, tt.want) { t.Errorf("UpdateArgoSetting() got = %v, want %v", got, tt.want) } }) } } func TestZoneServer_GetArgoSettingSubscription(t *testing.T) { type fields struct { UnimplementedZoneCloudflareServer zone.UnimplementedZoneCloudflareServer } type args struct { ctx context.Context request *zone.IdRequest } tests := []struct { name string fields fields args args want *zone.ArgoSetting wantErr bool }{ { name: "启用测试", fields: fields{ UnimplementedZoneCloudflareServer: zone.UnimplementedZoneCloudflareServer{}, }, args: args{ ctx: nil, request: &zone.IdRequest{ ApiKey: apiKey, ApiEmail: apiEmail, ZoneId: "f7eefa29114db32ab14397bdd5048ec8", }, }, want: &zone.ArgoSetting{Enabled: true}, wantErr: false, }, { name: "未启用测试", fields: fields{ UnimplementedZoneCloudflareServer: zone.UnimplementedZoneCloudflareServer{}, }, args: args{ ctx: nil, request: &zone.IdRequest{ ApiKey: apiKey, ApiEmail: apiEmail, ZoneId: "6d89e53e52a08fc51b99628ff546c7fb", }, }, want: &zone.ArgoSetting{Enabled: false}, wantErr: false, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { s := &ZoneServer{ UnimplementedZoneCloudflareServer: tt.fields.UnimplementedZoneCloudflareServer, } got, err := s.GetArgoSettingSubscription(tt.args.ctx, tt.args.request) if (err != nil) != tt.wantErr { t.Errorf("GetArgoSettingSubscription() error = %v, wantErr %v", err, tt.wantErr) return } if !reflect.DeepEqual(got, tt.want) { t.Errorf("GetArgoSettingSubscription() got = %v, want %v", got, tt.want) } }) } } func TestZoneServer_CreateArgoSettingSubscription(t *testing.T) { type fields struct { UnimplementedZoneCloudflareServer zone.UnimplementedZoneCloudflareServer } type args struct { ctx context.Context request *zone.IdRequest } tests := []struct { name string fields fields args args want *zone.ArgoSetting wantErr bool }{ { name: "测试", fields: fields{ UnimplementedZoneCloudflareServer: zone.UnimplementedZoneCloudflareServer{}, }, args: args{ ctx: nil, request: &zone.IdRequest{ ApiKey: apiKey, ApiEmail: apiEmail, ZoneId: "7a25de4f4ea643e99c66e84e7498ccc6", }, }, want: &zone.ArgoSetting{Enabled: true}, wantErr: false, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { s := &ZoneServer{ UnimplementedZoneCloudflareServer: tt.fields.UnimplementedZoneCloudflareServer, } got, err := s.CreateArgoSettingSubscription(tt.args.ctx, tt.args.request) if (err != nil) != tt.wantErr { t.Errorf("CreateArgoSettingSubscription() error = %v, wantErr %v", err, tt.wantErr) return } if !reflect.DeepEqual(got, tt.want) { t.Errorf("CreateArgoSettingSubscription() got = %v, want %v", got, tt.want) } }) } }