• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Golang martini.Classic函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Golang中github.com/flynn/flynn/Godeps/_workspace/src/github.com/go-martini/martini.Classic函数的典型用法代码示例。如果您正苦于以下问题:Golang Classic函数的具体用法?Golang Classic怎么用?Golang Classic使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。



在下文中一共展示了Classic函数的19个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的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)
	}
}
开发者ID:ericcapricorn,项目名称:flynn,代码行数:28,代码来源:binding_test.go


示例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
}
开发者ID:ericcapricorn,项目名称:flynn,代码行数:33,代码来源:binding_test.go


示例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)
	}
}
开发者ID:ericcapricorn,项目名称:flynn,代码行数:25,代码来源:binding_test.go


示例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)
	}
}
开发者ID:ericcapricorn,项目名称:flynn,代码行数:32,代码来源:binding_test.go


示例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
}
开发者ID:ericcapricorn,项目名称:flynn,代码行数:32,代码来源:render_test.go


示例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")
}
开发者ID:ericcapricorn,项目名称:flynn,代码行数:26,代码来源:render_test.go


示例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)
	}
}
开发者ID:ericcapricorn,项目名称:flynn,代码行数:32,代码来源:binding_test.go


示例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")
	}
}
开发者ID:ericcapricorn,项目名称:flynn,代码行数:12,代码来源:binding_test.go


示例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)
}
开发者ID:ericcapricorn,项目名称:flynn,代码行数:12,代码来源:binding_test.go


示例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)
	}
}
开发者ID:ericcapricorn,项目名称:flynn,代码行数:13,代码来源:binding_test.go


示例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")
		}
	}
}
开发者ID:ericcapricorn,项目名称:flynn,代码行数:15,代码来源:binding_test.go


示例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++
	}
}
开发者ID:ericcapricorn,项目名称:flynn,代码行数:43,代码来源:binding_test.go


示例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")
}
开发者ID:ericcapricorn,项目名称:flynn,代码行数:19,代码来源:render_test.go


示例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")
}
开发者ID:ericcapricorn,项目名称:flynn,代码行数:19,代码来源:render_test.go


示例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")
}
开发者ID:ericcapricorn,项目名称:flynn,代码行数:20,代码来源:render_test.go


示例16: Test_Render_Charset_JSON

func Test_Render_Charset_JSON(t *testing.T) {
	m := martini.Classic()
	m.Use(Renderer(Options{
		Charset: "foobar",
	}))

	// routing
	m.Get("/foobar", func(r Render) {
		r.JSON(300, Greeting{"hello", "world"})
	})

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

	m.ServeHTTP(res, req)

	expect(t, res.Code, 300)
	expect(t, res.Header().Get(ContentType), ContentJSON+"; charset=foobar")
	expect(t, res.Body.String(), `{"one":"hello","two":"world"}`)
}
开发者ID:ericcapricorn,项目名称:flynn,代码行数:20,代码来源:render_test.go


示例17: Test_Render_BinaryData

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

	// routing
	m.Get("/foobar", func(r Render) {
		r.Data(200, []byte("hello there"))
	})

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

	m.ServeHTTP(res, req)

	expect(t, res.Code, 200)
	expect(t, res.Header().Get(ContentType), ContentBinary)
	expect(t, res.Body.String(), "hello there")
}
开发者ID:ericcapricorn,项目名称:flynn,代码行数:20,代码来源:render_test.go


示例18: Test_Render_Extensions

func Test_Render_Extensions(t *testing.T) {
	m := martini.Classic()
	m.Use(Renderer(Options{
		Directory:  "fixtures/basic",
		Extensions: []string{".tmpl", ".html"},
	}))

	// routing
	m.Get("/foobar", func(r Render) {
		r.HTML(200, "hypertext", nil)
	})

	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(), "Hypertext!\n")
}
开发者ID:ericcapricorn,项目名称:flynn,代码行数:21,代码来源:render_test.go


示例19: Test_Render_Override_Layout

func Test_Render_Override_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", HTMLOptions{
			Layout: "another_layout",
		})
	})

	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(), "another head\n<h1>jeremy</h1>\n\nanother foot\n")
}
开发者ID:ericcapricorn,项目名称:flynn,代码行数:23,代码来源:render_test.go



注:本文中的github.com/flynn/flynn/Godeps/_workspace/src/github.com/go-martini/martini.Classic函数示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Golang martini.New函数代码示例发布时间:2022-05-23
下一篇:
Golang hstore.Hstore类代码示例发布时间:2022-05-23
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap