Exemple #1
0
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())

}
Exemple #2
0
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)
}
Exemple #3
0
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)
}
Exemple #4
0
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))
}
Exemple #5
0
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))
}
Exemple #6
0
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))
}
Exemple #7
0
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))
}
Exemple #8
0
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))
}
Exemple #9
0
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))
}
Exemple #10
0
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))
}
Exemple #11
0
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))
}
Exemple #12
0
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))
}
Exemple #13
0
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())
}
Exemple #14
0
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)
}
Exemple #15
0
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)
}
Exemple #16
0
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)
	})
}
Exemple #17
0
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)
}
Exemple #18
0
// 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()
	}
}
Exemple #19
0
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))

}