mirror of
https://github.com/gin-gonic/gin.git
synced 2025-04-06 03:57:46 +08:00
init test
This commit is contained in:
parent
8763f33c65
commit
0fe68969cf
@ -8,6 +8,7 @@ import (
|
|||||||
"encoding/xml"
|
"encoding/xml"
|
||||||
"errors"
|
"errors"
|
||||||
"html/template"
|
"html/template"
|
||||||
|
"io"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
@ -420,6 +421,36 @@ func TestRenderData(t *testing.T) {
|
|||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
assert.Equal(t, "#!PNG some raw data", w.Body.String())
|
assert.Equal(t, "#!PNG some raw data", w.Body.String())
|
||||||
assert.Equal(t, "image/png", w.Header().Get("Content-Type"))
|
assert.Equal(t, "image/png", w.Header().Get("Content-Type"))
|
||||||
|
assert.Equal(t, "", w.Header().Get("Content-Length"))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRenderDataContentLength(t *testing.T) {
|
||||||
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
size, err := strconv.Atoi(r.URL.Query().Get("size"))
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
data := Data{
|
||||||
|
ContentType: "application/octet-stream",
|
||||||
|
Data: make([]byte, size),
|
||||||
|
}
|
||||||
|
assert.NoError(t, data.Render(w))
|
||||||
|
}))
|
||||||
|
t.Cleanup(srv.Close)
|
||||||
|
|
||||||
|
for _, size := range []int{0, 1, 100, 100_000} {
|
||||||
|
t.Run(strconv.Itoa(size), func(t *testing.T) {
|
||||||
|
resp, err := http.Get(srv.URL + "?size=" + strconv.Itoa(size))
|
||||||
|
require.NoError(t, err)
|
||||||
|
defer resp.Body.Close()
|
||||||
|
|
||||||
|
assert.Equal(t, "application/octet-stream", resp.Header.Get("Content-Type"))
|
||||||
|
assert.EqualValues(t, strconv.Itoa(size), resp.Header.Get("Content-Length"))
|
||||||
|
|
||||||
|
actual, err := io.Copy(io.Discard, resp.Body)
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.EqualValues(t, size, actual)
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestRenderString(t *testing.T) {
|
func TestRenderString(t *testing.T) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user