http_test.go 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. package http
  2. import (
  3. "crypto/md5"
  4. "encoding/hex"
  5. "encoding/json"
  6. "errors"
  7. "fmt"
  8. "io/ioutil"
  9. "net/http"
  10. "net/url"
  11. "strconv"
  12. "strings"
  13. "testing"
  14. "time"
  15. "go-common/app/interface/main/dm2/model"
  16. )
  17. const (
  18. _host = "http://127.0.0.1:6701"
  19. _host2 = "http://api.bilibili.com"
  20. _host3 = "http://uat-api.bilibili.co"
  21. )
  22. var (
  23. cookie = `sid=88dol9eo; fts=1508407962; LIVE_BUVID=AUTO4115084079913953; UM_distinctid=15f3436b684ab-08804fe751374a-31657c00-1fa400-15f3436b685298; pgv_pvi=3618563072; rpdid=kmilkmximpdoswqploqxw; biliMzIsnew=1; biliMzTs=null; buvid3=CBB97852-6CF6-4D3A-B97F-5A9AD1D5827F26561infoc; finger=14bc3c4e; pgv_si=s6534896640; purl_token=bilibili_1511162275; DedeUserID=3078992; DedeUserID__ckMd5=55845496fd6119b5; SESSDATA=7bf20cf0%2C1513762568%2C258e3d17; bili_jct=160db0def61324719de104b7bfe1e3ef; _cnt_pm=0; _cnt_notify=0; _dfcaptcha=b5463dc96d22c22164a8c0e5a1ba9e96; member_v2=1`
  24. dmLikeActURL = _host2 + "/x/v2/dm/thumbup/add"
  25. dmLikeListURL = _host2 + "/x/v2/dm/thumbup/stats"
  26. dmEditURL = _host + "/x/internal/v2/dm/edit/state"
  27. dmPoolURL = _host + "/x/internal/v2/dm/edit/pool"
  28. dmUpSearchURL = _host3 + "/x/v2/dm/search"
  29. dmRecentURL = _host + "/x/internal/v2/dm/recent"
  30. updateMaskURL = _host3 + "/x/internal/v2/dm/mask/update"
  31. editUpFiltersURL = _host + "/x/v2/dm/filter/up/edit"
  32. )
  33. func TestEditUpFilter(t *testing.T) {
  34. fliters := model.UpFilter{
  35. Filter: "zzzzzz",
  36. Comment: "",
  37. }
  38. p := url.Values{}
  39. p.Set("active", "1")
  40. p.Set("type", "0")
  41. filter, err := json.Marshal(fliters)
  42. if err != nil {
  43. return
  44. }
  45. fmt.Print(string(filter))
  46. p.Set("filters", string(filter))
  47. mh := md5.Sum([]byte(p.Encode() + "3cf6bd1b0ff671021da5f424fea4b04a"))
  48. p.Set("sign", hex.EncodeToString(mh[:]))
  49. reqURL := editUpFiltersURL + "?" + p.Encode()
  50. fmt.Println(reqURL)
  51. body, err := opost(reqURL)
  52. if err != nil {
  53. t.Errorf("url(%s) error(%s)", reqURL, err)
  54. t.FailNow()
  55. }
  56. if err = ccode(body); err != nil {
  57. t.Fatal(err, string(body))
  58. t.FailNow()
  59. }
  60. fmt.Println(string(body))
  61. }
  62. func TestRecent(t *testing.T) {
  63. params := url.Values{}
  64. params.Set("uid", "27515256")
  65. params.Set("page", "5")
  66. //params.Set("mid", "27515313")
  67. params.Set("order", "ctime")
  68. params.Set("size", "1")
  69. params.Set("appkey", "53e2fa226f5ad348")
  70. params.Set("ts", strconv.FormatInt(time.Now().Unix(), 10))
  71. mh := md5.Sum([]byte(params.Encode() + "3cf6bd1b0ff671021da5f424fea4b04a"))
  72. params.Set("sign", hex.EncodeToString(mh[:]))
  73. s := dmRecentURL + "?" + params.Encode()
  74. body, err := oget(s)
  75. if err != nil {
  76. t.Errorf("url(%s) error(%s)", s, err)
  77. t.FailNow()
  78. }
  79. if err = ccode(body); err != nil {
  80. t.Fatal(err, string(body))
  81. t.FailNow()
  82. }
  83. fmt.Println(string(body))
  84. }
  85. func TestDMEdit(t *testing.T) {
  86. params := url.Values{}
  87. params.Set("oid", "5")
  88. params.Set("type", "1")
  89. params.Set("mid", "27515313")
  90. params.Set("dmids", "719149463")
  91. params.Set("state", "0")
  92. params.Set("appkey", "53e2fa226f5ad348")
  93. params.Set("ts", strconv.FormatInt(time.Now().Unix(), 10))
  94. mh := md5.Sum([]byte(params.Encode() + "3cf6bd1b0ff671021da5f424fea4b04a"))
  95. params.Set("sign", hex.EncodeToString(mh[:]))
  96. s := dmEditURL + "?" + params.Encode()
  97. body, err := opost(s)
  98. if err != nil {
  99. t.Errorf("url(%s) error(%s)", s, err)
  100. t.FailNow()
  101. }
  102. if err = ccode(body); err != nil {
  103. t.Fatal(err, string(body))
  104. t.FailNow()
  105. }
  106. fmt.Println(string(body))
  107. }
  108. func TestDMPool(t *testing.T) {
  109. params := url.Values{}
  110. params.Set("cid", "5")
  111. params.Set("mid", "27515313")
  112. params.Set("dmids", "719149463")
  113. params.Set("pool", "2")
  114. params.Set("appkey", "53e2fa226f5ad348")
  115. params.Set("ts", strconv.FormatInt(time.Now().Unix(), 10))
  116. mh := md5.Sum([]byte(params.Encode() + "3cf6bd1b0ff671021da5f424fea4b04a"))
  117. params.Set("sign", hex.EncodeToString(mh[:]))
  118. s := dmPoolURL + "?" + params.Encode()
  119. body, err := opost(s)
  120. if err != nil {
  121. t.Errorf("url(%s) error(%s)", s, err)
  122. t.FailNow()
  123. }
  124. if err = ccode(body); err != nil {
  125. t.Fatal(err, string(body))
  126. t.FailNow()
  127. }
  128. fmt.Println(string(body))
  129. }
  130. func TestLikeDM(t *testing.T) {
  131. params := url.Values{}
  132. params.Set("oid", "5")
  133. params.Set("dmid", "719149462")
  134. params.Set("op", "1")
  135. reqURL := dmLikeActURL + "?" + params.Encode()
  136. body, err := cpost(reqURL)
  137. if err != nil {
  138. t.Errorf("url(%s) error(%s)", reqURL, err)
  139. t.FailNow()
  140. }
  141. if err = ccode(body); err != nil {
  142. t.Fatal(err, string(body))
  143. t.FailNow()
  144. }
  145. fmt.Println(string(body))
  146. }
  147. func TestUpdateMask(t *testing.T) {
  148. params := url.Values{}
  149. params.Set("cid", "10109227")
  150. params.Set("time", "30")
  151. params.Set("fps", "20")
  152. params.Set("plat", "1")
  153. params.Set("count", "20")
  154. params.Set("list", "26777486_s0_0_1499")
  155. params.Set("appkey", "8f62754d8d594e90")
  156. params.Set("ts", strconv.FormatInt(1527564095, 10))
  157. data := params.Encode()
  158. if strings.IndexByte(data, '+') > -1 {
  159. data = strings.Replace(data, "+", "%20", -1)
  160. }
  161. mh := md5.Sum([]byte(data + "test"))
  162. params.Set("sign", hex.EncodeToString(mh[:]))
  163. s := updateMaskURL + "?" + params.Encode()
  164. body, err := opost(s)
  165. if err != nil {
  166. t.Errorf("url(%s) error(%s)", s, err)
  167. t.FailNow()
  168. }
  169. if err = ccode(body); err != nil {
  170. t.Fatal(err, string(body))
  171. t.FailNow()
  172. }
  173. fmt.Println(string(body))
  174. }
  175. func TestLikeList(t *testing.T) {
  176. params := url.Values{}
  177. params.Set("oid", "27139273")
  178. params.Set("ids", "719149462,719149463")
  179. reqURL := dmLikeListURL + "?" + params.Encode()
  180. fmt.Println(reqURL)
  181. body, err := cget(reqURL)
  182. if err != nil {
  183. t.Errorf("url(%s) error(%s)", reqURL, err)
  184. t.FailNow()
  185. }
  186. if err = ccode(body); err != nil {
  187. t.Fatal(err, string(body))
  188. t.FailNow()
  189. }
  190. fmt.Println(string(body))
  191. }
  192. func TestDMUpSearch(t *testing.T) {
  193. params := url.Values{}
  194. params.Set("oid", "10131156")
  195. params.Set("type", "1")
  196. params.Set("appkey", "53e2fa226f5ad348")
  197. params.Set("ts", strconv.FormatInt(time.Now().Unix(), 10))
  198. mh := md5.Sum([]byte(params.Encode() + "3cf6bd1b0ff671021da5f424fea4b04a"))
  199. params.Set("sign", hex.EncodeToString(mh[:]))
  200. reqURL := dmUpSearchURL + "?" + params.Encode()
  201. fmt.Println(reqURL)
  202. body, err := oget(reqURL)
  203. if err != nil {
  204. t.Errorf("url(%s) error(%s)", reqURL, err)
  205. t.FailNow()
  206. }
  207. if err = ccode(body); err != nil {
  208. t.Fatal(err, string(body))
  209. t.FailNow()
  210. }
  211. t.Logf("======%+v", body)
  212. fmt.Println(string(body))
  213. }
  214. // oget http get request
  215. func oget(url string) (body []byte, err error) {
  216. resp, err := http.Get(url)
  217. if err != nil {
  218. return
  219. }
  220. defer resp.Body.Close()
  221. body, err = ioutil.ReadAll(resp.Body)
  222. return
  223. }
  224. // ccode check code == 0
  225. func ccode(body []byte) (err error) {
  226. var d interface{}
  227. err = json.Unmarshal(body, d)
  228. if err != nil {
  229. return
  230. }
  231. ma, ok := d.(map[string]interface{})
  232. if !ok {
  233. return
  234. }
  235. code := ma["code"].(int)
  236. if code != 0 {
  237. err = errors.New("code != 0")
  238. return
  239. }
  240. return
  241. }
  242. // opost http post request
  243. func opost(url string) (body []byte, err error) {
  244. resp, err := http.Post(url, "", nil)
  245. if err != nil {
  246. return
  247. }
  248. defer resp.Body.Close()
  249. body, err = ioutil.ReadAll(resp.Body)
  250. return
  251. }
  252. func cpost(url string) (body []byte, err error) {
  253. client := &http.Client{}
  254. req, err := http.NewRequest("POST", url, nil)
  255. if err != nil {
  256. return
  257. }
  258. req.Header.Add("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
  259. req.Header.Add("Accept-Language", "ja,zh-CN;q=0.8,zh;q=0.6")
  260. req.Header.Add("Connection", "keep-alive")
  261. req.Header.Add("Cookie", cookie)
  262. req.Header.Add("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:12.0) Gecko/20100101 Firefox/12.0")
  263. resp, err := client.Do(req)
  264. if err != nil {
  265. return
  266. }
  267. defer resp.Body.Close()
  268. body, err = ioutil.ReadAll(resp.Body)
  269. return
  270. }
  271. func cget(url string) (body []byte, err error) {
  272. client := &http.Client{}
  273. req, err := http.NewRequest("GET", url, nil)
  274. if err != nil {
  275. return
  276. }
  277. req.Header.Add("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
  278. req.Header.Add("Accept-Language", "ja,zh-CN;q=0.8,zh;q=0.6")
  279. req.Header.Add("Connection", "keep-alive")
  280. req.Header.Add("Cookie", cookie)
  281. req.Header.Add("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:12.0) Gecko/20100101 Firefox/12.0")
  282. resp, err := client.Do(req)
  283. if err != nil {
  284. return
  285. }
  286. defer resp.Body.Close()
  287. body, err = ioutil.ReadAll(resp.Body)
  288. return
  289. }