ZoneCloudflareServer_test.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. package server
  2. import (
  3. "cfTest/cloudflareApi/zone"
  4. "reflect"
  5. "context"
  6. "github.com/samber/lo"
  7. "log"
  8. "testing"
  9. )
  10. //func TestCreateRuleSet(t *testing.T) {
  11. // type args struct {
  12. // zoneID string
  13. // rulesetPhase string
  14. // ruleset cloudflare.Ruleset
  15. // }
  16. //
  17. // var (
  18. // True = true
  19. // CONST604800 = uint(604800)
  20. // )
  21. //
  22. // tests := []struct {
  23. // name string
  24. // args args
  25. // want interface{}
  26. // wantErr bool
  27. // }{
  28. // {
  29. // name: "Test1",
  30. // args: args{
  31. // zoneID: "e29551894461bf16e43a113e5ab09ed3",
  32. // rulesetPhase: "http_request_cache_settings",
  33. // ruleset: cloudflare.Ruleset{
  34. // Rules: []cloudflare.RulesetRule{
  35. // {
  36. // Expression: "(starts_with(http.request.uri.path, \"/\"))",
  37. // Enabled: true,
  38. // Description: "test1",
  39. // Action: "set_cache_settings",
  40. // ActionParameters: &cloudflare.RulesetRuleActionParameters{
  41. // Cache: &True,
  42. // CacheKey: &cloudflare.RulesetRuleActionParametersCacheKey{
  43. // CustomKey: &cloudflare.RulesetRuleActionParametersCustomKey{
  44. // Query: &cloudflare.RulesetRuleActionParametersCustomKeyQuery{
  45. // Exclude: &cloudflare.RulesetRuleActionParametersCustomKeyList{
  46. // List: []string{
  47. // "abc",
  48. // },
  49. // All: false,
  50. // },
  51. // },
  52. // },
  53. // },
  54. // EdgeTTL: &cloudflare.RulesetRuleActionParametersEdgeTTL{
  55. // Mode: "override_origin",
  56. // Default: &CONST604800,
  57. // },
  58. // },
  59. // },
  60. // },
  61. // },
  62. // },
  63. // want: nil,
  64. // wantErr: false,
  65. // },
  66. // }
  67. // for _, tt := range tests {
  68. // t.Run(tt.name, func(t *testing.T) {
  69. // got, err := UpdateRuleSet("", "", tt.args.zoneID, tt.args.rulesetPhase, tt.args.ruleset)
  70. // if (err != nil) != tt.wantErr {
  71. // t.Errorf("UpdateRuleSet() error = %v, wantErr %v", err, tt.wantErr)
  72. // return
  73. // }
  74. // if !reflect.DeepEqual(got, tt.want) {
  75. // t.Errorf("UpdateRuleSet() got = %v, want %v", got, tt.want)
  76. // }
  77. // })
  78. // }
  79. //}
  80. //func newUint32(a uint32) *uint32 {
  81. // return &a
  82. //}
  83. //func newStr(a string) *string {
  84. // return &a
  85. //}
  86. var (
  87. apiKey = "2cc87f6213a4c40e5a90c8dced3c9c577eda5"
  88. apiEmail = "ll@centcloud.net"
  89. )
  90. //func TestUpdatePageRule(t *testing.T) {
  91. //
  92. // type args struct {
  93. // apiKey string
  94. // apiEmail string
  95. // zoneID string
  96. // rule *zone.Rule
  97. // }
  98. // tests := []struct {
  99. // name string
  100. // args args
  101. // want *cloudflare.PageRule
  102. // wantErr bool
  103. // }{
  104. // {
  105. // name: "测试1",
  106. // args: args{
  107. // apiKey: apiKey,
  108. // apiEmail: apiEmail,
  109. // zoneID: "e29551894461bf16e43a113e5ab09ed3",
  110. // rule: &zone.Rule{
  111. // Expression: "www.1011test5.com.cdn.sensordb.site/*\n",
  112. // Ttl: newUint32(18000),
  113. // QueryArgsType: newStr("ExcludeAll"),
  114. // QueryArgs: []string{"qwe12", "78u9hi"},
  115. // OriginHost: newStr("www.baidu.com"),
  116. // OriginPath: nil,
  117. // OriginProtocol: newUint32(80),
  118. // RequestHeader: nil,
  119. // ResponseHeader: nil,
  120. // },
  121. // },
  122. // want: nil,
  123. // wantErr: false,
  124. // },
  125. // // TODO: Add test cases.
  126. // }
  127. // for _, tt := range tests {
  128. // t.Run(tt.name, func(t *testing.T) {
  129. // got, err := UpdatePageRule(tt.args.apiKey, tt.args.apiEmail, tt.args.zoneID, tt.args.rule)
  130. // if (err != nil) != tt.wantErr {
  131. // t.Errorf("UpdatePageRule() error = %v, wantErr %v", err, tt.wantErr)
  132. // return
  133. // }
  134. // print("%v", got)
  135. // //if !reflect.DeepEqual(got, tt.want) {
  136. // // t.Errorf("UpdatePageRule() got = %v, want %v", got, tt.want)
  137. // //}
  138. // })
  139. // }
  140. //}
  141. func TestZoneServer_GetRuleSet(t *testing.T) {
  142. type fields struct {
  143. UnimplementedZoneCloudflareServer zone.UnimplementedZoneCloudflareServer
  144. }
  145. type args struct {
  146. ctx context.Context
  147. requestCloudflare *zone.GetRuleSetRequestCloudflare
  148. }
  149. tests := []struct {
  150. name string
  151. fields fields
  152. args args
  153. want *zone.RuleList
  154. wantErr bool
  155. }{
  156. {
  157. name: "测试1",
  158. fields: fields{
  159. UnimplementedZoneCloudflareServer: zone.UnimplementedZoneCloudflareServer{},
  160. },
  161. args: args{
  162. ctx: context.Background(),
  163. requestCloudflare: &zone.GetRuleSetRequestCloudflare{
  164. ApiKey: apiKey,
  165. ApiEmail: apiEmail,
  166. ZoneId: "e29551894461bf16e43a113e5ab09ed3",
  167. Phase: zone.Phase_http_request_transform,
  168. },
  169. },
  170. want: nil,
  171. wantErr: false,
  172. },
  173. }
  174. for _, tt := range tests {
  175. t.Run(tt.name, func(t *testing.T) {
  176. s := &ZoneServer{
  177. UnimplementedZoneCloudflareServer: tt.fields.UnimplementedZoneCloudflareServer,
  178. }
  179. got, err := s.GetRuleSet(tt.args.ctx, tt.args.requestCloudflare)
  180. if (err != nil) != tt.wantErr {
  181. t.Errorf("GetRuleSet() error = %v, wantErr %v", err, tt.wantErr)
  182. return
  183. }
  184. log.Print(got)
  185. //if !reflect.DeepEqual(got, tt.want) {
  186. // t.Errorf("GetRuleSet() got = %v, want %v", got, tt.want)
  187. //}
  188. })
  189. }
  190. }
  191. func TestZoneServer_UpdateRuleSet(t *testing.T) {
  192. type fields struct {
  193. UnimplementedZoneCloudflareServer zone.UnimplementedZoneCloudflareServer
  194. }
  195. type args struct {
  196. ctx context.Context
  197. requestCloudflare *zone.UpdateRuleSetRequestCloudflare
  198. }
  199. tests := []struct {
  200. name string
  201. fields fields
  202. args args
  203. want *zone.RuleList
  204. wantErr bool
  205. }{
  206. {
  207. name: "测试1",
  208. fields: fields{
  209. UnimplementedZoneCloudflareServer: zone.UnimplementedZoneCloudflareServer{},
  210. },
  211. args: args{
  212. ctx: nil,
  213. requestCloudflare: &zone.UpdateRuleSetRequestCloudflare{
  214. ApiKey: apiKey,
  215. ApiEmail: apiEmail,
  216. ZoneId: "e29551894461bf16e43a113e5ab09ed3",
  217. Phase: zone.Phase_http_request_transform,
  218. Rules: []*zone.Rule{
  219. {
  220. Expression: "(http.request.uri.path contains \"www.1011test5.com.cdn.sensordb.site/\")",
  221. OriginPath: lo.ToPtr[string]("www.baidu.com"),
  222. RequestHeader: nil,
  223. ResponseHeader: nil,
  224. },
  225. },
  226. },
  227. },
  228. want: nil,
  229. wantErr: false,
  230. },
  231. }
  232. for _, tt := range tests {
  233. t.Run(tt.name, func(t *testing.T) {
  234. s := &ZoneServer{
  235. UnimplementedZoneCloudflareServer: tt.fields.UnimplementedZoneCloudflareServer,
  236. }
  237. got, err := s.UpdateRuleSet(tt.args.ctx, tt.args.requestCloudflare)
  238. if (err != nil) != tt.wantErr {
  239. t.Errorf("UpdateRuleSet() error = %v, wantErr %v", err, tt.wantErr)
  240. return
  241. }
  242. log.Print(got)
  243. //if !reflect.DeepEqual(got, tt.want) {
  244. // t.Errorf("UpdateRuleSet() got = %v, want %v", got, tt.want)
  245. //}
  246. })
  247. }
  248. }
  249. func TestZoneServer_GetArgoSetting(t *testing.T) {
  250. type fields struct {
  251. UnimplementedZoneCloudflareServer zone.UnimplementedZoneCloudflareServer
  252. }
  253. type args struct {
  254. ctx context.Context
  255. requestCloudflare *zone.IdRequest
  256. }
  257. tests := []struct {
  258. name string
  259. fields fields
  260. args args
  261. want *zone.ArgoSetting
  262. wantErr bool
  263. }{
  264. {
  265. name: "test1",
  266. fields: fields{
  267. UnimplementedZoneCloudflareServer: zone.UnimplementedZoneCloudflareServer{},
  268. },
  269. args: args{
  270. ctx: context.Background(),
  271. requestCloudflare: &zone.IdRequest{
  272. ApiKey: apiKey,
  273. ApiEmail: apiEmail,
  274. ZoneId: "f7eefa29114db32ab14397bdd5048ec8",
  275. },
  276. },
  277. want: &zone.ArgoSetting{Enabled: true},
  278. wantErr: false,
  279. },
  280. {
  281. name: "test1",
  282. fields: fields{
  283. UnimplementedZoneCloudflareServer: zone.UnimplementedZoneCloudflareServer{},
  284. },
  285. args: args{
  286. ctx: context.Background(),
  287. requestCloudflare: &zone.IdRequest{
  288. ApiKey: apiKey,
  289. ApiEmail: apiEmail,
  290. ZoneId: "f7eefa29114db32ab14397bdd5048ec8",
  291. },
  292. },
  293. want: &zone.ArgoSetting{Enabled: false},
  294. wantErr: false,
  295. },
  296. }
  297. for _, tt := range tests {
  298. t.Run(tt.name, func(t *testing.T) {
  299. s := &ZoneServer{
  300. UnimplementedZoneCloudflareServer: tt.fields.UnimplementedZoneCloudflareServer,
  301. }
  302. got, err := s.GetArgoSetting(tt.args.ctx, tt.args.requestCloudflare)
  303. if (err != nil) != tt.wantErr {
  304. t.Errorf("GetArgoSetting() error = %v, wantErr %v", err, tt.wantErr)
  305. return
  306. }
  307. if !reflect.DeepEqual(got, tt.want) {
  308. t.Errorf("GetArgoSetting() got = %v, want %v", got, tt.want)
  309. }
  310. })
  311. }
  312. }
  313. func TestZoneServer_UpdateArgoSetting(t *testing.T) {
  314. type fields struct {
  315. UnimplementedZoneCloudflareServer zone.UnimplementedZoneCloudflareServer
  316. }
  317. type args struct {
  318. ctx context.Context
  319. requestCloudflare *zone.UpdateArgoRequestCloudflare
  320. }
  321. tests := []struct {
  322. name string
  323. fields fields
  324. args args
  325. want *zone.ArgoSetting
  326. wantErr bool
  327. }{
  328. {
  329. name: "test1",
  330. fields: fields{
  331. UnimplementedZoneCloudflareServer: zone.UnimplementedZoneCloudflareServer{},
  332. },
  333. args: args{
  334. ctx: context.Background(),
  335. requestCloudflare: &zone.UpdateArgoRequestCloudflare{
  336. ApiKey: apiKey,
  337. ApiEmail: apiEmail,
  338. ZoneId: "f7eefa29114db32ab14397bdd5048ec8",
  339. ArgoSetting: &zone.ArgoSetting{Enabled: false},
  340. },
  341. },
  342. want: &zone.ArgoSetting{Enabled: false},
  343. wantErr: false,
  344. },
  345. {
  346. name: "test1",
  347. fields: fields{
  348. UnimplementedZoneCloudflareServer: zone.UnimplementedZoneCloudflareServer{},
  349. },
  350. args: args{
  351. ctx: context.Background(),
  352. requestCloudflare: &zone.UpdateArgoRequestCloudflare{
  353. ApiKey: apiKey,
  354. ApiEmail: apiEmail,
  355. ZoneId: "f7eefa29114db32ab14397bdd5048ec8",
  356. ArgoSetting: &zone.ArgoSetting{Enabled: true},
  357. },
  358. },
  359. want: &zone.ArgoSetting{Enabled: true},
  360. wantErr: false,
  361. },
  362. }
  363. for _, tt := range tests {
  364. t.Run(tt.name, func(t *testing.T) {
  365. s := &ZoneServer{
  366. UnimplementedZoneCloudflareServer: tt.fields.UnimplementedZoneCloudflareServer,
  367. }
  368. got, err := s.UpdateArgoSetting(tt.args.ctx, tt.args.requestCloudflare)
  369. if (err != nil) != tt.wantErr {
  370. t.Errorf("UpdateArgoSetting() error = %v, wantErr %v", err, tt.wantErr)
  371. return
  372. }
  373. if !reflect.DeepEqual(got, tt.want) {
  374. t.Errorf("UpdateArgoSetting() got = %v, want %v", got, tt.want)
  375. }
  376. })
  377. }
  378. }
  379. func TestZoneServer_GetArgoSettingSubscription(t *testing.T) {
  380. type fields struct {
  381. UnimplementedZoneCloudflareServer zone.UnimplementedZoneCloudflareServer
  382. }
  383. type args struct {
  384. ctx context.Context
  385. request *zone.IdRequest
  386. }
  387. tests := []struct {
  388. name string
  389. fields fields
  390. args args
  391. want *zone.ArgoSetting
  392. wantErr bool
  393. }{
  394. {
  395. name: "启用测试",
  396. fields: fields{
  397. UnimplementedZoneCloudflareServer: zone.UnimplementedZoneCloudflareServer{},
  398. },
  399. args: args{
  400. ctx: nil,
  401. request: &zone.IdRequest{
  402. ApiKey: apiKey,
  403. ApiEmail: apiEmail,
  404. ZoneId: "f7eefa29114db32ab14397bdd5048ec8",
  405. },
  406. },
  407. want: &zone.ArgoSetting{Enabled: true},
  408. wantErr: false,
  409. },
  410. {
  411. name: "未启用测试",
  412. fields: fields{
  413. UnimplementedZoneCloudflareServer: zone.UnimplementedZoneCloudflareServer{},
  414. },
  415. args: args{
  416. ctx: nil,
  417. request: &zone.IdRequest{
  418. ApiKey: apiKey,
  419. ApiEmail: apiEmail,
  420. ZoneId: "6d89e53e52a08fc51b99628ff546c7fb",
  421. },
  422. },
  423. want: &zone.ArgoSetting{Enabled: false},
  424. wantErr: false,
  425. },
  426. }
  427. for _, tt := range tests {
  428. t.Run(tt.name, func(t *testing.T) {
  429. s := &ZoneServer{
  430. UnimplementedZoneCloudflareServer: tt.fields.UnimplementedZoneCloudflareServer,
  431. }
  432. got, err := s.GetArgoSettingSubscription(tt.args.ctx, tt.args.request)
  433. if (err != nil) != tt.wantErr {
  434. t.Errorf("GetArgoSettingSubscription() error = %v, wantErr %v", err, tt.wantErr)
  435. return
  436. }
  437. if !reflect.DeepEqual(got, tt.want) {
  438. t.Errorf("GetArgoSettingSubscription() got = %v, want %v", got, tt.want)
  439. }
  440. })
  441. }
  442. }
  443. func TestZoneServer_CreateArgoSettingSubscription(t *testing.T) {
  444. type fields struct {
  445. UnimplementedZoneCloudflareServer zone.UnimplementedZoneCloudflareServer
  446. }
  447. type args struct {
  448. ctx context.Context
  449. request *zone.IdRequest
  450. }
  451. tests := []struct {
  452. name string
  453. fields fields
  454. args args
  455. want *zone.ArgoSetting
  456. wantErr bool
  457. }{
  458. {
  459. name: "测试",
  460. fields: fields{
  461. UnimplementedZoneCloudflareServer: zone.UnimplementedZoneCloudflareServer{},
  462. },
  463. args: args{
  464. ctx: nil,
  465. request: &zone.IdRequest{
  466. ApiKey: apiKey,
  467. ApiEmail: apiEmail,
  468. ZoneId: "7a25de4f4ea643e99c66e84e7498ccc6",
  469. },
  470. },
  471. want: &zone.ArgoSetting{Enabled: true},
  472. wantErr: false,
  473. },
  474. }
  475. for _, tt := range tests {
  476. t.Run(tt.name, func(t *testing.T) {
  477. s := &ZoneServer{
  478. UnimplementedZoneCloudflareServer: tt.fields.UnimplementedZoneCloudflareServer,
  479. }
  480. got, err := s.CreateArgoSettingSubscription(tt.args.ctx, tt.args.request)
  481. if (err != nil) != tt.wantErr {
  482. t.Errorf("CreateArgoSettingSubscription() error = %v, wantErr %v", err, tt.wantErr)
  483. return
  484. }
  485. if !reflect.DeepEqual(got, tt.want) {
  486. t.Errorf("CreateArgoSettingSubscription() got = %v, want %v", got, tt.want)
  487. }
  488. })
  489. }
  490. }