main_test.go 4.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. package main
  2. import (
  3. "fmt"
  4. "github.com/gomodule/redigo/redis"
  5. "github.com/rafaeljusto/redigomock"
  6. "github.com/stretchr/testify/assert"
  7. "miads/adslib"
  8. "miads/adslib/ads_redis"
  9. "net/http"
  10. "net/http/httptest"
  11. "strings"
  12. "testing"
  13. "time"
  14. )
  15. // func TestAds(t *testing.T) {
  16. // router := setupRouter()
  17. //
  18. // w := httptest.NewRecorder()
  19. // req, _ := http.NewRequest("POST", "/ads?req_source=mh&network_type=9&imei=867252039444772&idfa=&ip=182.37.81.64&ua=Mozilla%2F5.0+%28Linux%3B+Android+8.1.0%3B+MI+8+Build%2FOPM1.171019.026%3B+wv%29+AppleWebKit%2F537.36+%28KHTML%2C+like+Gecko%29+Version%2F4.0+Chrome%2F68.0.3440.91+Mobile+Safari%2F537.36&model=MI+8&brand=Xiaomi&android_id=867252039444772&platform=1", nil)
  20. // router.ServeHTTP(w, req)
  21. //
  22. // assert.Equal(t, 200, w.Code)
  23. // assert.Equal(t, "pong", w.Body.String())
  24. // }
  25. func TestDecrypt(t *testing.T) {
  26. router := setupRouter()
  27. w := httptest.NewRecorder()
  28. r := strings.NewReader("ads_content=a827e6443149663e6d934392fffe24211c7895c5de901c79d0728f903c3aba6943df34363ebfe388dbb70bb824bd10dbed8504e66ed20070d7d5865ee17bd3caf7161039a7b0db08995ff9f9adcbfcd71b688a3c63d5f2ba88b737c4d1c5b016a5328a7df15b494d0c8afd34fd2798226ed9e1c8b2c6203b8ec3440f9df573a5fb093d4f5bf4130e99c9c55fe6cee5fbec2eeaf1d8315919ed1ff0d0490f4ff88d87d6a8094bfa18aa35f0d7349a633e1ea65297e9817b81cf35628fb4695e78bcd58eeb4ed645e2f7188c662727a98d0b587378bc973c92f8c566ea644e0cfe66e41ba11b0dfe8075b5ff8ce82f9e2d81ee7d99eefe9a899c790f8fe21eb30bca124429ae41a9dc95166e2a84763634de63ba01e53ce0f0788112558d16c08c40d324b910284f633d66d55cbea2dcbdc16f16c8f08f621d0a1b665550d3fb282b5c0b9bc54bd1c3b8a7f900a92acc560c452af6b8f9e13f1fd4fbcdc42e2ca110b471d708e63fda33f306c95acb96b15ea75fafbbeba979ddf7ccf91f777ed5bb810f493338ed4436c3ccebcb993b4cd8dd46610f29ec8d9ba6390f0a4f2821&new_ads_flag=1")
  29. req, _ := http.NewRequest("POST", "/ads", r)
  30. router.ServeHTTP(w, req)
  31. assert.Equal(t, 200, w.Code)
  32. assert.Equal(t, "pong", w.Body.String())
  33. }
  34. func TestAdsHandle(t *testing.T) {
  35. err := ads_redis.Setup()
  36. assert.Nil(t, err)
  37. conn := redigomock.NewConn()
  38. ads_redis.RedisConn = &redis.Pool{
  39. // Return the same connection mock for each Get() call.
  40. Dial: func() (redis.Conn, error) { return conn, nil },
  41. MaxIdle: 10,
  42. }
  43. conn.Command("SISMEMBER", "arsi", "zhiku").Expect([]byte("true"))
  44. conn.Command("GET", "acfv2_{kuxin}_{ads_click}").Expect("{\"channel_flag\": 1, \"weigth\": 100}")
  45. conn.Command("GET", "acfv2_{kuxin02}_{ads_click}").Expect("{\"channel_flag\": 1, \"weigth\": 100}")
  46. conn.Command("GET", "adsv2_xiaomi_0").Expect(int64(784))
  47. conn.Command("GET", "adsv3_xiaomi_VIEW_0").Expect(int64(0))
  48. conn.Command("GET", "adsv3_xiaomi_VIDEO_TIMER_0").Expect(int64(215))
  49. conn.Command("GET", "adsv3_xiaomi_CLICK_0").Expect(int64(24))
  50. conn.Command("GET", "adsv3_xiaomi_VIDEO_FINISH_0").Expect(int64(760))
  51. conn.Command("GET", "dim2_72710e3df592f07f6dbcc04b747f3653_'223.91.87.241'").Expect(int64(time.Now().Unix()))
  52. if err != nil {
  53. fmt.Printf("setup redis failed: %s\n", err)
  54. return
  55. }
  56. adslib.GetConf()
  57. router := setupRouter()
  58. w := httptest.NewRecorder()
  59. r := strings.NewReader("ads_content=dd284ac406027f9aa8b2bf019a89b608874c7c65025eb47c936e43720b0a71b599a7d33b8a6580dc6b096ca31fdc9534e7335d65aa3793a03674945e25854887ecd72bda77a03da07c5e8659e93423ad5f47434a698789399f5a74c90855cc44e382bf8a91fac765386ca04afeee82f1f9f844018f3f77aaa4dde1aeb38b218d8a4402749fdb0189e2e9e0e25ee5deff5d7f19bcae75d86deaf1c603f793a48c0f9f7219a2872285c60ba415eca5b975c6781fed550a49ff22ce8b360842f38f6e5a7e6cb8dbb80d7c550017ced3cfe563c5744c666f6fcf6bbe54476215cd634729d46eab1f34cea63883cc211d42d4d54c37e3579966fcfb1926a16f3d7ed62c49fbdbd88693fa980d35cee622498e1763979b542d244e15356b4c5c565d40d46bb443a837052dd98be510184b149d0df9cc56fa2867bfe882768dc058eee740a608083a47c21e69bc69ac2efd086351b0043ea884e68a4a6c51c1356dc22941ac6c1164af82b68d1775bfa361d7726e1a2f1ee8f6930351506261996972a1f125510fe780705e1e26aed1b688af2053f690d07518f97ce1dba2380daf6123157a16677751fc93f0db9df8f11906729093c5e441004452aa66a355da28c3e2&new_ads_flag=1")
  60. req, _ := http.NewRequest("POST", "/ads", r)
  61. router.ServeHTTP(w, req)
  62. assert.Equal(t, 200, w.Code)
  63. assert.Equal(t, "pong", w.Body.String())
  64. }