main_test.go 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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("")
  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("SISMEMBER", "arsi", "kuxin").Expect([]byte("true"))
  45. conn.Command("SISMEMBER", "ads_black_ip", "183.210.200.149").Expect([]byte("false"))
  46. conn.Command("GET", "ads_city_常州市").Expect("1156320400")
  47. conn.Command("GET", "acfv2_{kuxin}_{ads_click}").Expect("{\"channel_flag\": 1, \"weigth\": 100}")
  48. conn.Command("GET", "acfv2_{kuxin02}_{ads_click}").Expect("{\"channel_flag\": 1, \"weigth\": 100}")
  49. conn.Command("GET", "adsv2_xiaomi_0").Expect(int64(784))
  50. conn.Command("GET", "adsv3_xiaomi_VIEW_0").Expect(int64(0))
  51. conn.Command("GET", "adsv3_xiaomi_VIDEO_TIMER_0").Expect(int64(215))
  52. conn.Command("GET", "adsv3_xiaomi_CLICK_0").Expect(int64(24))
  53. conn.Command("GET", "adsv3_xiaomi_VIDEO_FINISH_0").Expect(int64(760))
  54. conn.Command("GET", "dim2_72710e3df592f07f6dbcc04b747f3653_'223.91.87.241'").Expect(int64(time.Now().Unix()))
  55. conn.Command("GET", "rsfreq_kuxin").Expect("[{\"freq_time\": 60, \"end_hour\": 23, \"begin_hour\": 0}]")
  56. conn.Command("GET", "adip_183210200149").Expect("1592485922")
  57. if err != nil {
  58. fmt.Printf("setup redis failed: %s\n", err)
  59. return
  60. }
  61. adslib.GetConf()
  62. router := setupRouter()
  63. w := httptest.NewRecorder()
  64. r := strings.NewReader("imei=868900043181108&mac=54:BA:D6:A5:EA:A1&os_version=9&ip=183.210.200.149&ua=Mozilla%2F5.0+%28Linux%3B+Android+9%3B+JKM-AL00+Build%2FHUAWEIJKM-AL00%3B+wv%29+AppleWebKit%2F537.36+%28KHTML%2C+like+Gecko%29+Version%2F4.0+Chrome%2F74.0.3729.136+Mobile+Safari%2F537.36&model=JKM-AL00&brand=HUAWEI&android_id=cf2447a8bd2e59f1&platform=1&req_source=kuxin&network_type=9&screen_size=2137x1080")
  65. req, _ := http.NewRequest("POST", "/ads", r)
  66. router.ServeHTTP(w, req)
  67. //assert.Equal(t, 200, w.Code)
  68. assert.Equal(t, "pong", w.Body.String())
  69. }