Added server tests

This commit is contained in:
2024-03-28 12:22:45 +01:00
parent 245a085020
commit 805f92468a
10 changed files with 116 additions and 36 deletions

View File

@ -18,7 +18,7 @@ func TestWrite(t *testing.T) {
return err
})
response := s.Request("GET", "/", nil)
response := s.Request("GET", "/", nil, nil)
assert.Equal(t, response.Status(), 200)
assert.Equal(t, string(response.Body()), "Hello")
}
@ -31,7 +31,7 @@ func TestWriteString(t *testing.T) {
return err
})
response := s.Request("GET", "/", nil)
response := s.Request("GET", "/", nil, nil)
assert.Equal(t, response.Status(), 200)
assert.Equal(t, string(response.Body()), "Hello")
}
@ -41,20 +41,22 @@ func TestResponseCompression(t *testing.T) {
uncompressed := bytes.Repeat([]byte("This text should be compressed to a size smaller than the original."), 5)
s.Use(func(ctx web.Context) error {
err := ctx.Next()
body := ctx.Response().Body()
ctx.Response().SetBody(nil)
zip := gzip.NewWriter(ctx.Response())
zip.Write(body)
zip.Close()
return err
defer func() {
body := ctx.Response().Body()
ctx.Response().SetBody(nil)
zip := gzip.NewWriter(ctx.Response())
zip.Write(body)
zip.Close()
}()
return ctx.Next()
})
s.Get("/", func(ctx web.Context) error {
return ctx.Bytes(uncompressed)
})
response := s.Request("GET", "/", nil)
response := s.Request("GET", "/", nil, nil)
assert.Equal(t, response.Status(), 200)
assert.True(t, len(response.Body()) < len(uncompressed))
@ -75,7 +77,7 @@ func TestResponseHeader(t *testing.T) {
return ctx.String(contentType)
})
response := s.Request("GET", "/", nil)
response := s.Request("GET", "/", nil, nil)
assert.Equal(t, response.Status(), 200)
assert.Equal(t, response.Header("Content-Type"), "text/plain")
assert.Equal(t, response.Header("Non existent header"), "")
@ -91,7 +93,7 @@ func TestResponseHeaderOverwrite(t *testing.T) {
return nil
})
response := s.Request("GET", "/", nil)
response := s.Request("GET", "/", nil, nil)
assert.Equal(t, response.Status(), 200)
assert.Equal(t, response.Header("Content-Type"), "text/html")
assert.Equal(t, string(response.Body()), "")