本文整理汇总了Golang中testing.T.Execute方法的典型用法代码示例。如果您正苦于以下问题:Golang T.Execute方法的具体用法?Golang T.Execute怎么用?Golang T.Execute使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类testing.T
的用法示例。
在下文中一共展示了T.Execute方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: TestFormToken
// Test that our form helpers correctly inject a token into the response body.
func TestFormToken(t *testing.T) {
s := web.New()
s.Use(Protect(testKey))
// Make the token available outside of the handler for comparison.
var token string
s.Get("/", web.HandlerFunc(func(c web.C, w http.ResponseWriter, r *http.Request) {
token = Token(c, r)
t := template.Must((template.New("base").Parse(testTemplate)))
t.Execute(w, map[string]interface{}{
TemplateTag: TemplateField(c, r),
})
}))
r, err := http.NewRequest("GET", "/", nil)
if err != nil {
t.Fatal(err)
}
rr := httptest.NewRecorder()
s.ServeHTTP(rr, r)
if rr.Code != http.StatusOK {
t.Fatalf("middleware failed to pass to the next handler: got %v want %v",
rr.Code, http.StatusOK)
}
if len(token) != base64.StdEncoding.EncodedLen(tokenLength*2) {
t.Fatalf("token length invalid: got %v want %v", len(token), base64.StdEncoding.EncodedLen(tokenLength*2))
}
if !strings.Contains(rr.Body.String(), token) {
t.Fatalf("token not in response body: got %v want %v", rr.Body.String(), token)
}
}
示例2: TestMultipartFormToken
// Test that we can extract a CSRF token from a multipart form.
func TestMultipartFormToken(t *testing.T) {
s := web.New()
s.Use(Protect(testKey))
// Make the token available outside of the handler for comparison.
var token string
s.Handle("/", web.HandlerFunc(func(c web.C, w http.ResponseWriter, r *http.Request) {
token = Token(c, r)
t := template.Must((template.New("base").Parse(testTemplate)))
t.Execute(w, map[string]interface{}{
TemplateTag: TemplateField(c, r),
})
}))
r, err := http.NewRequest("GET", "/", nil)
if err != nil {
t.Fatal(err)
}
rr := httptest.NewRecorder()
s.ServeHTTP(rr, r)
// Set up our multipart form
var b bytes.Buffer
mp := multipart.NewWriter(&b)
wr, err := mp.CreateFormField(fieldName)
if err != nil {
t.Fatal(err)
}
wr.Write([]byte(token))
mp.Close()
r, err = http.NewRequest("POST", "/", &b)
if err != nil {
t.Fatal(err)
}
// Add the multipart header.
r.Header.Set("Content-Type", mp.FormDataContentType())
// Send back the issued cookie.
setCookie(rr, r)
rr = httptest.NewRecorder()
s.ServeHTTP(rr, r)
if rr.Code != http.StatusOK {
t.Fatalf("middleware failed to pass to the next handler: got %v want %v",
rr.Code, http.StatusOK)
}
if body := rr.Body.String(); !strings.Contains(body, token) {
t.Fatalf("token not in response body: got %v want %v", body, token)
}
}
示例3: TestTemplateField
func TestTemplateField(t *testing.T) {
s := http.NewServeMux()
// Make the token & template field available outside of the handler.
var token string
var templateField string
s.HandleFunc("/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
token = Token(r)
templateField = string(TemplateField(r))
t := template.Must((template.New("base").Parse(testTemplate)))
t.Execute(w, map[string]interface{}{
TemplateTag: TemplateField(r),
})
}))
testFieldName := "custom_field_name"
r, err := http.NewRequest("GET", "/", nil)
if err != nil {
t.Fatal(err)
}
rr := httptest.NewRecorder()
p := Protect(testKey, FieldName(testFieldName))(s)
p.ServeHTTP(rr, r)
expectedField := fmt.Sprintf(`<input type="hidden" name="%s" value="%s">`,
testFieldName, token)
if rr.Code != http.StatusOK {
t.Fatalf("middleware failed to pass to the next handler: got %v want %v",
rr.Code, http.StatusOK)
}
if templateField != expectedField {
t.Fatalf("custom FieldName was not set correctly: got %v want %v",
templateField, expectedField)
}
}
示例4: TestTheBardSearch
//.........这里部分代码省略.........
timeoutStr := os.Getenv("SEARCHER_WEB_TIMEOUT_SECONDS")
timeout, err := strconv.Atoi(timeoutStr)
if err != nil {
timeout = 10
}
zfpath := "testdata/shakespeare.mit.edu.zip"
// wait for specified time
go func() { time.Sleep(time.Duration(timeout) * time.Second); ln.Close() }()
// main request handler
err = http.Serve(ln, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// home page redirect
if r.URL.Path == "/" || r.URL.Path == "/Shakespeare" {
http.Redirect(w, r, "/shakespeare.mit.edu/index.html", 302)
return
}
// handle search result page
if r.URL.Path == "/searchresults.html" {
w.Header().Set("Content-type", "text/html")
q := r.FormValue("q")
// do search
sr, err := s.SimpleSearch(q, 20)
if err != nil {
panic(err)
}
// render results page
sres, err := ioutil.ReadFile("testdata/searchresults.html")
if err != nil {
panic(err)
}
t := template.Must(template.New("main").Parse(string(sres)))
var buf bytes.Buffer
t.Execute(&buf, &map[string]interface{}{
"q": q,
"sr": sr,
})
sresbytes := buf.Bytes()
w.Write(sresbytes)
return
}
// by default look through zip file
b, err := zipExtract(zfpath, r.URL.Path)
if err != nil {
http.Error(w, "File not found", 404)
}
if strings.HasSuffix(r.URL.Path, ".css") {
w.Header().Set("Content-type", "text/css")
}
if strings.HasSuffix(r.URL.Path, ".gif") {
w.Header().Set("Content-type", "image/gif")
}
if strings.HasSuffix(r.URL.Path, ".jpg") {
w.Header().Set("Content-type", "image/jpeg")
}
// for html files we inject a search box
if strings.HasSuffix(r.URL.Path, ".html") {
w.Header().Set("Content-type", "text/html")
// render search form
sf, err := ioutil.ReadFile("testdata/searchform.html")
if err != nil {
panic(err)
}
t := template.Must(template.New("main").Parse(string(sf)))
var buf bytes.Buffer
t.Execute(&buf, r.FormValue("q"))
sfbytes := buf.Bytes()
// inject into page
pagebytes := re.MustCompile("(<body[^>]*>)").ReplaceAllLiteral(b, []byte("<body bgcolor=\"#ffffff\" text=\"#000000\">"+string(sfbytes)))
w.Write(pagebytes)
return
}
w.Write(b)
}))
if err != nil {
fmt.Printf("err from listen: %s\n", err)
}
s.Close()
}