fix headers

This commit is contained in:
Nazar Kanaev 2021-04-26 15:16:26 +01:00
parent 87b53fb8ec
commit 36e359c881
2 changed files with 30 additions and 2 deletions

View File

@ -29,15 +29,15 @@ func (c *Context) JSON(status int, data interface{}) {
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
c.Out.WriteHeader(status)
c.Out.Header().Set("Content-Type", "application/json; charset=utf-8") c.Out.Header().Set("Content-Type", "application/json; charset=utf-8")
c.Out.WriteHeader(status)
c.Out.Write(body) c.Out.Write(body)
c.Out.Write([]byte("\n")) c.Out.Write([]byte("\n"))
} }
func (c *Context) HTML(status int, tmpl *template.Template, data interface{}) { func (c *Context) HTML(status int, tmpl *template.Template, data interface{}) {
c.Out.WriteHeader(status)
c.Out.Header().Set("Content-Type", "text/html") c.Out.Header().Set("Content-Type", "text/html")
c.Out.WriteHeader(status)
tmpl.Execute(c.Out, data) tmpl.Execute(c.Out, data)
} }

View File

@ -1,8 +1,13 @@
package server package server
import ( import (
"io"
"log"
"net/http/httptest" "net/http/httptest"
"os"
"testing" "testing"
"github.com/nkanaev/yarr/src/storage"
) )
func TestStatic(t *testing.T) { func TestStatic(t *testing.T) {
@ -43,3 +48,26 @@ func TestStaticBanTemplates(t *testing.T) {
t.FailNow() t.FailNow()
} }
} }
func TestIndexGzipped(t *testing.T) {
log.SetOutput(io.Discard)
db, _ := storage.New(":memory:")
log.SetOutput(os.Stderr)
handler := NewServer(db, "127.0.0.1:8000").handler()
url := "/"
recorder := httptest.NewRecorder()
request := httptest.NewRequest("GET", url, nil)
request.Header.Set("accept-encoding", "gzip")
handler.ServeHTTP(recorder, request)
response := recorder.Result()
if response.StatusCode != 200 {
t.FailNow()
}
if response.Header.Get("content-encoding") != "gzip" {
t.Errorf("invalid content-encoding header: %#v", response.Header.Get("content-encoding"))
}
if response.Header.Get("content-type") != "text/html" {
t.Errorf("invalid content-type header: %#v", response.Header.Get("content-type"))
}
}