本文整理匯總了Golang中github.com/flynn/flynn/Godeps/_workspace/src/github.com/go-martini/martini.Classic函數的典型用法代碼示例。如果您正苦於以下問題:Golang Classic函數的具體用法?Golang Classic怎麽用?Golang Classic使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了Classic函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: testForm
func testForm(t *testing.T, withInterface bool) {
for index, test := range formTests {
recorder := httptest.NewRecorder()
handler := func(post BlogPost, errors Errors) { handle(test, t, index, post, errors) }
binding := Form(BlogPost{})
if withInterface {
handler = func(post BlogPost, errors Errors) {
post.Create(test, t, index)
}
binding = Form(BlogPost{}, (*Modeler)(nil))
}
m := martini.Classic()
switch test.method {
case "GET":
m.Get(route, binding, handler)
case "POST":
m.Post(route, binding, handler)
}
req, err := http.NewRequest(test.method, test.path, nil)
if err != nil {
t.Error(err)
}
m.ServeHTTP(recorder, req)
}
}
示例2: testMultipart
func testMultipart(t *testing.T, test testCase, middleware martini.Handler, handler martini.Handler, index int) *httptest.ResponseRecorder {
recorder := httptest.NewRecorder()
m := martini.Classic()
m.Post(route, middleware, handler)
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
writer.WriteField("title", test.ref.Title)
writer.WriteField("content", test.ref.Content)
writer.WriteField("views", strconv.Itoa(test.ref.Views))
if len(test.ref.Multiple) != 0 {
for _, value := range test.ref.Multiple {
writer.WriteField("multiple", strconv.Itoa(value))
}
}
req, err := http.NewRequest(test.method, test.path, body)
req.Header.Add("Content-Type", writer.FormDataContentType())
if err != nil {
t.Error(err)
}
err = writer.Close()
if err != nil {
t.Error(err)
}
m.ServeHTTP(recorder, req)
return recorder
}
示例3: testEmptyJson
func testEmptyJson(t *testing.T) {
for index, test := range emptyPayloadTests {
recorder := httptest.NewRecorder()
handler := func(section BlogSection, errors Errors) { handleEmpty(test, t, index, section, errors) }
binding := Json(BlogSection{})
m := martini.Classic()
switch test.method {
case "GET":
m.Get(route, binding, handler)
case "POST":
m.Post(route, binding, handler)
case "PUT":
m.Put(route, binding, handler)
case "DELETE":
m.Delete(route, binding, handler)
}
req, err := http.NewRequest(test.method, route, strings.NewReader(test.payload))
if err != nil {
t.Error(err)
}
m.ServeHTTP(recorder, req)
}
}
示例4: testJson
func testJson(t *testing.T, withInterface bool) {
for index, test := range jsonTests {
recorder := httptest.NewRecorder()
handler := func(post BlogPost, errors Errors) { handle(test, t, index, post, errors) }
binding := Json(BlogPost{})
if withInterface {
handler = func(post BlogPost, errors Errors) {
post.Create(test, t, index)
}
binding = Bind(BlogPost{}, (*Modeler)(nil))
}
m := martini.Classic()
switch test.method {
case "GET":
m.Get(route, binding, handler)
case "POST":
m.Post(route, binding, handler)
case "PUT":
m.Put(route, binding, handler)
case "DELETE":
m.Delete(route, binding, handler)
}
req, err := http.NewRequest(test.method, route, strings.NewReader(test.payload))
if err != nil {
t.Error(err)
}
m.ServeHTTP(recorder, req)
}
}
示例5: Test_Render_NoRace
func Test_Render_NoRace(t *testing.T) {
// This test used to fail if run with -race
m := martini.Classic()
m.Use(Renderer(Options{
Directory: "fixtures/basic",
}))
// routing
m.Get("/foobar", func(r Render) {
r.HTML(200, "hello", "world")
})
done := make(chan bool)
doreq := func() {
res := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/foobar", nil)
m.ServeHTTP(res, req)
expect(t, res.Code, 200)
expect(t, res.Header().Get(ContentType), ContentHTML+"; charset=UTF-8")
// ContentLength should be deferred to the ResponseWriter and not Render
expect(t, res.Header().Get(ContentLength), "")
expect(t, res.Body.String(), "<h1>Hello world</h1>\n")
done <- true
}
// Run two requests to check there is no race condition
go doreq()
go doreq()
<-done
<-done
}
示例6: Test_Render_Funcs
func Test_Render_Funcs(t *testing.T) {
m := martini.Classic()
m.Use(Renderer(Options{
Directory: "fixtures/custom_funcs",
Funcs: []template.FuncMap{
{
"myCustomFunc": func() string {
return "My custom function"
},
},
},
}))
// routing
m.Get("/foobar", func(r Render) {
r.HTML(200, "index", "jeremy")
})
res := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/foobar", nil)
m.ServeHTTP(res, req)
expect(t, res.Body.String(), "My custom function\n")
}
示例7: TestMultipartMultipleFileForm
func TestMultipartMultipleFileForm(t *testing.T) {
for testIdx, tc := range multifileTests {
req := buildFormFileReq(t, &tc)
recorder := httptest.NewRecorder()
handler := func(fup MultipleFileUpload, errors Errors) {
// expecting everything to succeed
if errors.Count() > 0 {
t.Errorf("Expected no errors, got: %+v", errors)
}
assertEqualField(t, "Title", testIdx, tc.title, fup.Title)
if len(tc.documents) != len(fup.Document) {
t.Errorf("Expected %d documents, got: %+v", len(tc.documents), fup.Document)
}
for i, tcDocument := range tc.documents {
if (fup.Document[i] == nil) != tcDocument.isNil {
t.Errorf("Expected document.isNil: %+v, got %+v", tcDocument.isNil, fup.Document[i])
}
if fup.Document[i] != nil {
assertEqualField(t, "Filename", testIdx, tcDocument.fileName, fup.Document[i].Filename)
uploadData := unpackFileHeaderData(fup.Document[i], t)
assertEqualField(t, "Document Data", testIdx, tcDocument.data, uploadData)
}
}
}
m := martini.Classic()
m.Post(fileroute, MultipartForm(MultipleFileUpload{}), handler)
m.ServeHTTP(recorder, req)
}
}
示例8: TestMissingRequiredTypeForm
func TestMissingRequiredTypeForm(t *testing.T) {
m := martini.Classic()
m.Post("/", Bind(missingForm{}), func() {})
req, _ := http.NewRequest("POST", "/", bytes.NewBufferString(""))
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
recorder := httptest.NewRecorder()
m.ServeHTTP(recorder, req)
data, _ := ioutil.ReadAll(recorder.Body)
if string(data) != `{"overall":{},"fields":{"foo":"Required"}}` {
t.Error("Incorrect repsonse for missing required form field")
}
}
示例9: performValidationTest
func performValidationTest(data interface{}, handler func(Errors), t *testing.T) {
recorder := httptest.NewRecorder()
m := martini.Classic()
m.Get(route, Validate(data), handler)
req, err := http.NewRequest("GET", route, nil)
if err != nil {
t.Error("HTTP error:", err)
}
m.ServeHTTP(recorder, req)
}
示例10: TestMultipartFileForm
func TestMultipartFileForm(t *testing.T) {
for idx, tc := range multipartformfileTests {
req := buildFormFileReq(t, &tc)
recorder := httptest.NewRecorder()
handler := func(fup FileUpload, errors Errors) {
handleFile(tc, t, &fup, errors, recorder, idx)
}
m := martini.Classic()
m.Post(fileroute, MultipartForm(FileUpload{}), handler)
m.ServeHTTP(recorder, req)
}
}
示例11: TestMissingRequiredTypeJSON
func TestMissingRequiredTypeJSON(t *testing.T) {
m := martini.Classic()
m.Post("/", Bind(missingJSON{}), func() {})
m.Put("/", Bind(missingJSON{}), func() {})
for _, method := range []string{"POST", "PUT"} {
req, _ := http.NewRequest(method, "/", bytes.NewBufferString(""))
req.Header.Set("Content-Type", "application/json")
recorder := httptest.NewRecorder()
m.ServeHTTP(recorder, req)
data, _ := ioutil.ReadAll(recorder.Body)
if string(data) != `{"overall":{},"fields":{"foo":"Required"}}` {
t.Error("Incorrect repsonse for missing required JSON field")
}
}
}
示例12: testBind
func testBind(t *testing.T, withInterface bool) {
index := 0
for test, expectStatus := range bindTests {
m := martini.Classic()
recorder := httptest.NewRecorder()
handler := func(post BlogPost, errors Errors) { handle(test, t, index, post, errors) }
binding := Bind(BlogPost{})
if withInterface {
handler = func(post BlogPost, errors Errors) {
post.Create(test, t, index)
}
binding = Bind(BlogPost{}, (*Modeler)(nil))
}
switch test.method {
case "GET":
m.Get(route, binding, handler)
case "POST":
m.Post(route, binding, handler)
case "PUT":
m.Put(route, binding, handler)
case "DELETE":
m.Delete(route, binding, handler)
case "PATCH":
m.Patch(route, binding, handler)
}
req, err := http.NewRequest(test.method, test.path, strings.NewReader(test.payload))
req.Header.Add("Content-Type", test.contentType)
if err != nil {
t.Error(err)
}
m.ServeHTTP(recorder, req)
if recorder.Code != expectStatus {
t.Errorf("On test case %+v, got status code %d but expected %d", test, recorder.Code, expectStatus)
}
index++
}
}
示例13: Test_Render_Bad_HTML
func Test_Render_Bad_HTML(t *testing.T) {
m := martini.Classic()
m.Use(Renderer(Options{
Directory: "fixtures/basic",
}))
// routing
m.Get("/foobar", func(r Render) {
r.HTML(200, "nope", nil)
})
res := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/foobar", nil)
m.ServeHTTP(res, req)
expect(t, res.Code, 500)
expect(t, res.Body.String(), "html/template: \"nope\" is undefined\n")
}
示例14: Test_Render_Layout
func Test_Render_Layout(t *testing.T) {
m := martini.Classic()
m.Use(Renderer(Options{
Directory: "fixtures/basic",
Layout: "layout",
}))
// routing
m.Get("/foobar", func(r Render) {
r.HTML(200, "content", "jeremy")
})
res := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/foobar", nil)
m.ServeHTTP(res, req)
expect(t, res.Body.String(), "head\n<h1>jeremy</h1>\n\nfoot\n")
}
示例15: Test_Render_HTML
func Test_Render_HTML(t *testing.T) {
m := martini.Classic()
m.Use(Renderer(Options{
Directory: "fixtures/basic",
}))
// routing
m.Get("/foobar", func(r Render) {
r.HTML(200, "hello", "jeremy")
})
res := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/foobar", nil)
m.ServeHTTP(res, req)
expect(t, res.Code, 200)
expect(t, res.Header().Get(ContentType), ContentHTML+"; charset=UTF-8")
expect(t, res.Body.String(), "<h1>Hello jeremy</h1>\n")
}