http_test.go 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752
  1. package http
  2. import (
  3. "bytes"
  4. "crypto/md5"
  5. "encoding/hex"
  6. "encoding/json"
  7. "errors"
  8. "fmt"
  9. "io/ioutil"
  10. "net/http"
  11. "net/url"
  12. "os"
  13. "strconv"
  14. "testing"
  15. "time"
  16. "go-common/app/admin/main/dm/conf"
  17. )
  18. const (
  19. localHost = "http://127.0.0.1:6311"
  20. host = "http://uat-api.bilibili.co"
  21. managerHost = "http://uat-manager.bilibili.co"
  22. )
  23. var (
  24. _dmList = host + "/x/internal/dmadmin/content/list"
  25. _refresh = host + "/x/internal/dmadmin/content/refresh"
  26. _editState = host + "/x/internal/dmadmin/content/edit/state"
  27. _editPool = host + "/x/internal/dmadmin/content/edit/pool"
  28. _editAttr = host + "/x/internal/dmadmin/content/edit/attr"
  29. _monitorList = host + "/x/internal/dmadmin/monitor/list"
  30. _editMonitor = host + "/x/internal/dmadmin/monitor/edit"
  31. _dmReportStat = host + "/x/internal/dmadmin/report/stat/change"
  32. _dmReportList = host + "/x/internal/dmadmin/report/list"
  33. _dmReportdetail = host + "/x/internal/dmadmin/report"
  34. _archiveList = localHost + "/x/admin/dm/subject/archive"
  35. _subjectLog = localHost + "/x/admin/dm/subject/log"
  36. _uptSubState = localHost + "/x/admin/dm/subject/state/edit"
  37. _dmSendJudge = host + "/x/internal/dmadmin/report/judge"
  38. _dmJudgeResult = host + "/x/internal/dmadmin/report/judge/result"
  39. _dmIndexInfoURL = host + "/x/internal/dmadmin/content/index/info"
  40. _subjectLimit = host + "/x/admin/dm/subject/maxlimit"
  41. _maskUps = localHost + "/x/admin/dm/mask/up"
  42. _maskUpOpen = localHost + "/x/admin/dm/mask/up/open"
  43. _taskList = managerHost + "/x/admin/dm/task/list"
  44. _taskNew = managerHost + "/x/admin/dm/task/new"
  45. _taskReview = localHost + "/x/admin/dm/task/review"
  46. _taskView = localHost + "/x/admin/dm/task/view"
  47. )
  48. func TestMain(m *testing.M) {
  49. var err error
  50. conf.ConfPath = "../cmd/dm-admin-test.toml"
  51. if err = conf.Init(); err != nil {
  52. panic(err)
  53. }
  54. os.Exit(m.Run())
  55. }
  56. // TestDMList list
  57. func TestDMList(t *testing.T) {
  58. params := url.Values{}
  59. params.Set("type", "1")
  60. params.Set("oid", "10108994")
  61. params.Set("keyword", "哈哈")
  62. params.Set("page_size", "1")
  63. params.Set("appkey", "f6433799dbd88751")
  64. params.Set("ts", strconv.FormatInt(time.Now().Unix(), 10))
  65. mh := md5.Sum([]byte(params.Encode() + "36f8ddb1806207fe07013ab6a77a3935"))
  66. params.Set("sign", hex.EncodeToString(mh[:]))
  67. s := _dmList + "?" + params.Encode()
  68. fmt.Println(s)
  69. body, err := oget(s)
  70. if err != nil {
  71. t.Errorf("url(%s) error(%s)", s, err)
  72. t.FailNow()
  73. }
  74. if err = ccode(body); err != nil {
  75. t.Fatal(err, string(body))
  76. t.FailNow()
  77. }
  78. fmt.Println(string(body))
  79. }
  80. // TestRefrash refrash
  81. func TestRefresh(t *testing.T) {
  82. params := url.Values{}
  83. params.Set("cid", "8937277")
  84. params.Set("appkey", "f6433799dbd88751")
  85. params.Set("ts", strconv.FormatInt(time.Now().Unix(), 10))
  86. mh := md5.Sum([]byte(params.Encode() + "36f8ddb1806207fe07013ab6a77a3935"))
  87. params.Set("sign", hex.EncodeToString(mh[:]))
  88. url := _refresh + "?" + params.Encode()
  89. t.Log(url)
  90. body, err := opost(url)
  91. if err != nil {
  92. t.Errorf("url(%s) error(%s)", url, err)
  93. t.FailNow()
  94. }
  95. if err = ccode(body); err != nil {
  96. t.Fatal(err, string(body))
  97. t.FailNow()
  98. }
  99. var out bytes.Buffer
  100. if err = json.Indent(&out, body, "", " "); err != nil {
  101. t.Fatal(err)
  102. t.FailNow()
  103. }
  104. fmt.Println(out.String())
  105. }
  106. // TestEditState batch
  107. func TestEditState(t *testing.T) {
  108. params := url.Values{}
  109. params.Set("type", "1")
  110. params.Set("oid", "1222")
  111. params.Set("state", "0")
  112. params.Set("dmids", "719181990,719181991")
  113. params.Set("moral", "1")
  114. params.Set("uname", "test")
  115. params.Set("remark", "测试下")
  116. params.Set("appkey", "f6433799dbd88751")
  117. params.Set("ts", strconv.FormatInt(time.Now().Unix(), 10))
  118. mh := md5.Sum([]byte(params.Encode() + "36f8ddb1806207fe07013ab6a77a3935"))
  119. params.Set("sign", hex.EncodeToString(mh[:]))
  120. url := _editState + "?" + params.Encode()
  121. body, err := opost(url)
  122. if err != nil {
  123. t.Errorf("url(%s) error(%s)", url, err)
  124. t.FailNow()
  125. }
  126. if err = ccode(body); err != nil {
  127. t.Fatal(err, string(body))
  128. t.FailNow()
  129. }
  130. var out bytes.Buffer
  131. if err = json.Indent(&out, body, "", " "); err != nil {
  132. t.Fatal(err)
  133. t.FailNow()
  134. }
  135. fmt.Println(out.String())
  136. }
  137. // TestEditPool batch
  138. func TestEditPool(t *testing.T) {
  139. params := url.Values{}
  140. params.Set("type", "1")
  141. params.Set("oid", "1222")
  142. params.Set("pool", "0")
  143. params.Set("dmids", "719181990,719181991")
  144. params.Set("appkey", "f6433799dbd88751")
  145. params.Set("ts", strconv.FormatInt(time.Now().Unix(), 10))
  146. mh := md5.Sum([]byte(params.Encode() + "36f8ddb1806207fe07013ab6a77a3935"))
  147. params.Set("sign", hex.EncodeToString(mh[:]))
  148. url := _editPool + "?" + params.Encode()
  149. fmt.Println(url)
  150. body, err := opost(url)
  151. if err != nil {
  152. t.Errorf("url(%s) error(%s)", url, err)
  153. t.FailNow()
  154. }
  155. if err = ccode(body); err != nil {
  156. t.Fatal(err, string(body))
  157. t.FailNow()
  158. }
  159. var out bytes.Buffer
  160. if err = json.Indent(&out, body, "", " "); err != nil {
  161. t.Fatal(err)
  162. t.FailNow()
  163. }
  164. fmt.Println(out.String())
  165. }
  166. // TestEditAttr batch
  167. func TestEditAttr(t *testing.T) {
  168. params := url.Values{}
  169. params.Set("type", "1")
  170. params.Set("oid", "1222")
  171. params.Set("attr", "1")
  172. params.Set("dmids", "719181990,719181991")
  173. params.Set("appkey", "f6433799dbd88751")
  174. params.Set("ts", strconv.FormatInt(time.Now().Unix(), 10))
  175. mh := md5.Sum([]byte(params.Encode() + "36f8ddb1806207fe07013ab6a77a3935"))
  176. params.Set("sign", hex.EncodeToString(mh[:]))
  177. url := _editAttr + "?" + params.Encode()
  178. fmt.Println(url)
  179. body, err := opost(url)
  180. if err != nil {
  181. t.Errorf("url(%s) error(%s)", url, err)
  182. t.FailNow()
  183. }
  184. if err = ccode(body); err != nil {
  185. t.Fatal(err, string(body))
  186. t.FailNow()
  187. }
  188. var out bytes.Buffer
  189. if err = json.Indent(&out, body, "", " "); err != nil {
  190. t.Fatal(err)
  191. t.FailNow()
  192. }
  193. fmt.Println(out.String())
  194. }
  195. func TestArchiveList(t *testing.T) {
  196. p := url.Values{}
  197. // p.Set("type", "oid")
  198. // p.Set("id", "1345")
  199. p.Set("sort", "desc")
  200. p.Set("order", "mtime")
  201. requestURL := _archiveList + "?" + p.Encode()
  202. fmt.Println(requestURL)
  203. body, err := oget(requestURL)
  204. fmt.Println(string(body))
  205. if err != nil {
  206. t.Errorf("url(%s) error(%s)", requestURL, err)
  207. t.FailNow()
  208. }
  209. var out bytes.Buffer
  210. if err = json.Indent(&out, body, "", " "); err != nil {
  211. t.Fatal(err)
  212. t.FailNow()
  213. }
  214. fmt.Println(out.String())
  215. }
  216. func TestSubjectLog(t *testing.T) {
  217. p := url.Values{}
  218. p.Set("type", "1")
  219. p.Set("oid", "1221")
  220. requestURL := _subjectLog + "?" + p.Encode()
  221. fmt.Println(requestURL)
  222. body, err := oget(requestURL)
  223. fmt.Println(string(body))
  224. if err != nil {
  225. t.Errorf("url(%s) error(%s)", requestURL, err)
  226. t.FailNow()
  227. }
  228. var out bytes.Buffer
  229. if err = json.Indent(&out, body, "", " "); err != nil {
  230. t.Fatal(err)
  231. t.FailNow()
  232. }
  233. fmt.Println(out.String())
  234. }
  235. func TestUptSubjectsState(t *testing.T) {
  236. p := url.Values{}
  237. p.Set("oids", "1223")
  238. p.Set("state", "1")
  239. requestURL := _uptSubState + "?" + p.Encode()
  240. fmt.Println(requestURL)
  241. body, err := opost(requestURL)
  242. fmt.Println(string(body))
  243. if err != nil {
  244. t.Errorf("url(%s) error(%s)", requestURL, err)
  245. t.FailNow()
  246. }
  247. if err = ccode(body); err != nil {
  248. t.Fatal(err, string(body))
  249. t.FailNow()
  250. }
  251. var out bytes.Buffer
  252. if err = json.Indent(&out, body, "", " "); err != nil {
  253. t.Fatal(err)
  254. t.FailNow()
  255. }
  256. fmt.Println(out.String())
  257. }
  258. func TestReportStatChange(t *testing.T) {
  259. params := url.Values{}
  260. params.Set("ids", "9968618:719923090,719923092,719923093")
  261. params.Set("state", "2")
  262. params.Set("uid", "150781")
  263. params.Set("reason", "1")
  264. params.Set("state", "3")
  265. params.Set("remark", "test")
  266. params.Set("notice", "3")
  267. params.Set("block", "-1")
  268. params.Set("moral", "10")
  269. params.Set("block_reason", "5")
  270. params.Set("uname", "zhanghongwen")
  271. params.Set("appkey", "f6433799dbd88751")
  272. params.Set("ts", strconv.FormatInt(time.Now().Unix(), 10))
  273. mh := md5.Sum([]byte(params.Encode() + "36f8ddb1806207fe07013ab6a77a3935"))
  274. params.Set("sign", hex.EncodeToString(mh[:]))
  275. requestURL := _dmReportStat + "?" + params.Encode()
  276. body, err := opost(requestURL)
  277. if err != nil {
  278. t.Errorf("url(%s) error(%s)", requestURL, err)
  279. t.FailNow()
  280. }
  281. if err = ccode(body); err != nil {
  282. t.Fatal(err, string(body))
  283. t.FailNow()
  284. }
  285. var out bytes.Buffer
  286. if err = json.Indent(&out, body, "", " "); err != nil {
  287. t.Fatal(err)
  288. t.FailNow()
  289. }
  290. fmt.Println(out.String())
  291. }
  292. func TestReportList(t *testing.T) {
  293. params := url.Values{}
  294. params.Set("state", "0,1")
  295. params.Set("up_op", "0")
  296. params.Set("page", "1")
  297. params.Set("tid", "24")
  298. params.Set("aid", "")
  299. params.Set("uid", "")
  300. params.Set("rp_user", "")
  301. params.Set("rp_type", "")
  302. params.Set("start", url.QueryEscape("2017-03-21 00:00:00"))
  303. params.Set("end", url.QueryEscape("2017-05-29 00:00:00"))
  304. params.Set("order", "desc")
  305. params.Set("sort", "rp_time")
  306. params.Set("page_size", "2")
  307. params.Set("appkey", "f6433799dbd88751")
  308. params.Set("ts", strconv.FormatInt(time.Now().Unix(), 10))
  309. mh := md5.Sum([]byte(params.Encode() + "36f8ddb1806207fe07013ab6a77a3935"))
  310. params.Set("sign", hex.EncodeToString(mh[:]))
  311. requestURL := _dmReportList + "?" + params.Encode()
  312. t.Log(requestURL)
  313. body, err := oget(requestURL)
  314. if err != nil {
  315. t.Errorf("url(%s) error(%s)", requestURL, err)
  316. t.FailNow()
  317. }
  318. if err = ccode(body); err != nil {
  319. t.Fatal(err, string(body))
  320. t.FailNow()
  321. }
  322. var out bytes.Buffer
  323. if err = json.Indent(&out, body, "", " "); err != nil {
  324. t.Fatal(err)
  325. t.FailNow()
  326. }
  327. fmt.Println(out.String())
  328. }
  329. func TestReportDetail(t *testing.T) {
  330. params := url.Values{}
  331. params.Set("dmid", "719254592")
  332. params.Set("cid", "10106598")
  333. params.Set("appkey", "f6433799dbd88751")
  334. params.Set("ts", strconv.FormatInt(time.Now().Unix(), 10))
  335. mh := md5.Sum([]byte(params.Encode() + "36f8ddb1806207fe07013ab6a77a3935"))
  336. params.Set("sign", hex.EncodeToString(mh[:]))
  337. requestURL := _dmReportdetail + "?" + params.Encode()
  338. body, err := oget(requestURL)
  339. if err != nil {
  340. t.Errorf("url(%s) error(%s)", requestURL, err)
  341. t.FailNow()
  342. }
  343. if err = ccode(body); err != nil {
  344. t.Fatal(err, string(body))
  345. t.FailNow()
  346. }
  347. var out bytes.Buffer
  348. if err = json.Indent(&out, body, "", " "); err != nil {
  349. t.Fatal(err)
  350. t.FailNow()
  351. }
  352. fmt.Println(out.String())
  353. }
  354. func TestSendJudge(t *testing.T) {
  355. params := url.Values{}
  356. params.Set("ids", "10108441:719925897")
  357. params.Set("uname", "luoxiaofan")
  358. params.Set("appkey", "f6433799dbd88751")
  359. params.Set("ts", strconv.FormatInt(time.Now().Unix(), 10))
  360. mh := md5.Sum([]byte(params.Encode() + "36f8ddb1806207fe07013ab6a77a3935"))
  361. params.Set("sign", hex.EncodeToString(mh[:]))
  362. requestURL := _dmSendJudge + "?" + params.Encode()
  363. t.Log(requestURL)
  364. fmt.Println(requestURL)
  365. body, err := opost(requestURL)
  366. if err != nil {
  367. t.Errorf("url(%s) error(%s)", requestURL, err)
  368. t.FailNow()
  369. }
  370. if err = ccode(body); err != nil {
  371. t.Fatal(err, string(body))
  372. t.FailNow()
  373. }
  374. var out bytes.Buffer
  375. if err = json.Indent(&out, body, "", " "); err != nil {
  376. t.Fatal(err)
  377. t.FailNow()
  378. }
  379. fmt.Println(out.String())
  380. }
  381. func TestJudgeResult(t *testing.T) {
  382. params := url.Values{}
  383. params.Set("cid", "1")
  384. params.Set("dmid", "2")
  385. params.Set("result", "1")
  386. params.Set("appkey", "f6433799dbd88751")
  387. params.Set("ts", strconv.FormatInt(time.Now().Unix(), 10))
  388. mh := md5.Sum([]byte(params.Encode() + "36f8ddb1806207fe07013ab6a77a3935"))
  389. params.Set("sign", hex.EncodeToString(mh[:]))
  390. requestURL := _dmJudgeResult + "?" + params.Encode()
  391. fmt.Println(requestURL)
  392. body, err := opost(requestURL)
  393. if err != nil {
  394. t.Errorf("url(%s) error(%s)", requestURL, err)
  395. t.FailNow()
  396. }
  397. if err = ccode(body); err != nil {
  398. t.Fatal(err, string(body))
  399. t.FailNow()
  400. }
  401. var out bytes.Buffer
  402. if err = json.Indent(&out, body, "", " "); err != nil {
  403. t.Fatal(err)
  404. t.FailNow()
  405. }
  406. fmt.Println(out.String())
  407. }
  408. func TestSubjectLimit(t *testing.T) {
  409. params := url.Values{}
  410. params.Set("cid", "1221")
  411. params.Set("limit", "99")
  412. params.Set("appkey", "f6433799dbd88751")
  413. params.Set("ts", strconv.FormatInt(time.Now().Unix(), 10))
  414. mh := md5.Sum([]byte(params.Encode() + "36f8ddb1806207fe07013ab6a77a3935"))
  415. params.Set("sign", hex.EncodeToString(mh[:]))
  416. requestURL := _subjectLimit + "?" + params.Encode()
  417. fmt.Println(requestURL)
  418. body, err := opost(requestURL)
  419. if err != nil {
  420. t.Errorf("url(%s) error(%s)", requestURL, err)
  421. t.FailNow()
  422. }
  423. if err = ccode(body); err != nil {
  424. t.Fatal(err, string(body))
  425. t.FailNow()
  426. }
  427. var out bytes.Buffer
  428. if err = json.Indent(&out, body, "", " "); err != nil {
  429. t.Fatal(err)
  430. t.FailNow()
  431. }
  432. fmt.Println(out.String())
  433. }
  434. func TestDMIndexInfo(t *testing.T) {
  435. params := url.Values{}
  436. params.Set("cid", "9967205")
  437. params.Set("appkey", "f6433799dbd88751")
  438. params.Set("ts", strconv.FormatInt(time.Now().Unix(), 10))
  439. mh := md5.Sum([]byte(params.Encode() + "36f8ddb1806207fe07013ab6a77a3935"))
  440. params.Set("sign", hex.EncodeToString(mh[:]))
  441. requestURL := _dmIndexInfoURL + "?" + params.Encode()
  442. fmt.Println(requestURL)
  443. body, err := oget(requestURL)
  444. if err != nil {
  445. t.Errorf("url(%s) error(%s)", requestURL, err)
  446. t.FailNow()
  447. }
  448. if err = ccode(body); err != nil {
  449. t.Fatal(err, string(body))
  450. t.FailNow()
  451. }
  452. var out bytes.Buffer
  453. if err = json.Indent(&out, body, "", " "); err != nil {
  454. t.Fatal(err)
  455. t.FailNow()
  456. }
  457. fmt.Println(out.String())
  458. }
  459. func TestAddTrJob(t *testing.T) {
  460. params := url.Values{}
  461. params.Set("cid", "9967205")
  462. params.Set("appkey", "f6433799dbd88751")
  463. params.Set("ts", strconv.FormatInt(time.Now().Unix(), 10))
  464. mh := md5.Sum([]byte(params.Encode() + "36f8ddb1806207fe07013ab6a77a3935"))
  465. params.Set("sign", hex.EncodeToString(mh[:]))
  466. }
  467. func TestMonitorList(t *testing.T) {
  468. params := url.Values{}
  469. params.Set("page", "1")
  470. params.Set("page_size", "5")
  471. params.Set("appkey", "f6433799dbd88751")
  472. params.Set("ts", strconv.FormatInt(time.Now().Unix(), 10))
  473. mh := md5.Sum([]byte(params.Encode() + "36f8ddb1806207fe07013ab6a77a3935"))
  474. params.Set("sign", hex.EncodeToString(mh[:]))
  475. requestURL := _monitorList + "?" + params.Encode()
  476. t.Log(requestURL)
  477. body, err := oget(requestURL)
  478. if err != nil {
  479. t.Errorf("url(%s) error(%s)", requestURL, err)
  480. t.FailNow()
  481. }
  482. if err = ccode(body); err != nil {
  483. t.Fatal(err, string(body))
  484. t.FailNow()
  485. }
  486. var out bytes.Buffer
  487. if err = json.Indent(&out, body, "", " "); err != nil {
  488. t.Fatal(err)
  489. t.FailNow()
  490. }
  491. fmt.Println(out.String())
  492. }
  493. func TestEditMonitor(t *testing.T) {
  494. params := url.Values{}
  495. params.Set("type", "1")
  496. params.Set("oids", "1221,1222")
  497. params.Set("state", "1")
  498. params.Set("appkey", "f6433799dbd88751")
  499. params.Set("ts", strconv.FormatInt(time.Now().Unix(), 10))
  500. mh := md5.Sum([]byte(params.Encode() + "36f8ddb1806207fe07013ab6a77a3935"))
  501. params.Set("sign", hex.EncodeToString(mh[:]))
  502. requestURL := _editMonitor + "?" + params.Encode()
  503. t.Log(requestURL)
  504. body, err := opost(requestURL)
  505. if err != nil {
  506. t.Errorf("url(%s) error(%s)", requestURL, err)
  507. t.FailNow()
  508. }
  509. if err = ccode(body); err != nil {
  510. t.Fatal(err, string(body))
  511. t.FailNow()
  512. }
  513. var out bytes.Buffer
  514. if err = json.Indent(&out, body, "", " "); err != nil {
  515. t.Fatal(err)
  516. t.FailNow()
  517. }
  518. fmt.Println(out.String())
  519. }
  520. func TestTransferList(t *testing.T) {
  521. params := url.Values{}
  522. params.Set("cid", "10108682")
  523. params.Set("state", "1")
  524. params.Set("appkey", "53e2fa226f5ad348")
  525. params.Set("ts", strconv.FormatInt(time.Now().Unix(), 10))
  526. mh := md5.Sum([]byte(params.Encode() + "3cf6bd1b0ff671021da5f424fea4b04a"))
  527. params.Set("sign", hex.EncodeToString(mh[:]))
  528. s := managerHost + "/x/admin/dm/transfer/list?" + params.Encode()
  529. fmt.Println(s)
  530. body, err := oget(s)
  531. if err != nil {
  532. t.Errorf("url(%s) error(%s)", s, err)
  533. t.FailNow()
  534. }
  535. if err = ccode(body); err != nil {
  536. t.Fatal(err, string(body))
  537. t.FailNow()
  538. }
  539. //t.Logf("===========%v", body)
  540. }
  541. func TestReTransferJob(t *testing.T) {
  542. params := url.Values{}
  543. params.Set("id", "265")
  544. params.Set("mid", "1")
  545. s := managerHost + "/x/admin/dm/transfer/retry?" + params.Encode()
  546. t.Log(s)
  547. body, err := cpost(s)
  548. if err != nil {
  549. t.Errorf("url(%s) error(%s)", s, err)
  550. t.FailNow()
  551. }
  552. if err = ccode(body); err != nil {
  553. t.Fatal(err, string(body))
  554. t.FailNow()
  555. }
  556. fmt.Println(string(body))
  557. }
  558. func TestMaskUps(t *testing.T) {
  559. body, err := oget(_maskUps)
  560. fmt.Println(string(body))
  561. if err != nil {
  562. t.Errorf("url(%s) error(%s)", _maskUps, err)
  563. t.FailNow()
  564. }
  565. var out bytes.Buffer
  566. if err = json.Indent(&out, body, "", " "); err != nil {
  567. t.Fatal(err)
  568. t.FailNow()
  569. }
  570. fmt.Println(out.String())
  571. }
  572. func TestMaskUpOpen(t *testing.T) {
  573. params := url.Values{}
  574. params.Set("mids", "11111")
  575. params.Set("state", "1")
  576. // params.Set("comment", "test")
  577. requestURL := _maskUpOpen + "?" + params.Encode()
  578. fmt.Println(requestURL)
  579. body, err := opost(requestURL)
  580. fmt.Println(string(body))
  581. if err != nil {
  582. t.Errorf("url(%s) error(%s)", _maskUps, err)
  583. t.FailNow()
  584. }
  585. var out bytes.Buffer
  586. if err = json.Indent(&out, body, "", " "); err != nil {
  587. t.Fatal(err)
  588. t.FailNow()
  589. }
  590. fmt.Println(out.String())
  591. }
  592. func TestTaskList(t *testing.T) {
  593. body, err := oget(_taskList)
  594. fmt.Println(string(body))
  595. if err != nil {
  596. t.Errorf("url(%s) error(%s)", _taskList, err)
  597. t.FailNow()
  598. }
  599. var out bytes.Buffer
  600. if err = json.Indent(&out, body, "", " "); err != nil {
  601. t.Fatal(err)
  602. t.FailNow()
  603. }
  604. fmt.Println(out.String())
  605. }
  606. func TestTaskNew(t *testing.T) {
  607. params := url.Values{}
  608. params.Set("title", "test")
  609. params.Set("mids", "1111")
  610. params.Set("start", "2018-11-11 00:00:00")
  611. params.Set("end", "2018-11-12 00:00:00")
  612. requestURL := _taskNew + "?" + params.Encode()
  613. body, err := cpost(requestURL)
  614. fmt.Println(string(body))
  615. if err != nil {
  616. t.Errorf("url(%s) error(%s)", _taskNew, err)
  617. t.FailNow()
  618. }
  619. var out bytes.Buffer
  620. if err = json.Indent(&out, body, "", " "); err != nil {
  621. t.Fatal(err)
  622. t.FailNow()
  623. }
  624. fmt.Println(out.String())
  625. }
  626. func TestTaskReview(t *testing.T) {
  627. params := url.Values{}
  628. params.Set("id", "15")
  629. params.Set("state", "1")
  630. requestURL := _taskReview + "?" + params.Encode()
  631. body, err := opost(requestURL)
  632. fmt.Println(string(body))
  633. if err != nil {
  634. t.Errorf("url(%s) error(%s)", _taskNew, err)
  635. t.FailNow()
  636. }
  637. var out bytes.Buffer
  638. if err = json.Indent(&out, body, "", " "); err != nil {
  639. t.Fatal(err)
  640. t.FailNow()
  641. }
  642. fmt.Println(out.String())
  643. }
  644. func TestTaskView(t *testing.T) {
  645. params := url.Values{}
  646. params.Set("id", "14")
  647. requestURL := _taskView + "?" + params.Encode()
  648. body, err := oget(requestURL)
  649. fmt.Println(string(body))
  650. if err != nil {
  651. t.Errorf("url(%s) error(%s)", _taskNew, err)
  652. t.FailNow()
  653. }
  654. var out bytes.Buffer
  655. if err = json.Indent(&out, body, "", " "); err != nil {
  656. t.Fatal(err)
  657. t.FailNow()
  658. }
  659. fmt.Println(out.String())
  660. }
  661. // oget http get request
  662. func oget(url string) (body []byte, err error) {
  663. resp, err := http.Get(url)
  664. if err != nil {
  665. return
  666. }
  667. defer resp.Body.Close()
  668. body, err = ioutil.ReadAll(resp.Body)
  669. return
  670. }
  671. // ccode check code == 0
  672. func ccode(body []byte) (err error) {
  673. var d interface{}
  674. err = json.Unmarshal(body, d)
  675. if err != nil {
  676. return
  677. }
  678. ma, ok := d.(map[string]interface{})
  679. if !ok {
  680. return
  681. }
  682. code := ma["code"].(int)
  683. if code != 0 {
  684. err = errors.New("code != 0")
  685. return
  686. }
  687. return
  688. }
  689. // opost http post request
  690. func opost(url string) (body []byte, err error) {
  691. resp, err := http.Post(url, "", nil)
  692. if err != nil {
  693. return
  694. }
  695. defer resp.Body.Close()
  696. body, err = ioutil.ReadAll(resp.Body)
  697. return
  698. }
  699. func cpost(url string) (body []byte, err error) {
  700. client := &http.Client{}
  701. req, err := http.NewRequest("POST", url, nil)
  702. if err != nil {
  703. return
  704. }
  705. req.Header.Add("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
  706. req.Header.Add("Accept-Language", "ja,zh-CN;q=0.8,zh;q=0.6")
  707. req.Header.Add("Connection", "keep-alive")
  708. req.Header.Add("Cookie", `uid=1130; username=fengduzhen; mng-bilibili=b4nec4bi6un0l9ftkfsgj59uq1; _AJSESSIONID=0935506167be9a62bd477a9f10bd6021; JSESSIONID=940FB2C9CAE4FD14171A5D632689C6E5; _uuid=FCC1090C-7E26-52E5-2DC6-26D8EE1B830506546infoc; mng-go=d56eabfa9edf138604586d5dee4929273d1f76a00cfcd69278b6264c453bae2e`)
  709. req.Header.Add("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:12.0) Gecko/20100101 Firefox/12.0")
  710. resp, err := client.Do(req)
  711. if err != nil {
  712. return
  713. }
  714. defer resp.Body.Close()
  715. body, err = ioutil.ReadAll(resp.Body)
  716. return
  717. }