本文整理汇总了Golang中github.com/corestoreio/csfw/net/httputil.NewPrinter函数的典型用法代码示例。如果您正苦于以下问题:Golang NewPrinter函数的具体用法?Golang NewPrinter怎么用?Golang NewPrinter使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了NewPrinter函数的19个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。
示例1: TestPrintRenderErrors
func TestPrintRenderErrors(t *testing.T) {
assert.EqualError(t, httputil.NewPrinter(nil, nil).Render(0, "", nil), httputil.ErrRendererNotRegistered.Error())
w := httptest.NewRecorder()
p := httputil.NewPrinter(w, nil)
tpl, err := template.New("foo").Parse(`{{define "T"}}Hello, {{.}}!{{end}}`)
assert.NoError(t, err)
p.Renderer = tpl
assert.EqualError(t, p.Render(3141, "X", nil), "template: no template \"X\" associated with template \"foo\"")
assert.Exactly(t, ``, w.Body.String())
}
开发者ID:joao-parana,项目名称:csfw,代码行数:12,代码来源:response_test.go
示例2: TestPrintFileDirectoryIndex
func TestPrintFileDirectoryIndex(t *testing.T) {
testMemFs := &memFS{MemMapFs: new(afero.MemMapFs)}
assert.NoError(t, testMemFs.Mkdir("test", 0777))
f, err := testMemFs.Create("test/index.html")
if err != nil {
t.Fatal(err)
}
if _, err = f.Write([]byte(`<h1>This is a huge h1 tag!</h1>`)); err != nil {
t.Fatal(err)
}
if err := f.Close(); err != nil {
t.Fatal(err)
}
w := httptest.NewRecorder()
r, err := http.NewRequest("GET", "http://coretore.io", nil)
assert.NoError(t, err)
p := httputil.NewPrinter(w, r)
p.FileSystem = testMemFs
assert.NoError(t, p.File("/test", "", false))
assert.Equal(t, "text/html; charset=utf-8", w.Header().Get(httputil.ContentType))
assert.Equal(t, "", w.Header().Get(httputil.ContentDisposition))
assert.Exactly(t, "<h1>This is a huge h1 tag!</h1>", w.Body.String())
assert.Exactly(t, 200, w.Code)
}
开发者ID:joao-parana,项目名称:csfw,代码行数:30,代码来源:response_test.go
示例3: TestPrintNoContent
func TestPrintNoContent(t *testing.T) {
w := httptest.NewRecorder()
p := httputil.NewPrinter(w, nil)
assert.NoError(t, p.NoContent(501))
assert.Exactly(t, "", w.Body.String())
assert.Exactly(t, 501, w.Code)
}
开发者ID:joao-parana,项目名称:csfw,代码行数:7,代码来源:response_test.go
示例4: TestPrintHTML
func TestPrintHTML(t *testing.T) {
w := httptest.NewRecorder()
p := httputil.NewPrinter(w, nil)
assert.NoError(t, p.HTML(3141, "Hello %s. Wanna have some %.5f?", "Gophers", math.Pi))
assert.Exactly(t, `Hello Gophers. Wanna have some 3.14159?`, w.Body.String())
assert.Exactly(t, 3141, w.Code)
assert.Equal(t, httputil.TextHTMLCharsetUTF8, w.Header().Get(httputil.ContentType))
}
开发者ID:joao-parana,项目名称:csfw,代码行数:9,代码来源:response_test.go
示例5: TestPrintXMLIndent
func TestPrintXMLIndent(t *testing.T) {
w := httptest.NewRecorder()
p := httputil.NewPrinter(w, nil)
assert.NoError(t, p.XMLIndent(3141, encodeData, "\n", "\t"))
assert.Exactly(t, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\n<EncData>\n\n\t<Title>Camera</Title>\n\n\t<SKU>323423423</SKU>\n\n\t<Price>45.12</Price>\n\n</EncData>\n\n<EncData>\n\n\t<Title>LCD TV</Title>\n\n\t<SKU>8785344</SKU>\n\n\t<Price>145.99</Price>\n\n</EncData>", w.Body.String())
assert.Exactly(t, 3141, w.Code)
assert.Equal(t, httputil.ApplicationXMLCharsetUTF8, w.Header().Get(httputil.ContentType))
}
开发者ID:joao-parana,项目名称:csfw,代码行数:9,代码来源:response_test.go
示例6: TestPrintXMLIndentError
func TestPrintXMLIndentError(t *testing.T) {
w := httptest.NewRecorder()
p := httputil.NewPrinter(w, nil)
assert.EqualError(t, p.XMLIndent(3141, nonMarshallableChannel, " ", " "), "xml: unsupported type: chan bool")
assert.Exactly(t, "", w.Body.String())
assert.Exactly(t, 200, w.Code)
assert.Equal(t, "", w.Header().Get(httputil.ContentType))
}
开发者ID:joao-parana,项目名称:csfw,代码行数:9,代码来源:response_test.go
示例7: TestPrintJSONPError
func TestPrintJSONPError(t *testing.T) {
w := httptest.NewRecorder()
p := httputil.NewPrinter(w, nil)
assert.EqualError(t, p.JSONP(3141, "awesomeReact", nonMarshallableChannel), "json: unsupported type: chan bool")
assert.Exactly(t, "", w.Body.String())
assert.Exactly(t, 200, w.Code)
assert.Equal(t, "", w.Header().Get(httputil.ContentType))
}
开发者ID:joao-parana,项目名称:csfw,代码行数:9,代码来源:response_test.go
示例8: TestPrintJSONP
func TestPrintJSONP(t *testing.T) {
w := httptest.NewRecorder()
p := httputil.NewPrinter(w, nil)
assert.NoError(t, p.JSONP(3141, "awesomeReact", encodeData))
assert.Exactly(t, "awesomeReact([{\"Title\":\"Camera\",\"SKU\":\"323423423\",\"Price\":45.12},{\"Title\":\"LCD TV\",\"SKU\":\"8785344\",\"Price\":145.99}]\n);", w.Body.String())
assert.Exactly(t, 3141, w.Code)
assert.Equal(t, httputil.ApplicationJavaScriptCharsetUTF8, w.Header().Get(httputil.ContentType))
}
开发者ID:joao-parana,项目名称:csfw,代码行数:9,代码来源:response_test.go
示例9: TestPrintJSONIndent
func TestPrintJSONIndent(t *testing.T) {
w := httptest.NewRecorder()
p := httputil.NewPrinter(w, nil)
assert.NoError(t, p.JSONIndent(3141, encodeData, " ", "\t"))
assert.Exactly(t, "[\n \t{\n \t\t\"Title\": \"Camera\",\n \t\t\"SKU\": \"323423423\",\n \t\t\"Price\": 45.12\n \t},\n \t{\n \t\t\"Title\": \"LCD TV\",\n \t\t\"SKU\": \"8785344\",\n \t\t\"Price\": 145.99\n \t}\n ]", w.Body.String())
assert.Exactly(t, 3141, w.Code)
assert.Equal(t, httputil.ApplicationJSONCharsetUTF8, w.Header().Get(httputil.ContentType))
}
开发者ID:joao-parana,项目名称:csfw,代码行数:9,代码来源:response_test.go
示例10: TestPrintWriteString
func TestPrintWriteString(t *testing.T) {
w := httptest.NewRecorder()
p := httputil.NewPrinter(w, nil)
assert.NoError(t, p.WriteString(3141, "Hello %s. Wanna have some %.5f?"))
assert.Exactly(t, `Hello %s. Wanna have some %.5f?`, w.Body.String())
assert.Exactly(t, 3141, w.Code)
assert.Equal(t, httputil.TextPlain, w.Header().Get(httputil.ContentType))
}
开发者ID:joao-parana,项目名称:csfw,代码行数:9,代码来源:response_test.go
示例11: TestPrintHTMLError
func TestPrintHTMLError(t *testing.T) {
w := new(errorWriter)
w.ResponseRecorder = httptest.NewRecorder()
p := httputil.NewPrinter(w, nil)
assert.EqualError(t, p.HTML(31415, "Hello %s", "Gophers"), "Not in the mood to write today")
assert.Exactly(t, ``, w.Body.String())
assert.Exactly(t, 31415, w.Code)
assert.Equal(t, httputil.TextHTMLCharsetUTF8, w.Header().Get(httputil.ContentType))
}
开发者ID:joao-parana,项目名称:csfw,代码行数:10,代码来源:response_test.go
示例12: TestPrintRender
func TestPrintRender(t *testing.T) {
w := httptest.NewRecorder()
p := httputil.NewPrinter(w, nil)
tpl, err := template.New("foo").Parse(`{{define "T"}}Hello, {{.}}!{{end}}`)
assert.NoError(t, err)
p.Renderer = tpl
assert.NoError(t, p.Render(3141, "T", "<script>alert('you have been pwned')</script>"))
assert.Exactly(t, `Hello, <script>alert('you have been pwned')</script>!`, w.Body.String())
assert.Exactly(t, 3141, w.Code)
assert.Equal(t, httputil.TextHTMLCharsetUTF8, w.Header().Get(httputil.ContentType))
}
开发者ID:joao-parana,项目名称:csfw,代码行数:11,代码来源:response_test.go
示例13: TestPrintRedirect
func TestPrintRedirect(t *testing.T) {
w := httptest.NewRecorder()
r, err := http.NewRequest("GET", "http://coretore.io", nil)
assert.NoError(t, err)
p := httputil.NewPrinter(w, r)
assert.EqualError(t, p.Redirect(501, ""), httputil.ErrInvalidRedirectCode.Error())
p.Redirect(http.StatusMovedPermanently, "http://cs.io")
assert.Exactly(t, http.StatusMovedPermanently, w.Code)
assert.Equal(t, "http://cs.io", w.Header().Get("Location"))
assert.Exactly(t, "<a href=\"http://cs.io\">Moved Permanently</a>.\n\n", w.Body.String())
}
开发者ID:joao-parana,项目名称:csfw,代码行数:13,代码来源:response_test.go
示例14: jsonStores
func jsonStores(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
storeReader, _, err := store.FromContextReader(ctx)
if err != nil {
return err // default StatusInternalServerError
}
stores, err := storeReader.Stores()
if err != nil {
return ctxhttp.NewErrorFromErrors(http.StatusInternalServerError, err)
}
return httputil.NewPrinter(w, r).JSON(http.StatusOK, stores)
}
开发者ID:corestoreio,项目名称:csms,代码行数:13,代码来源:main.go
示例15: TestPrintFileWithAttachmentError
func TestPrintFileWithAttachmentError(t *testing.T) {
w := httptest.NewRecorder()
r, err := http.NewRequest("GET", "http://coretore.io", nil)
assert.NoError(t, err)
p := httputil.NewPrinter(w, r)
assert.EqualError(t, p.File("gopher.svg", "gopher-logo.svg", true), "File not found: => gopher.svg")
assert.Equal(t, "", w.Header().Get(httputil.ContentType))
assert.Equal(t, "", w.Header().Get(httputil.ContentDisposition))
assert.Exactly(t, "", w.Body.String())
assert.Exactly(t, 200, w.Code)
}
开发者ID:joao-parana,项目名称:csfw,代码行数:14,代码来源:response_test.go
示例16: routeLogin
func (a *app) routeLogin(rtr *ctxrouter.Router) {
staticClaims := map[string]interface{}{
"xfoo": "bar",
"xtime": time.Now().Unix(),
}
rtr.GET("/login", func(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
ts, _, err := a.jwtSrv.GenerateToken(staticClaims)
if err != nil {
return err
}
return httputil.NewPrinter(w, r).WriteString(http.StatusOK, ts)
})
}
开发者ID:corestoreio,项目名称:csms,代码行数:15,代码来源:main.go
示例17: TestPrintFileWithAttachment
func TestPrintFileWithAttachment(t *testing.T) {
w := httptest.NewRecorder()
r, err := http.NewRequest("GET", "http://coretore.io", nil)
assert.NoError(t, err)
p := httputil.NewPrinter(w, r)
p.FileSystem = testMemFs
assert.NoError(t, p.File("gopher.svg", "gopher-logo.svg", true))
assert.Equal(t, "image/svg+xml", w.Header().Get(httputil.ContentType))
assert.Equal(t, "attachment; filename=gopher-logo.svg", w.Header().Get(httputil.ContentDisposition))
assert.Exactly(t, "<svg/>", w.Body.String())
assert.Exactly(t, 200, w.Code)
}
开发者ID:joao-parana,项目名称:csfw,代码行数:16,代码来源:response_test.go
示例18: BenchmarkWithCompressorGZIP_1024B
// BenchmarkWithCompressorGZIP_1024B-4 20000 81916 ns/op 1330 B/op 5 allocs/op
func BenchmarkWithCompressorGZIP_1024B(b *testing.B) {
rawData := randSeq(1024)
finalCH := ctxhttp.Chain(func(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
return httputil.NewPrinter(w, r).WriteString(http.StatusOK, rawData)
}, ctxmw.WithCompressor())
w, r := testCompressReqRes()
r.Header.Set(httputil.AcceptEncoding, httputil.CompressGZIP)
ctx := context.TODO()
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
if err := finalCH.ServeHTTPContext(ctx, w, r); err != nil {
b.Fatal(err)
}
w.Body.Reset()
}
}
开发者ID:joao-parana,项目名称:csfw,代码行数:22,代码来源:compress_test.go
示例19: testWithCompressorConcrete
func testWithCompressorConcrete(t *testing.T, header string, uncompressor func(io.Reader) string) {
rawData := randSeq(1024)
finalCH := ctxhttp.Chain(func(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
return httputil.NewPrinter(w, r).WriteString(http.StatusOK, rawData)
}, ctxmw.WithCompressor())
w, r := testCompressReqRes()
r.Header.Set(httputil.AcceptEncoding, header)
if err := finalCH.ServeHTTPContext(context.TODO(), w, r); err != nil {
t.Fatal(err)
}
assert.False(t, len(rawData) < len(w.Body.Bytes()))
uncompressedBody := uncompressor(w.Body)
assert.Exactly(t, rawData, uncompressedBody)
assert.Exactly(t, header, w.Header().Get(httputil.ContentEncoding))
assert.Exactly(t, httputil.AcceptEncoding, w.Header().Get(httputil.Vary))
assert.Exactly(t, httputil.TextPlain, w.Header().Get(httputil.ContentType))
}
开发者ID:joao-parana,项目名称:csfw,代码行数:23,代码来源:compress_test.go
注:本文中的github.com/corestoreio/csfw/net/httputil.NewPrinter函数示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论