ZoneCloudflareServer_test.go 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969
  1. package server
  2. import (
  3. "cfTest/cloudflareApi/zone"
  4. "context"
  5. "fmt"
  6. "github.com/cloudflare/cloudflare-go"
  7. "github.com/samber/lo"
  8. "log"
  9. "reflect"
  10. "testing"
  11. "time"
  12. )
  13. // func TestCreateRuleSet(t *testing.T) {
  14. // type args struct {
  15. // zoneID string
  16. // rulesetPhase string
  17. // ruleset cloudflare.Ruleset
  18. // }
  19. //
  20. // var (
  21. // True = true
  22. // CONST604800 = uint(604800)
  23. // )
  24. //
  25. // tests := []struct {
  26. // name string
  27. // args args
  28. // want interface{}
  29. // wantErr bool
  30. // }{
  31. // {
  32. // name: "Test1",
  33. // args: args{
  34. // zoneID: "e29551894461bf16e43a113e5ab09ed3",
  35. // rulesetPhase: "http_request_cache_settings",
  36. // ruleset: cloudflare.Ruleset{
  37. // Rules: []cloudflare.RulesetRule{
  38. // {
  39. // Expression: "(starts_with(http.request.uri.path, \"/\"))",
  40. // Enabled: true,
  41. // Description: "test1",
  42. // Action: "set_cache_settings",
  43. // ActionParameters: &cloudflare.RulesetRuleActionParameters{
  44. // Cache: &True,
  45. // CacheKey: &cloudflare.RulesetRuleActionParametersCacheKey{
  46. // CustomKey: &cloudflare.RulesetRuleActionParametersCustomKey{
  47. // Query: &cloudflare.RulesetRuleActionParametersCustomKeyQuery{
  48. // Exclude: &cloudflare.RulesetRuleActionParametersCustomKeyList{
  49. // List: []string{
  50. // "abc",
  51. // },
  52. // All: false,
  53. // },
  54. // },
  55. // },
  56. // },
  57. // EdgeTTL: &cloudflare.RulesetRuleActionParametersEdgeTTL{
  58. // Mode: "override_origin",
  59. // Default: &CONST604800,
  60. // },
  61. // },
  62. // },
  63. // },
  64. // },
  65. // },
  66. // want: nil,
  67. // wantErr: false,
  68. // },
  69. // }
  70. // for _, tt := range tests {
  71. // t.Run(tt.name, func(t *testing.T) {
  72. // got, err := UpdateRuleSet("", "", tt.args.zoneID, tt.args.rulesetPhase, tt.args.ruleset)
  73. // if (err != nil) != tt.wantErr {
  74. // t.Errorf("UpdateRuleSet() error = %v, wantErr %v", err, tt.wantErr)
  75. // return
  76. // }
  77. // if !reflect.DeepEqual(got, tt.want) {
  78. // t.Errorf("UpdateRuleSet() got = %v, want %v", got, tt.want)
  79. // }
  80. // })
  81. // }
  82. // }
  83. //
  84. // func newUint32(a uint32) *uint32 {
  85. // return &a
  86. // }
  87. //
  88. // func newStr(a string) *string {
  89. // return &a
  90. // }
  91. var (
  92. apiKey = "2cc87f6213a4c40e5a90c8dced3c9c577eda5"
  93. apiEmail = "ll@centcloud.net"
  94. )
  95. //func TestUpdatePageRule(t *testing.T) {
  96. //
  97. // type args struct {
  98. // apiKey string
  99. // apiEmail string
  100. // zoneID string
  101. // rule *zone.Rule
  102. // }
  103. // tests := []struct {
  104. // name string
  105. // args args
  106. // want *cloudflare.PageRule
  107. // wantErr bool
  108. // }{
  109. // {
  110. // name: "测试1",
  111. // args: args{
  112. // apiKey: apiKey,
  113. // apiEmail: apiEmail,
  114. // zoneID: "e29551894461bf16e43a113e5ab09ed3",
  115. // rule: &zone.Rule{
  116. // Expression: "www.1011test5.com.cdn.sensordb.site/*\n",
  117. // Ttl: newUint32(18000),
  118. // QueryArgsType: newStr("ExcludeAll"),
  119. // QueryArgs: []string{"qwe12", "78u9hi"},
  120. // OriginHost: newStr("www.baidu.com"),
  121. // OriginPath: nil,
  122. // OriginProtocol: newUint32(80),
  123. // RequestHeader: nil,
  124. // ResponseHeader: nil,
  125. // },
  126. // },
  127. // want: nil,
  128. // wantErr: false,
  129. // },
  130. // // TODO: Add test cases.
  131. // }
  132. // for _, tt := range tests {
  133. // t.Run(tt.name, func(t *testing.T) {
  134. // got, err := UpdatePageRule(tt.args.apiKey, tt.args.apiEmail, tt.args.zoneID, tt.args.rule)
  135. // if (err != nil) != tt.wantErr {
  136. // t.Errorf("UpdatePageRule() error = %v, wantErr %v", err, tt.wantErr)
  137. // return
  138. // }
  139. // print("%v", got)
  140. // //if !reflect.DeepEqual(got, tt.want) {
  141. // // t.Errorf("UpdatePageRule() got = %v, want %v", got, tt.want)
  142. // //}
  143. // })
  144. // }
  145. //}
  146. func TestZoneServer_GetRuleSet(t *testing.T) {
  147. type fields struct {
  148. UnimplementedZoneCloudflareServer zone.UnimplementedZoneCloudflareServer
  149. }
  150. type args struct {
  151. ctx context.Context
  152. requestCloudflare *zone.GetRuleSetRequestCloudflare
  153. }
  154. tests := []struct {
  155. name string
  156. fields fields
  157. args args
  158. want *zone.RuleList
  159. wantErr bool
  160. }{
  161. {
  162. name: "测试1",
  163. fields: fields{
  164. UnimplementedZoneCloudflareServer: zone.UnimplementedZoneCloudflareServer{},
  165. },
  166. args: args{
  167. ctx: context.Background(),
  168. requestCloudflare: &zone.GetRuleSetRequestCloudflare{
  169. ApiKey: "d2589c4f617b0493357ae187a7b4518171d07",
  170. ApiEmail: "shiqi@centcloud.net",
  171. ZoneId: "5801a8cbe2fdfd6c53413d0eef164d23",
  172. Phase: zone.Phase_http_request_cache_settings,
  173. },
  174. },
  175. wantErr: false,
  176. },
  177. }
  178. for _, tt := range tests {
  179. t.Run(tt.name, func(t *testing.T) {
  180. s := &ZoneServer{
  181. UnimplementedZoneCloudflareServer: tt.fields.UnimplementedZoneCloudflareServer,
  182. }
  183. got, err := s.GetRuleSet(tt.args.ctx, tt.args.requestCloudflare)
  184. fmt.Println(got)
  185. if (err != nil) != tt.wantErr {
  186. t.Errorf("GetRuleSet() error = %v, wantErr %v", err, tt.wantErr)
  187. return
  188. }
  189. log.Print(got)
  190. //if !reflect.DeepEqual(got, tt.want) {
  191. // t.Errorf("GetRuleSet() got = %v, want %v", got, tt.want)
  192. //}
  193. })
  194. }
  195. }
  196. func TestZoneServer_UpdateRuleSet(t *testing.T) {
  197. type fields struct {
  198. UnimplementedZoneCloudflareServer zone.UnimplementedZoneCloudflareServer
  199. }
  200. type args struct {
  201. ctx context.Context
  202. requestCloudflare *zone.UpdateRuleSetRequestCloudflare
  203. }
  204. tests := []struct {
  205. name string
  206. fields fields
  207. args args
  208. want *zone.RuleList
  209. wantErr bool
  210. }{
  211. {
  212. name: "测试1",
  213. fields: fields{
  214. UnimplementedZoneCloudflareServer: zone.UnimplementedZoneCloudflareServer{},
  215. },
  216. args: args{
  217. ctx: nil,
  218. requestCloudflare: &zone.UpdateRuleSetRequestCloudflare{
  219. ApiKey: apiKey,
  220. ApiEmail: apiEmail,
  221. ZoneId: "e29551894461bf16e43a113e5ab09ed3",
  222. Phase: zone.Phase_http_request_transform,
  223. Rules: []*zone.Rule{
  224. {
  225. Expression: "(http.request.uri.path contains \"www.1011test5.com.cdn.sensordb.site/\")",
  226. OriginPath: lo.ToPtr[string]("www.baidu.com"),
  227. RequestHeader: nil,
  228. ResponseHeader: nil,
  229. },
  230. },
  231. },
  232. },
  233. want: nil,
  234. wantErr: false,
  235. },
  236. }
  237. for _, tt := range tests {
  238. t.Run(tt.name, func(t *testing.T) {
  239. s := &ZoneServer{
  240. UnimplementedZoneCloudflareServer: tt.fields.UnimplementedZoneCloudflareServer,
  241. }
  242. got, err := s.UpdateRuleSet(tt.args.ctx, tt.args.requestCloudflare)
  243. if (err != nil) != tt.wantErr {
  244. t.Errorf("UpdateRuleSet() error = %v, wantErr %v", err, tt.wantErr)
  245. return
  246. }
  247. log.Print(got)
  248. //if !reflect.DeepEqual(got, tt.want) {
  249. // t.Errorf("UpdateRuleSet() got = %v, want %v", got, tt.want)
  250. //}
  251. })
  252. }
  253. }
  254. func TestZoneServer_GetArgoSetting(t *testing.T) {
  255. type fields struct {
  256. UnimplementedZoneCloudflareServer zone.UnimplementedZoneCloudflareServer
  257. }
  258. type args struct {
  259. ctx context.Context
  260. requestCloudflare *zone.IdRequest
  261. }
  262. tests := []struct {
  263. name string
  264. fields fields
  265. args args
  266. want *zone.ArgoSetting
  267. wantErr bool
  268. }{
  269. {
  270. name: "test1",
  271. fields: fields{
  272. UnimplementedZoneCloudflareServer: zone.UnimplementedZoneCloudflareServer{},
  273. },
  274. args: args{
  275. ctx: context.Background(),
  276. requestCloudflare: &zone.IdRequest{
  277. ApiKey: apiKey,
  278. ApiEmail: apiEmail,
  279. ZoneId: "f7eefa29114db32ab14397bdd5048ec8",
  280. },
  281. },
  282. want: &zone.ArgoSetting{Enabled: true},
  283. wantErr: false,
  284. },
  285. {
  286. name: "test1",
  287. fields: fields{
  288. UnimplementedZoneCloudflareServer: zone.UnimplementedZoneCloudflareServer{},
  289. },
  290. args: args{
  291. ctx: context.Background(),
  292. requestCloudflare: &zone.IdRequest{
  293. ApiKey: apiKey,
  294. ApiEmail: apiEmail,
  295. ZoneId: "f7eefa29114db32ab14397bdd5048ec8",
  296. },
  297. },
  298. want: &zone.ArgoSetting{Enabled: false},
  299. wantErr: false,
  300. },
  301. }
  302. for _, tt := range tests {
  303. t.Run(tt.name, func(t *testing.T) {
  304. s := &ZoneServer{
  305. UnimplementedZoneCloudflareServer: tt.fields.UnimplementedZoneCloudflareServer,
  306. }
  307. got, err := s.GetArgoSetting(tt.args.ctx, tt.args.requestCloudflare)
  308. if (err != nil) != tt.wantErr {
  309. t.Errorf("GetArgoSetting() error = %v, wantErr %v", err, tt.wantErr)
  310. return
  311. }
  312. if !reflect.DeepEqual(got, tt.want) {
  313. t.Errorf("GetArgoSetting() got = %v, want %v", got, tt.want)
  314. }
  315. })
  316. }
  317. }
  318. func TestZoneServer_UpdateArgoSetting(t *testing.T) {
  319. type fields struct {
  320. UnimplementedZoneCloudflareServer zone.UnimplementedZoneCloudflareServer
  321. }
  322. type args struct {
  323. ctx context.Context
  324. requestCloudflare *zone.UpdateArgoRequestCloudflare
  325. }
  326. tests := []struct {
  327. name string
  328. fields fields
  329. args args
  330. want *zone.ArgoSetting
  331. wantErr bool
  332. }{
  333. {
  334. name: "test1",
  335. fields: fields{
  336. UnimplementedZoneCloudflareServer: zone.UnimplementedZoneCloudflareServer{},
  337. },
  338. args: args{
  339. ctx: context.Background(),
  340. requestCloudflare: &zone.UpdateArgoRequestCloudflare{
  341. ApiKey: apiKey,
  342. ApiEmail: apiEmail,
  343. ZoneId: "f7eefa29114db32ab14397bdd5048ec8",
  344. ArgoSetting: &zone.ArgoSetting{Enabled: false},
  345. },
  346. },
  347. want: &zone.ArgoSetting{Enabled: false},
  348. wantErr: false,
  349. },
  350. {
  351. name: "test1",
  352. fields: fields{
  353. UnimplementedZoneCloudflareServer: zone.UnimplementedZoneCloudflareServer{},
  354. },
  355. args: args{
  356. ctx: context.Background(),
  357. requestCloudflare: &zone.UpdateArgoRequestCloudflare{
  358. ApiKey: apiKey,
  359. ApiEmail: apiEmail,
  360. ZoneId: "f7eefa29114db32ab14397bdd5048ec8",
  361. ArgoSetting: &zone.ArgoSetting{Enabled: true},
  362. },
  363. },
  364. want: &zone.ArgoSetting{Enabled: true},
  365. wantErr: false,
  366. },
  367. }
  368. for _, tt := range tests {
  369. t.Run(tt.name, func(t *testing.T) {
  370. s := &ZoneServer{
  371. UnimplementedZoneCloudflareServer: tt.fields.UnimplementedZoneCloudflareServer,
  372. }
  373. got, err := s.UpdateArgoSetting(tt.args.ctx, tt.args.requestCloudflare)
  374. if (err != nil) != tt.wantErr {
  375. t.Errorf("UpdateArgoSetting() error = %v, wantErr %v", err, tt.wantErr)
  376. return
  377. }
  378. if !reflect.DeepEqual(got, tt.want) {
  379. t.Errorf("UpdateArgoSetting() got = %v, want %v", got, tt.want)
  380. }
  381. })
  382. }
  383. }
  384. func TestZoneServer_GetArgoSettingSubscription(t *testing.T) {
  385. type fields struct {
  386. UnimplementedZoneCloudflareServer zone.UnimplementedZoneCloudflareServer
  387. }
  388. type args struct {
  389. ctx context.Context
  390. request *zone.IdRequest
  391. }
  392. tests := []struct {
  393. name string
  394. fields fields
  395. args args
  396. want *zone.ArgoSetting
  397. wantErr bool
  398. }{
  399. {
  400. name: "启用测试",
  401. fields: fields{
  402. UnimplementedZoneCloudflareServer: zone.UnimplementedZoneCloudflareServer{},
  403. },
  404. args: args{
  405. ctx: nil,
  406. request: &zone.IdRequest{
  407. ApiKey: apiKey,
  408. ApiEmail: apiEmail,
  409. ZoneId: "f7eefa29114db32ab14397bdd5048ec8",
  410. },
  411. },
  412. want: &zone.ArgoSetting{Enabled: true},
  413. wantErr: false,
  414. },
  415. {
  416. name: "未启用测试",
  417. fields: fields{
  418. UnimplementedZoneCloudflareServer: zone.UnimplementedZoneCloudflareServer{},
  419. },
  420. args: args{
  421. ctx: nil,
  422. request: &zone.IdRequest{
  423. ApiKey: apiKey,
  424. ApiEmail: apiEmail,
  425. ZoneId: "6d89e53e52a08fc51b99628ff546c7fb",
  426. },
  427. },
  428. want: &zone.ArgoSetting{Enabled: false},
  429. wantErr: false,
  430. },
  431. }
  432. for _, tt := range tests {
  433. t.Run(tt.name, func(t *testing.T) {
  434. s := &ZoneServer{
  435. UnimplementedZoneCloudflareServer: tt.fields.UnimplementedZoneCloudflareServer,
  436. }
  437. got, err := s.GetArgoSettingSubscription(tt.args.ctx, tt.args.request)
  438. if (err != nil) != tt.wantErr {
  439. t.Errorf("GetArgoSettingSubscription() error = %v, wantErr %v", err, tt.wantErr)
  440. return
  441. }
  442. if !reflect.DeepEqual(got, tt.want) {
  443. t.Errorf("GetArgoSettingSubscription() got = %v, want %v", got, tt.want)
  444. }
  445. })
  446. }
  447. }
  448. func TestZoneServer_CreateArgoSettingSubscription(t *testing.T) {
  449. type fields struct {
  450. UnimplementedZoneCloudflareServer zone.UnimplementedZoneCloudflareServer
  451. }
  452. type args struct {
  453. ctx context.Context
  454. request *zone.IdRequest
  455. }
  456. tests := []struct {
  457. name string
  458. fields fields
  459. args args
  460. want *zone.ArgoSetting
  461. wantErr bool
  462. }{
  463. {
  464. name: "测试",
  465. fields: fields{
  466. UnimplementedZoneCloudflareServer: zone.UnimplementedZoneCloudflareServer{},
  467. },
  468. args: args{
  469. ctx: nil,
  470. request: &zone.IdRequest{
  471. ApiKey: apiKey,
  472. ApiEmail: apiEmail,
  473. ZoneId: "7a25de4f4ea643e99c66e84e7498ccc6",
  474. },
  475. },
  476. want: &zone.ArgoSetting{Enabled: true},
  477. wantErr: false,
  478. },
  479. }
  480. for _, tt := range tests {
  481. t.Run(tt.name, func(t *testing.T) {
  482. s := &ZoneServer{
  483. UnimplementedZoneCloudflareServer: tt.fields.UnimplementedZoneCloudflareServer,
  484. }
  485. got, err := s.CreateArgoSettingSubscription(tt.args.ctx, tt.args.request)
  486. if (err != nil) != tt.wantErr {
  487. t.Errorf("CreateArgoSettingSubscription() error = %v, wantErr %v", err, tt.wantErr)
  488. return
  489. }
  490. if !reflect.DeepEqual(got, tt.want) {
  491. t.Errorf("CreateArgoSettingSubscription() got = %v, want %v", got, tt.want)
  492. }
  493. })
  494. }
  495. }
  496. func TestCreateZone(t *testing.T) {
  497. api, err := cloudflare.New("d2589c4f617b0493357ae187a7b4518171d07", "shiqi@centcloud.net")
  498. if err != nil {
  499. t.Fatal(err)
  500. }
  501. ctx := context.Background()
  502. account, r, err := api.Account(ctx, "b9145a83318cae896ad9b23bdf1681a5")
  503. if err != nil {
  504. t.Fatal(err)
  505. }
  506. t.Logf("Account Info:%+v", r)
  507. createZone, err := api.CreateZone(ctx, "cf004.shiqi.website", false, account, "partial")
  508. if err != nil {
  509. t.Fatal(err)
  510. }
  511. t.Logf("createZone: %+v", createZone)
  512. time.Sleep(time.Second)
  513. zoneId := createZone.ID
  514. err = api.ZoneSetPlan(ctx, zoneId, "PARTNERS_ENT")
  515. if err != nil {
  516. t.Fatal(err)
  517. }
  518. }
  519. func TestZoneServer_CreateZone(t *testing.T) {
  520. type fields struct {
  521. UnimplementedZoneCloudflareServer zone.UnimplementedZoneCloudflareServer
  522. }
  523. type args struct {
  524. ctx context.Context
  525. requestCloudflare *zone.CreateZoneRequestCloudflare
  526. }
  527. tests := []struct {
  528. name string
  529. fields fields
  530. args args
  531. want *zone.CreateZoneReplyCloudflare
  532. wantErr bool
  533. }{
  534. {
  535. name: "测试用例1",
  536. fields: fields{
  537. UnimplementedZoneCloudflareServer: zone.UnimplementedZoneCloudflareServer{},
  538. },
  539. args: args{
  540. ctx: context.Background(),
  541. requestCloudflare: &zone.CreateZoneRequestCloudflare{
  542. ApiKey: "d2589c4f617b0493357ae187a7b4518171d07",
  543. ApiEmail: "shiqi@centcloud.net",
  544. AccountId: "b9145a83318cae896ad9b23bdf1681a5",
  545. Domain: "cf005.shiqi.website",
  546. },
  547. },
  548. want: nil,
  549. wantErr: false,
  550. },
  551. }
  552. for _, tt := range tests {
  553. t.Run(tt.name, func(t *testing.T) {
  554. s := &ZoneServer{
  555. UnimplementedZoneCloudflareServer: tt.fields.UnimplementedZoneCloudflareServer,
  556. }
  557. got, err := s.CreateZone(tt.args.ctx, tt.args.requestCloudflare)
  558. if (err != nil) != tt.wantErr {
  559. t.Errorf("CreateZone() error = %v, wantErr %v", err, tt.wantErr)
  560. return
  561. }
  562. t.Logf("CreateZone() got = %v", got)
  563. })
  564. }
  565. }
  566. func TestZoneServer_SetZonePlan(t *testing.T) {
  567. type fields struct {
  568. UnimplementedZoneCloudflareServer zone.UnimplementedZoneCloudflareServer
  569. }
  570. type args struct {
  571. ctx context.Context
  572. requestCloudflare *zone.SetZonePlanRequestCloudflare
  573. }
  574. tests := []struct {
  575. name string
  576. fields fields
  577. args args
  578. want *zone.SetZonePlanReplyCloudflare
  579. wantErr bool
  580. }{
  581. {
  582. name: "测试用例1",
  583. fields: fields{
  584. UnimplementedZoneCloudflareServer: zone.UnimplementedZoneCloudflareServer{},
  585. },
  586. args: args{
  587. ctx: context.Background(),
  588. requestCloudflare: &zone.SetZonePlanRequestCloudflare{
  589. ApiKey: "d2589c4f617b0493357ae187a7b4518171d07",
  590. ApiEmail: "shiqi@centcloud.net",
  591. ZoneId: "19e7978bb9292fe11d3c75ff39f87685",
  592. ZonePlan: "PARTNERS_ENT",
  593. },
  594. },
  595. want: &zone.SetZonePlanReplyCloudflare{Success: true},
  596. wantErr: false,
  597. },
  598. }
  599. for _, tt := range tests {
  600. t.Run(tt.name, func(t *testing.T) {
  601. s := &ZoneServer{
  602. UnimplementedZoneCloudflareServer: tt.fields.UnimplementedZoneCloudflareServer,
  603. }
  604. got, err := s.SetZonePlan(tt.args.ctx, tt.args.requestCloudflare)
  605. if (err != nil) != tt.wantErr {
  606. t.Errorf("SetZonePlan() error = %v, wantErr %v", err, tt.wantErr)
  607. return
  608. }
  609. if !reflect.DeepEqual(got, tt.want) {
  610. t.Errorf("SetZonePlan() got = %v, want %v", got, tt.want)
  611. }
  612. })
  613. }
  614. }
  615. func TestZoneServer_CreateZoneRuleSet(t *testing.T) {
  616. type fields struct {
  617. UnimplementedZoneCloudflareServer zone.UnimplementedZoneCloudflareServer
  618. }
  619. type args struct {
  620. ctx context.Context
  621. requestCloudflare *zone.CreateZoneRuleSetRequestCloudflare
  622. }
  623. tests := []struct {
  624. name string
  625. fields fields
  626. args args
  627. want *zone.RuleSet
  628. wantErr bool
  629. }{
  630. {
  631. name: "测试用例1",
  632. fields: fields{
  633. UnimplementedZoneCloudflareServer: zone.UnimplementedZoneCloudflareServer{},
  634. },
  635. args: args{
  636. requestCloudflare: &zone.CreateZoneRuleSetRequestCloudflare{
  637. ApiKey: "d2589c4f617b0493357ae187a7b4518171d07",
  638. ApiEmail: "shiqi@centcloud.net",
  639. ZoneId: "5801a8cbe2fdfd6c53413d0eef164d23",
  640. RuleSet: &zone.RuleSet{
  641. Name: "rule1",
  642. Kind: "zone",
  643. Phase: zone.Phase_http_request_cache_settings,
  644. },
  645. Rules: []*zone.Rule{
  646. {
  647. Expression: "(http.request.uri.path contains \"shiqi.website/\")",
  648. Ttl: lo.ToPtr[uint32](604800),
  649. QueryArgsType: lo.ToPtr[string]("Include"),
  650. QueryArgs: []string{"qwe12", "78u9hi"},
  651. },
  652. },
  653. },
  654. },
  655. wantErr: false,
  656. },
  657. // TODO: Add test cases.
  658. }
  659. for _, tt := range tests {
  660. t.Run(tt.name, func(t *testing.T) {
  661. s := &ZoneServer{
  662. UnimplementedZoneCloudflareServer: tt.fields.UnimplementedZoneCloudflareServer,
  663. }
  664. got, err := s.CreateZoneRuleSet(tt.args.ctx, tt.args.requestCloudflare)
  665. if (err != nil) != tt.wantErr {
  666. t.Errorf("CreateZoneRuleSet() error = %v, wantErr %v", err, tt.wantErr)
  667. return
  668. }
  669. if !reflect.DeepEqual(got, tt.want) {
  670. t.Errorf("CreateZoneRuleSet() got = %v, want %v", got, tt.want)
  671. }
  672. })
  673. }
  674. }
  675. func TestZoneServer_ListZoneRuleSets(t *testing.T) {
  676. type fields struct {
  677. UnimplementedZoneCloudflareServer zone.UnimplementedZoneCloudflareServer
  678. }
  679. type args struct {
  680. ctx context.Context
  681. requestCloudflare *zone.ListZoneRuleSetsRequestCloudflare
  682. }
  683. tests := []struct {
  684. name string
  685. fields fields
  686. args args
  687. want *zone.RuleSetList
  688. wantErr bool
  689. }{
  690. {
  691. name: "测试用例1",
  692. fields: fields{
  693. UnimplementedZoneCloudflareServer: zone.UnimplementedZoneCloudflareServer{},
  694. },
  695. args: args{
  696. requestCloudflare: &zone.ListZoneRuleSetsRequestCloudflare{
  697. ApiKey: "d2589c4f617b0493357ae187a7b4518171d07",
  698. ApiEmail: "shiqi@centcloud.net",
  699. ZoneId: "5801a8cbe2fdfd6c53413d0eef164d23",
  700. },
  701. },
  702. wantErr: false,
  703. },
  704. }
  705. for _, tt := range tests {
  706. t.Run(tt.name, func(t *testing.T) {
  707. s := &ZoneServer{
  708. UnimplementedZoneCloudflareServer: tt.fields.UnimplementedZoneCloudflareServer,
  709. }
  710. got, err := s.ListZoneRuleSets(tt.args.ctx, tt.args.requestCloudflare)
  711. if (err != nil) != tt.wantErr {
  712. t.Errorf("ListZoneRuleSets() error = %v, wantErr %v", err, tt.wantErr)
  713. return
  714. }
  715. if !reflect.DeepEqual(got, tt.want) {
  716. t.Errorf("ListZoneRuleSets() got = %v, want %v", got, tt.want)
  717. }
  718. })
  719. }
  720. }
  721. func TestZoneServer_UpdateRuleSet1(t *testing.T) {
  722. type fields struct {
  723. UnimplementedZoneCloudflareServer zone.UnimplementedZoneCloudflareServer
  724. }
  725. type args struct {
  726. ctx context.Context
  727. requestCloudflare *zone.UpdateRuleSetRequestCloudflare
  728. }
  729. tests := []struct {
  730. name string
  731. fields fields
  732. args args
  733. want *zone.RuleList
  734. wantErr bool
  735. }{
  736. // TODO: Add test cases.
  737. {
  738. name: "测试用例1",
  739. fields: fields{
  740. UnimplementedZoneCloudflareServer: zone.UnimplementedZoneCloudflareServer{},
  741. },
  742. args: args{
  743. requestCloudflare: &zone.UpdateRuleSetRequestCloudflare{
  744. ApiKey: "d2589c4f617b0493357ae187a7b4518171d07",
  745. ApiEmail: "shiqi@centcloud.net",
  746. ZoneId: "5ee6daeeecf5c088c7517afa95c50739",
  747. Phase: zone.Phase_http_request_cache_settings,
  748. Rules: []*zone.Rule{
  749. {
  750. Expression: "(http.request.uri.path contains \"shiqi.website/\")",
  751. Ttl: lo.ToPtr[uint32](604800 / 7),
  752. QueryArgsType: lo.ToPtr[string]("Include"),
  753. QueryArgs: []string{"qwe12", "78u9hi"},
  754. },
  755. {
  756. Expression: "(http.request.uri.path contains \"shiqi.website/\")",
  757. Ttl: lo.ToPtr[uint32](604800),
  758. QueryArgsType: lo.ToPtr[string]("Include"),
  759. QueryArgs: []string{"qwe12"},
  760. },
  761. },
  762. },
  763. },
  764. wantErr: false,
  765. },
  766. }
  767. for _, tt := range tests {
  768. t.Run(tt.name, func(t *testing.T) {
  769. s := &ZoneServer{
  770. UnimplementedZoneCloudflareServer: tt.fields.UnimplementedZoneCloudflareServer,
  771. }
  772. got, err := s.UpdateRuleSet(tt.args.ctx, tt.args.requestCloudflare)
  773. fmt.Println(got, err)
  774. if (err != nil) != tt.wantErr {
  775. t.Errorf("UpdateRuleSet() error = %v, wantErr %v", err, tt.wantErr)
  776. return
  777. }
  778. if !reflect.DeepEqual(got, tt.want) {
  779. t.Errorf("UpdateRuleSet() got = %v, want %v", got, tt.want)
  780. }
  781. })
  782. }
  783. }
  784. //RuleSetId = 12371a315d344f719382becef2577d4c
  785. func TestZoneServer_UpdateRuleSetByRuleSetId(t *testing.T) {
  786. type fields struct {
  787. UnimplementedZoneCloudflareServer zone.UnimplementedZoneCloudflareServer
  788. }
  789. type args struct {
  790. ctx context.Context
  791. requestCloudflare *zone.UpdateRuleSetByRuleSetIdRequestCloudflare
  792. }
  793. tests := []struct {
  794. name string
  795. fields fields
  796. args args
  797. want *zone.RuleSet
  798. wantErr bool
  799. }{
  800. // TODO: Add test cases.
  801. {
  802. name: "测试用例1",
  803. fields: fields{
  804. UnimplementedZoneCloudflareServer: zone.UnimplementedZoneCloudflareServer{},
  805. },
  806. args: args{
  807. requestCloudflare: &zone.UpdateRuleSetByRuleSetIdRequestCloudflare{
  808. ApiKey: "d2589c4f617b0493357ae187a7b4518171d07",
  809. ApiEmail: "shiqi@centcloud.net",
  810. ZoneId: "f62e1be1fea702904da5ead2d1dc15de",
  811. RuleSetId: "12371a315d344f719382becef2577d4c",
  812. Phase: zone.Phase_http_request_cache_settings,
  813. Rules: []*zone.Rule{
  814. {
  815. Expression: "(http.request.uri.path contains \"shi.website/\")",
  816. Ttl: lo.ToPtr[uint32](604800),
  817. QueryArgsType: lo.ToPtr[string]("ExcludeAll"),
  818. QueryArgs: []string{"qwe12", "78u9hi"},
  819. },
  820. },
  821. },
  822. },
  823. wantErr: false,
  824. },
  825. }
  826. for _, tt := range tests {
  827. t.Run(tt.name, func(t *testing.T) {
  828. s := &ZoneServer{
  829. UnimplementedZoneCloudflareServer: tt.fields.UnimplementedZoneCloudflareServer,
  830. }
  831. got, err := s.UpdateRuleSetByRuleSetId(tt.args.ctx, tt.args.requestCloudflare)
  832. if (err != nil) != tt.wantErr {
  833. t.Errorf("UpdateRuleSetByRuleSetId() error = %v, wantErr %v", err, tt.wantErr)
  834. return
  835. }
  836. if !reflect.DeepEqual(got, tt.want) {
  837. t.Errorf("UpdateRuleSetByRuleSetId() got = %v, want %v", got, tt.want)
  838. }
  839. })
  840. }
  841. }
  842. func TestZoneServer_DeleteZoneRuleSet(t *testing.T) {
  843. type fields struct {
  844. UnimplementedZoneCloudflareServer zone.UnimplementedZoneCloudflareServer
  845. }
  846. type args struct {
  847. ctx context.Context
  848. requestCloudflare *zone.DeleteZoneRuleSetRequestCloudflare
  849. }
  850. tests := []struct {
  851. name string
  852. fields fields
  853. args args
  854. want *zone.DeleteZoneRuleSetReplyCloudflare
  855. wantErr bool
  856. }{
  857. {
  858. name: "测试用例1",
  859. fields: fields{
  860. UnimplementedZoneCloudflareServer: zone.UnimplementedZoneCloudflareServer{},
  861. },
  862. args: args{
  863. requestCloudflare: &zone.DeleteZoneRuleSetRequestCloudflare{
  864. ApiKey: "d2589c4f617b0493357ae187a7b4518171d07",
  865. ApiEmail: "shiqi@centcloud.net",
  866. ZoneId: "5801a8cbe2fdfd6c53413d0eef164d23",
  867. RuleSetId: "c9e073e5447c461da612be101d64ec04",
  868. },
  869. },
  870. wantErr: false,
  871. },
  872. }
  873. for _, tt := range tests {
  874. t.Run(tt.name, func(t *testing.T) {
  875. s := &ZoneServer{
  876. UnimplementedZoneCloudflareServer: tt.fields.UnimplementedZoneCloudflareServer,
  877. }
  878. got, err := s.DeleteZoneRuleSet(tt.args.ctx, tt.args.requestCloudflare)
  879. if (err != nil) != tt.wantErr {
  880. t.Errorf("DeleteZoneRuleSet() error = %v, wantErr %v", err, tt.wantErr)
  881. return
  882. }
  883. if !reflect.DeepEqual(got, tt.want) {
  884. t.Errorf("DeleteZoneRuleSet() got = %v, want %v", got, tt.want)
  885. }
  886. })
  887. }
  888. }
  889. func TestZoneServer_UpdateAlwaysUseHttps(t *testing.T) {
  890. type fields struct {
  891. UnimplementedZoneCloudflareServer zone.UnimplementedZoneCloudflareServer
  892. }
  893. type args struct {
  894. ctx context.Context
  895. requestCloudflare *zone.UpdateAlwaysUseHttpsRequestCloudflare
  896. }
  897. tests := []struct {
  898. name string
  899. fields fields
  900. args args
  901. want *zone.UpdateAlwaysUseHttpsReplyCloudflare
  902. wantErr bool
  903. }{
  904. // TODO: Add test cases.
  905. {
  906. name: "测试用例1",
  907. fields: fields{
  908. UnimplementedZoneCloudflareServer: zone.UnimplementedZoneCloudflareServer{},
  909. },
  910. args: args{
  911. requestCloudflare: &zone.UpdateAlwaysUseHttpsRequestCloudflare{
  912. ApiKey: "d2589c4f617b0493357ae187a7b4518171d07",
  913. ApiEmail: "shiqi@centcloud.net",
  914. ZoneId: "5801a8cbe2fdfd6c53413d0eef164d23",
  915. Enabled: false,
  916. },
  917. },
  918. wantErr: false,
  919. },
  920. }
  921. for _, tt := range tests {
  922. t.Run(tt.name, func(t *testing.T) {
  923. s := &ZoneServer{
  924. UnimplementedZoneCloudflareServer: tt.fields.UnimplementedZoneCloudflareServer,
  925. }
  926. got, err := s.UpdateAlwaysUseHttps(tt.args.ctx, tt.args.requestCloudflare)
  927. if (err != nil) != tt.wantErr {
  928. t.Errorf("UpdateAlwaysUseHttps() error = %v, wantErr %v", err, tt.wantErr)
  929. return
  930. }
  931. if !reflect.DeepEqual(got, tt.want) {
  932. t.Errorf("UpdateAlwaysUseHttps() got = %v, want %v", got, tt.want)
  933. }
  934. })
  935. }
  936. }