代码拉取完成,页面将自动刷新
同步操作将从 Flamego/flamego 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
// Copyright 2021 Flamego. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package flamego
import (
"bytes"
"net/http"
"net/http/httptest"
"os"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestClassic(_ *testing.T) {
_ = Classic() // For the sake of code coverage
}
// Make sure Run doesn't blow up
func TestFlame_Run(_ *testing.T) {
_ = os.Setenv("FLAMEGO_ADDR", "0.0.0.0:4001")
f := NewWithLogger(&bytes.Buffer{})
go f.Run(4002)
time.Sleep(1 * time.Second)
f.Stop()
}
func TestFlame_Before(t *testing.T) {
f := New()
var buf bytes.Buffer
f.Before(func(http.ResponseWriter, *http.Request) bool {
buf.WriteString("foo")
return false
})
f.Before(func(http.ResponseWriter, *http.Request) bool {
buf.WriteString("bar")
return true
})
f.Get("/", func() {
buf.WriteString("boom")
})
resp := httptest.NewRecorder()
req, err := http.NewRequest(http.MethodGet, "/", nil)
assert.Nil(t, err)
f.ServeHTTP(resp, req)
assert.Equal(t, "foobar", buf.String())
}
func TestFlame_ServeHTTP(t *testing.T) {
var buf bytes.Buffer
f := New()
f.Use(func(c Context) {
buf.WriteString("foo")
c.Next()
buf.WriteString("ban")
})
f.Use(func(c Context) {
buf.WriteString("bar")
c.Next()
buf.WriteString("baz")
})
f.Get("/", func() {})
f.Action(func(w http.ResponseWriter, r *http.Request) {
buf.WriteString("bat")
w.WriteHeader(http.StatusBadRequest)
})
resp := httptest.NewRecorder()
req, err := http.NewRequest(http.MethodGet, "/", nil)
assert.Nil(t, err)
f.ServeHTTP(resp, req)
assert.Equal(t, "foobarbatbazban", buf.String())
assert.Equal(t, http.StatusBadRequest, resp.Code)
}
func TestFlame_Handlers(t *testing.T) {
var buf bytes.Buffer
batman := func() {
buf.WriteString("batman!")
}
f := New()
f.Use(func(c Context) {
buf.WriteString("foo")
c.Next()
buf.WriteString("ban")
})
f.Handlers(
batman,
batman,
batman,
)
f.Get("/", func() {})
f.Action(func(w http.ResponseWriter, r *http.Request) {
buf.WriteString("bat")
w.WriteHeader(http.StatusBadRequest)
})
resp := httptest.NewRecorder()
req, err := http.NewRequest(http.MethodGet, "/", nil)
assert.Nil(t, err)
f.ServeHTTP(resp, req)
assert.Equal(t, "batman!batman!batman!bat", buf.String())
assert.Equal(t, http.StatusBadRequest, resp.Code)
}
func TestFlame_EarlyWrite(t *testing.T) {
var buf bytes.Buffer
f := New()
f.Use(func(w http.ResponseWriter) {
buf.WriteString("foobar")
_, _ = w.Write([]byte("Hello world"))
})
f.Use(func() {
buf.WriteString("bat")
})
f.Get("/", func() {})
f.Action(func(w http.ResponseWriter) {
buf.WriteString("baz")
w.WriteHeader(http.StatusBadRequest)
})
resp := httptest.NewRecorder()
req, err := http.NewRequest(http.MethodGet, "/", nil)
assert.Nil(t, err)
f.ServeHTTP(resp, req)
assert.Equal(t, "foobar", buf.String())
assert.Equal(t, http.StatusOK, resp.Code)
}
func TestFlame_NoRace(t *testing.T) {
f := New()
handlers := []Handler{func() {}, func() {}}
// Ensure append will not reallocate alice that triggers the race condition
f.handlers = handlers[:1]
f.Get("/", func() {})
for i := 0; i < 2; i++ {
go func() {
req, err := http.NewRequest(http.MethodGet, "/", nil)
resp := httptest.NewRecorder()
assert.Nil(t, err)
f.ServeHTTP(resp, req)
}()
}
}
func TestEnv(t *testing.T) {
defer SetEnv(EnvTypeDev)
envs := []EnvType{
EnvTypeDev,
EnvTypeProd,
EnvTypeTest,
}
for _, env := range envs {
SetEnv(env)
assert.Equal(t, env, Env())
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。