2015-06-20 113 views

回答

2

马提尼-的contrib库有很多现有的代码值得看:https://github.com/martini-contrib/secure/blob/master/secure_test.go

例如

func Test_No_Config(t *testing.T) { 
    m := martini.Classic() 
    m.Use(Secure(Options{ 
    // nothing here to configure 
    })) 

    m.Get("/foo", func() string { 
     return "bar" 
    }) 

    res := httptest.NewRecorder() 
    req, _ := http.NewRequest("GET", "/foo", nil) 

    m.ServeHTTP(res, req) 

    expect(t, res.Code, http.StatusOK) 
    expect(t, res.Body.String(), `bar`) 
} 

综上所述:

  1. 创建martini.Classic()
  2. 服务器创建到要测试
  3. 执行它针对的响应记录
  4. 检查结果的处理路线(状态码,正文)与预期一致。