func testSmtp(t *testing.T) { smtp := NewSmtp("*****@*****.**", "pwd", "test", "smtp.qq.com:25", []string{"*****@*****.**"}) size, err := smtp.Write([]byte("test")) assert.NotError(t, err) assert.True(t, size > 0) time.Sleep(30 * time.Second) size, err = smtp.Write([]byte("test2")) assert.NotError(t, err) assert.True(t, size > 0) }
func TestHost(t *testing.T) { defHandler := func(w http.ResponseWriter, r *http.Request) bool { return true } defFunc := MatcherFunc(defHandler) fn := func(host string, hh *Host, wont bool) { r, err := http.NewRequest("GET", "", nil) assert.NotError(t, err) r.Host = host assert.Equal(t, r.Host, host) assert.Equal(t, hh.ServeHTTP2(nil, r), wont, "域名[%v]无法正确匹配", host) } h := NewHost(defFunc, "www.example.com") fn("www.example.com", h, true) fn("www.abc.com", h, false) h = NewHost(defFunc, "\\w+.example.com") fn("www.example.com", h, true) fn("api.example.com", h, true) fn("www.abc.com", h, false) }
func TestMethod(t *testing.T) { defHandler := func(w http.ResponseWriter, r *http.Request) bool { return true } defFunc := MatcherFunc(defHandler) fn := func(method string, m Matcher, wont bool) { r, err := http.NewRequest(method, "", nil) assert.NotError(t, err) assert.Equal(t, m.ServeHTTP2(nil, r), wont) } m := NewMethod().Get(defFunc) fn("GET", m, true) fn("POST", m, false) m = NewMethod().Get(defFunc).Post(defFunc) fn("POST", m, true) fn("GET", m, true) fn("OPTIONS", m, false) }
func TestPath(t *testing.T) { defHandler := func(w http.ResponseWriter, r *http.Request) bool { return true } defFunc := MatcherFunc(defHandler) fn := func(pattern string, p *Path, wont bool) { r, err := http.NewRequest("GET", pattern, nil) assert.NotError(t, err) assert.Equal(t, p.ServeHTTP2(nil, r), wont) } p := NewPath(defFunc, "/api") fn("/api", p, true) fn("/api/v1", p, true) p = NewPath(defFunc, "/api/v(\\d+)") fn("/api", p, false) fn("/api/v1", p, true) fn("/api/v1/post/1", p, true) }