-
Notifications
You must be signed in to change notification settings - Fork 1
/
main_test.go
125 lines (108 loc) · 4.11 KB
/
main_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package main
import (
"testing"
"github.com/Andrewpqc/muxi-promotion-service/redis-client"
"github.com/kataras/iris/httptest"
//"encoding/json"
//"strings"
//"time"
)
func init() {
redis_client.RedisClient.Del("xueer_promotion")
}
type Token struct{
Token string
}
//func fromPrivateLink2QueryArgs(tk string)string{
//
// queryStr:=tk[33:]
// a:=strings.Split(string(queryStr),"=")
//
// return a[1]
//}
func TestBasicAuth(t *testing.T) {
e := httptest.New(t, newApp())
//测试不受Basic Auth保护的路由
e.GET("/").Expect().Status(httptest.StatusOK)
// without basic
e.GET("/api/v1.0/statistic/").Expect().Status(httptest.StatusUnauthorized)
e.GET("/api/v1.0/statistic/").WithBasicAuth("andrewpqc", "andrewpqc").Expect().
Status(httptest.StatusOK)
//// with valid basic auth
//e.GET("/admin").WithBasicAuth("myusername", "mypassword").Expect().
// Status(httptest.StatusOK).Body().Equal("/admin myusername:mypassword")
//e.GET("/admin/profile").WithBasicAuth("myusername", "mypassword").Expect().
// Status(httptest.StatusOK).Body().Equal("/admin/profile myusername:mypassword")
//e.GET("/admin/settings").WithBasicAuth("myusername", "mypassword").Expect().
// Status(httptest.StatusOK).Body().Equal("/admin/settings myusername:mypassword")
//
//// with invalid basic auth
//e.GET("/admin/settings").WithBasicAuth("invalidusername", "invalidpassword").
// Expect().Status(httptest.StatusUnauthorized)
redis_client.RedisClient.Del("xueer_promotion")
}
func TestGetPrivatePromotionLin(t *testing.T) {
e := httptest.New(t, newApp())
e.GET("/api/v1.0/private-promotion-link/").
WithQuery("id", "1").WithQuery("url", "www.baidu.com").
WithBasicAuth("andrewpqc", "andrewpqc").Expect().Status(httptest.StatusOK)
e.GET("/api/v1.0/private-promotion-link/").
WithQuery("id", "1").WithQuery("url", "www.baidu.com").WithQuery("ex", "1000").
WithBasicAuth("andrewpqc", "andrewpqc").Expect().Status(httptest.StatusOK)
e.GET("/api/v1.0/private-promotion-link/").
WithQuery("id", "1").WithQuery("ex", "1000").
WithBasicAuth("andrewpqc", "andrewpqc").Expect().Status(httptest.StatusBadRequest)
redis_client.RedisClient.Del("xueer_promotion")
}
//
//func TestProcessRequest(t *testing.T) {
// e := httptest.New(t, newApp())
// a:=e.GET("/api/v1.0/private-promotion-link/").
// WithQuery("id", "1").WithQuery("url", "www.baidu.com").
// WithBasicAuth("andrewpqc", "andrewpqc").Expect().Status(httptest.StatusOK).Body().Raw()
//
// var tk1 Token
// json.Unmarshal([]byte(a),&tk1)
// aa:=fromPrivateLink2QueryArgs(tk1.Token)
// e.GET("/promotion/").WithQuery("t",aa).
// Expect().Status(httptest.StatusPermanentRedirect)
//
//
//
// b:=e.GET("/api/v1.0/private-promotion-link/").
// WithQuery("id", "1").WithQuery("url", "www.baidu.com").WithQuery("ex","5").
// WithBasicAuth("andrewpqc", "andrewpqc").Expect().Status(httptest.StatusOK).Body().Raw()
//
// var tk2 Token
// json.Unmarshal([]byte(b),&tk2)
// aa=fromPrivateLink2QueryArgs(tk2.Token)
// e.GET("/promotion/").WithQuery("t",aa).
// Expect().Status(httptest.StatusPermanentRedirect)
// time.Sleep(6*time.Second)
// e.GET("/promotion/").WithQuery("t",aa).
// Expect().Status(httptest.StatusForbidden)
//
// redis_client.RedisClient.Del("xueer_promotion")
//}
func TestOtherUnimportant(t *testing.T){
e := httptest.New(t, newApp())
e.GET("/api/v1.0/statistic/").WithBasicAuth("andrewpqc","andrewpqc").
Expect().Status(httptest.StatusOK)
e.GET("/api/v1.0/clean/").WithBasicAuth("andrewpqc","andrewpqc").
Expect().Status(httptest.StatusOK)
redis_client.MyZadd("A")
redis_client.MyZadd("1")
redis_client.MyZadd("B")
redis_client.MyZadd("C")
redis_client.MyZadd("A")
redis_client.MyZadd("D")
redis_client.MyZadd("2")
redis_client.MyZadd("A")
e.GET("/api/v1.0/top/{n}/").WithPath("n",2).
WithBasicAuth("andrewpqc","andrewpqc").Expect().Status(httptest.StatusOK)
e.GET("/api/v1.0/rank/{id}").WithPath("id","1").
WithBasicAuth("andrewpqc","andrewpqc").Expect().Status(httptest.StatusOK)
e.GET("/api/v1.0/range/").WithQuery("start",1).WithQuery("end","3").
WithBasicAuth("andrewpqc","andrewpqc").Expect().Status(httptest.StatusOK)
redis_client.RedisClient.Del("xueer_promotion")
}