mirror of https://github.com/gofiber/fiber.git
Release Test function for Fiber
parent
b69d99b2ac
commit
406cfe67d5
50
listen.go
50
listen.go
|
@ -36,29 +36,7 @@ func (r *Fiber) Listen(address interface{}, tls ...string) {
|
||||||
log.Fatal("Listen: Host must be an INT port or STRING address")
|
log.Fatal("Listen: Host must be an INT port or STRING address")
|
||||||
}
|
}
|
||||||
// Create fasthttp server
|
// Create fasthttp server
|
||||||
server := &fasthttp.Server{
|
server := r.setupServer()
|
||||||
Handler: r.handler,
|
|
||||||
Name: r.Server,
|
|
||||||
Concurrency: r.Engine.Concurrency,
|
|
||||||
DisableKeepalive: r.Engine.DisableKeepAlive,
|
|
||||||
ReadBufferSize: r.Engine.ReadBufferSize,
|
|
||||||
WriteBufferSize: r.Engine.WriteBufferSize,
|
|
||||||
ReadTimeout: r.Engine.ReadTimeout,
|
|
||||||
WriteTimeout: r.Engine.WriteTimeout,
|
|
||||||
IdleTimeout: r.Engine.IdleTimeout,
|
|
||||||
MaxConnsPerIP: r.Engine.MaxConnsPerIP,
|
|
||||||
MaxRequestsPerConn: r.Engine.MaxRequestsPerConn,
|
|
||||||
TCPKeepalive: r.Engine.TCPKeepalive,
|
|
||||||
TCPKeepalivePeriod: r.Engine.TCPKeepalivePeriod,
|
|
||||||
MaxRequestBodySize: r.Engine.MaxRequestBodySize,
|
|
||||||
ReduceMemoryUsage: r.Engine.ReduceMemoryUsage,
|
|
||||||
GetOnly: r.Engine.GetOnly,
|
|
||||||
DisableHeaderNamesNormalizing: r.Engine.DisableHeaderNamesNormalizing,
|
|
||||||
SleepWhenConcurrencyLimitsExceeded: r.Engine.SleepWhenConcurrencyLimitsExceeded,
|
|
||||||
NoDefaultServerHeader: r.Server == "",
|
|
||||||
NoDefaultContentType: r.Engine.NoDefaultContentType,
|
|
||||||
KeepHijackedConns: r.Engine.KeepHijackedConns,
|
|
||||||
}
|
|
||||||
|
|
||||||
// Prefork enabled
|
// Prefork enabled
|
||||||
if r.Prefork && runtime.NumCPU() > 1 {
|
if r.Prefork && runtime.NumCPU() > 1 {
|
||||||
|
@ -137,3 +115,29 @@ func (r *Fiber) prefork(server *fasthttp.Server, host string, tls ...string) {
|
||||||
log.Fatal("Listen-prefork: ", err)
|
log.Fatal("Listen-prefork: ", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (r *Fiber) setupServer() *fasthttp.Server {
|
||||||
|
return &fasthttp.Server{
|
||||||
|
Handler: r.handler,
|
||||||
|
Name: r.Server,
|
||||||
|
Concurrency: r.Engine.Concurrency,
|
||||||
|
DisableKeepalive: r.Engine.DisableKeepAlive,
|
||||||
|
ReadBufferSize: r.Engine.ReadBufferSize,
|
||||||
|
WriteBufferSize: r.Engine.WriteBufferSize,
|
||||||
|
ReadTimeout: r.Engine.ReadTimeout,
|
||||||
|
WriteTimeout: r.Engine.WriteTimeout,
|
||||||
|
IdleTimeout: r.Engine.IdleTimeout,
|
||||||
|
MaxConnsPerIP: r.Engine.MaxConnsPerIP,
|
||||||
|
MaxRequestsPerConn: r.Engine.MaxRequestsPerConn,
|
||||||
|
TCPKeepalive: r.Engine.TCPKeepalive,
|
||||||
|
TCPKeepalivePeriod: r.Engine.TCPKeepalivePeriod,
|
||||||
|
MaxRequestBodySize: r.Engine.MaxRequestBodySize,
|
||||||
|
ReduceMemoryUsage: r.Engine.ReduceMemoryUsage,
|
||||||
|
GetOnly: r.Engine.GetOnly,
|
||||||
|
DisableHeaderNamesNormalizing: r.Engine.DisableHeaderNamesNormalizing,
|
||||||
|
SleepWhenConcurrencyLimitsExceeded: r.Engine.SleepWhenConcurrencyLimitsExceeded,
|
||||||
|
NoDefaultServerHeader: r.Server == "",
|
||||||
|
NoDefaultContentType: r.Engine.NoDefaultContentType,
|
||||||
|
KeepHijackedConns: r.Engine.KeepHijackedConns,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
153
request_test.go
153
request_test.go
|
@ -5,6 +5,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"mime/multipart"
|
"mime/multipart"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"net/http/httptest"
|
||||||
"net/url"
|
"net/url"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
@ -14,19 +15,26 @@ import (
|
||||||
func Test_Accepts(t *testing.T) {
|
func Test_Accepts(t *testing.T) {
|
||||||
app := New()
|
app := New()
|
||||||
app.Get("/test", func(c *Ctx) {
|
app.Get("/test", func(c *Ctx) {
|
||||||
c.Accepts()
|
expect := ""
|
||||||
expect := ".xml"
|
|
||||||
result := c.Accepts(expect)
|
result := c.Accepts(expect)
|
||||||
|
if c.Accepts() != "" {
|
||||||
|
t.Fatalf(`Expecting %s, got %s`, expect, result)
|
||||||
|
}
|
||||||
|
expect = ".xml"
|
||||||
|
result = c.Accepts(expect)
|
||||||
if result != expect {
|
if result != expect {
|
||||||
t.Fatalf(`%s: Expecting %s, got %s`, t.Name(), expect, result)
|
t.Fatalf(`Expecting %s, got %s`, expect, result)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
req, _ := http.NewRequest("GET", "/test", nil)
|
req := httptest.NewRequest("GET", "/test", nil)
|
||||||
req.Header.Set("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
|
req.Header.Set("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
|
||||||
_, err := app.Test(req)
|
resp, err := app.Test(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf(`%s: %s`, t.Name(), err)
|
t.Fatalf(`%s: %s`, t.Name(), err)
|
||||||
}
|
}
|
||||||
|
if resp.StatusCode != 200 {
|
||||||
|
t.Fatalf(`%s: StatusCode %v`, t.Name(), resp.StatusCode)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
func Test_AcceptsCharsets(t *testing.T) {
|
func Test_AcceptsCharsets(t *testing.T) {
|
||||||
app := New()
|
app := New()
|
||||||
|
@ -41,10 +49,13 @@ func Test_AcceptsCharsets(t *testing.T) {
|
||||||
})
|
})
|
||||||
req, _ := http.NewRequest("GET", "/test", nil)
|
req, _ := http.NewRequest("GET", "/test", nil)
|
||||||
req.Header.Set("Accept-Charset", "utf-8, iso-8859-1;q=0.5")
|
req.Header.Set("Accept-Charset", "utf-8, iso-8859-1;q=0.5")
|
||||||
_, err := app.Test(req)
|
resp, err := app.Test(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf(`%s: %s`, t.Name(), err)
|
t.Fatalf(`%s: %s`, t.Name(), err)
|
||||||
}
|
}
|
||||||
|
if resp.StatusCode != 200 {
|
||||||
|
t.Fatalf(`%s: StatusCode %v`, t.Name(), resp.StatusCode)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
func Test_AcceptsEncodings(t *testing.T) {
|
func Test_AcceptsEncodings(t *testing.T) {
|
||||||
app := New()
|
app := New()
|
||||||
|
@ -58,10 +69,13 @@ func Test_AcceptsEncodings(t *testing.T) {
|
||||||
})
|
})
|
||||||
req, _ := http.NewRequest("GET", "/test", nil)
|
req, _ := http.NewRequest("GET", "/test", nil)
|
||||||
req.Header.Set("Accept-Encoding", "deflate, gzip;q=1.0, *;q=0.5")
|
req.Header.Set("Accept-Encoding", "deflate, gzip;q=1.0, *;q=0.5")
|
||||||
_, err := app.Test(req)
|
resp, err := app.Test(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf(`%s: %s`, t.Name(), err)
|
t.Fatalf(`%s: %s`, t.Name(), err)
|
||||||
}
|
}
|
||||||
|
if resp.StatusCode != 200 {
|
||||||
|
t.Fatalf(`%s: StatusCode %v`, t.Name(), resp.StatusCode)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
func Test_AcceptsLanguages(t *testing.T) {
|
func Test_AcceptsLanguages(t *testing.T) {
|
||||||
app := New()
|
app := New()
|
||||||
|
@ -75,10 +89,13 @@ func Test_AcceptsLanguages(t *testing.T) {
|
||||||
})
|
})
|
||||||
req, _ := http.NewRequest("GET", "/test", nil)
|
req, _ := http.NewRequest("GET", "/test", nil)
|
||||||
req.Header.Set("Accept-Language", "fr-CH, fr;q=0.9, en;q=0.8, de;q=0.7, *;q=0.5")
|
req.Header.Set("Accept-Language", "fr-CH, fr;q=0.9, en;q=0.8, de;q=0.7, *;q=0.5")
|
||||||
_, err := app.Test(req)
|
resp, err := app.Test(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf(`%s: %s`, t.Name(), err)
|
t.Fatalf(`%s: %s`, t.Name(), err)
|
||||||
}
|
}
|
||||||
|
if resp.StatusCode != 200 {
|
||||||
|
t.Fatalf(`%s: StatusCode %v`, t.Name(), resp.StatusCode)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
func Test_BaseURL(t *testing.T) {
|
func Test_BaseURL(t *testing.T) {
|
||||||
app := New()
|
app := New()
|
||||||
|
@ -91,10 +108,13 @@ func Test_BaseURL(t *testing.T) {
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
req, _ := http.NewRequest("GET", "http://google.com/test", nil)
|
req, _ := http.NewRequest("GET", "http://google.com/test", nil)
|
||||||
_, err := app.Test(req)
|
resp, err := app.Test(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf(`%s: %s`, t.Name(), err)
|
t.Fatalf(`%s: %s`, t.Name(), err)
|
||||||
}
|
}
|
||||||
|
if resp.StatusCode != 200 {
|
||||||
|
t.Fatalf(`%s: StatusCode %v`, t.Name(), resp.StatusCode)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
func Test_BasicAuth(t *testing.T) {
|
func Test_BasicAuth(t *testing.T) {
|
||||||
app := New()
|
app := New()
|
||||||
|
@ -111,10 +131,13 @@ func Test_BasicAuth(t *testing.T) {
|
||||||
})
|
})
|
||||||
req, _ := http.NewRequest("GET", "/test", nil)
|
req, _ := http.NewRequest("GET", "/test", nil)
|
||||||
req.SetBasicAuth("john", "doe")
|
req.SetBasicAuth("john", "doe")
|
||||||
_, err := app.Test(req)
|
resp, err := app.Test(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf(`%s: %s`, t.Name(), err)
|
t.Fatalf(`%s: %s`, t.Name(), err)
|
||||||
}
|
}
|
||||||
|
if resp.StatusCode != 200 {
|
||||||
|
t.Fatalf(`%s: StatusCode %v`, t.Name(), resp.StatusCode)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
func Test_Body(t *testing.T) {
|
func Test_Body(t *testing.T) {
|
||||||
app := New()
|
app := New()
|
||||||
|
@ -151,10 +174,13 @@ func Test_Body(t *testing.T) {
|
||||||
req, _ := http.NewRequest("POST", "/test", strings.NewReader(data.Encode()))
|
req, _ := http.NewRequest("POST", "/test", strings.NewReader(data.Encode()))
|
||||||
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
|
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
|
||||||
req.Header.Add("Content-Length", strconv.Itoa(len(data.Encode())))
|
req.Header.Add("Content-Length", strconv.Itoa(len(data.Encode())))
|
||||||
_, err := app.Test(req)
|
resp, err := app.Test(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf(`%s: %s`, t.Name(), err)
|
t.Fatalf(`%s: %s`, t.Name(), err)
|
||||||
}
|
}
|
||||||
|
if resp.StatusCode != 200 {
|
||||||
|
t.Fatalf(`%s: StatusCode %v`, t.Name(), resp.StatusCode)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
func Test_Cookies(t *testing.T) {
|
func Test_Cookies(t *testing.T) {
|
||||||
app := New()
|
app := New()
|
||||||
|
@ -217,10 +243,13 @@ func Test_FormValue(t *testing.T) {
|
||||||
|
|
||||||
req.Header.Set("Content-Type", contentType)
|
req.Header.Set("Content-Type", contentType)
|
||||||
req.Header.Set("Content-Length", strconv.Itoa(len(body.Bytes())))
|
req.Header.Set("Content-Length", strconv.Itoa(len(body.Bytes())))
|
||||||
_, err := app.Test(req)
|
resp, err := app.Test(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf(`%s: %s`, t.Name(), err)
|
t.Fatalf(`%s: %s`, t.Name(), err)
|
||||||
}
|
}
|
||||||
|
if resp.StatusCode != 200 {
|
||||||
|
t.Fatalf(`%s: StatusCode %v`, t.Name(), resp.StatusCode)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
func Test_Fresh(t *testing.T) {
|
func Test_Fresh(t *testing.T) {
|
||||||
app := New()
|
app := New()
|
||||||
|
@ -256,10 +285,13 @@ func Test_Get(t *testing.T) {
|
||||||
req, _ := http.NewRequest("GET", "/test", nil)
|
req, _ := http.NewRequest("GET", "/test", nil)
|
||||||
req.Header.Set("Accept-Charset", "utf-8, iso-8859-1;q=0.5")
|
req.Header.Set("Accept-Charset", "utf-8, iso-8859-1;q=0.5")
|
||||||
req.Header.Set("Referer", "Cookie")
|
req.Header.Set("Referer", "Cookie")
|
||||||
_, err := app.Test(req)
|
resp, err := app.Test(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf(`%s: %s`, t.Name(), err)
|
t.Fatalf(`%s: %s`, t.Name(), err)
|
||||||
}
|
}
|
||||||
|
if resp.StatusCode != 200 {
|
||||||
|
t.Fatalf(`%s: StatusCode %v`, t.Name(), resp.StatusCode)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
func Test_Hostname(t *testing.T) {
|
func Test_Hostname(t *testing.T) {
|
||||||
app := New()
|
app := New()
|
||||||
|
@ -271,10 +303,13 @@ func Test_Hostname(t *testing.T) {
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
req, _ := http.NewRequest("GET", "http://google.com/test", nil)
|
req, _ := http.NewRequest("GET", "http://google.com/test", nil)
|
||||||
_, err := app.Test(req)
|
resp, err := app.Test(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf(`%s: %s`, t.Name(), err)
|
t.Fatalf(`%s: %s`, t.Name(), err)
|
||||||
}
|
}
|
||||||
|
if resp.StatusCode != 200 {
|
||||||
|
t.Fatalf(`%s: StatusCode %v`, t.Name(), resp.StatusCode)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
func Test_IP(t *testing.T) {
|
func Test_IP(t *testing.T) {
|
||||||
app := New()
|
app := New()
|
||||||
|
@ -287,10 +322,13 @@ func Test_IP(t *testing.T) {
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
req, _ := http.NewRequest("GET", "http://google.com/test", nil)
|
req, _ := http.NewRequest("GET", "http://google.com/test", nil)
|
||||||
_, err := app.Test(req)
|
resp, err := app.Test(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf(`%s: %s`, t.Name(), err)
|
t.Fatalf(`%s: %s`, t.Name(), err)
|
||||||
}
|
}
|
||||||
|
if resp.StatusCode != 200 {
|
||||||
|
t.Fatalf(`%s: StatusCode %v`, t.Name(), resp.StatusCode)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
func Test_IPs(t *testing.T) {
|
func Test_IPs(t *testing.T) {
|
||||||
app := New()
|
app := New()
|
||||||
|
@ -304,10 +342,13 @@ func Test_IPs(t *testing.T) {
|
||||||
})
|
})
|
||||||
req, _ := http.NewRequest("GET", "/test", nil)
|
req, _ := http.NewRequest("GET", "/test", nil)
|
||||||
req.Header.Set("X-Forwarded-For", "0.0.0.0, 1.1.1.1")
|
req.Header.Set("X-Forwarded-For", "0.0.0.0, 1.1.1.1")
|
||||||
_, err := app.Test(req)
|
resp, err := app.Test(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf(`%s: %s`, t.Name(), err)
|
t.Fatalf(`%s: %s`, t.Name(), err)
|
||||||
}
|
}
|
||||||
|
if resp.StatusCode != 200 {
|
||||||
|
t.Fatalf(`%s: StatusCode %v`, t.Name(), resp.StatusCode)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
func Test_Is(t *testing.T) {
|
func Test_Is(t *testing.T) {
|
||||||
app := New()
|
app := New()
|
||||||
|
@ -321,10 +362,13 @@ func Test_Is(t *testing.T) {
|
||||||
})
|
})
|
||||||
req, _ := http.NewRequest("GET", "/test", nil)
|
req, _ := http.NewRequest("GET", "/test", nil)
|
||||||
req.Header.Set("Content-Type", "text/html")
|
req.Header.Set("Content-Type", "text/html")
|
||||||
_, err := app.Test(req)
|
resp, err := app.Test(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf(`%s: %s`, t.Name(), err)
|
t.Fatalf(`%s: %s`, t.Name(), err)
|
||||||
}
|
}
|
||||||
|
if resp.StatusCode != 200 {
|
||||||
|
t.Fatalf(`%s: StatusCode %v`, t.Name(), resp.StatusCode)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
func Test_Locals(t *testing.T) {
|
func Test_Locals(t *testing.T) {
|
||||||
app := New()
|
app := New()
|
||||||
|
@ -340,10 +384,13 @@ func Test_Locals(t *testing.T) {
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
req, _ := http.NewRequest("GET", "/test", nil)
|
req, _ := http.NewRequest("GET", "/test", nil)
|
||||||
_, err := app.Test(req)
|
resp, err := app.Test(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf(`%s: %s`, t.Name(), err)
|
t.Fatalf(`%s: %s`, t.Name(), err)
|
||||||
}
|
}
|
||||||
|
if resp.StatusCode != 200 {
|
||||||
|
t.Fatalf(`%s: StatusCode %v`, t.Name(), resp.StatusCode)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
func Test_Method(t *testing.T) {
|
func Test_Method(t *testing.T) {
|
||||||
app := New()
|
app := New()
|
||||||
|
@ -369,20 +416,29 @@ func Test_Method(t *testing.T) {
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
req, _ := http.NewRequest("GET", "/test", nil)
|
req, _ := http.NewRequest("GET", "/test", nil)
|
||||||
_, err := app.Test(req)
|
resp, err := app.Test(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf(`%s: %s`, t.Name(), err)
|
t.Fatalf(`%s: %s`, t.Name(), err)
|
||||||
}
|
}
|
||||||
|
if resp.StatusCode != 200 {
|
||||||
|
t.Fatalf(`%s: StatusCode %v`, t.Name(), resp.StatusCode)
|
||||||
|
}
|
||||||
req, _ = http.NewRequest("POST", "/test", nil)
|
req, _ = http.NewRequest("POST", "/test", nil)
|
||||||
_, err = app.Test(req)
|
resp, err = app.Test(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf(`%s: %s`, t.Name(), err)
|
t.Fatalf(`%s: %s`, t.Name(), err)
|
||||||
}
|
}
|
||||||
|
if resp.StatusCode != 200 {
|
||||||
|
t.Fatalf(`%s: StatusCode %v`, t.Name(), resp.StatusCode)
|
||||||
|
}
|
||||||
req, _ = http.NewRequest("PUT", "/test", nil)
|
req, _ = http.NewRequest("PUT", "/test", nil)
|
||||||
_, err = app.Test(req)
|
resp, err = app.Test(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf(`%s: %s`, t.Name(), err)
|
t.Fatalf(`%s: %s`, t.Name(), err)
|
||||||
}
|
}
|
||||||
|
if resp.StatusCode != 200 {
|
||||||
|
t.Fatalf(`%s: StatusCode %v`, t.Name(), resp.StatusCode)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
func Test_MultipartForm(t *testing.T) {
|
func Test_MultipartForm(t *testing.T) {
|
||||||
app := New()
|
app := New()
|
||||||
|
@ -407,10 +463,13 @@ func Test_MultipartForm(t *testing.T) {
|
||||||
|
|
||||||
req.Header.Set("Content-Type", contentType)
|
req.Header.Set("Content-Type", contentType)
|
||||||
req.Header.Set("Content-Length", strconv.Itoa(len(body.Bytes())))
|
req.Header.Set("Content-Length", strconv.Itoa(len(body.Bytes())))
|
||||||
_, err := app.Test(req)
|
resp, err := app.Test(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf(`%s: %s`, t.Name(), err)
|
t.Fatalf(`%s: %s`, t.Name(), err)
|
||||||
}
|
}
|
||||||
|
if resp.StatusCode != 200 {
|
||||||
|
t.Fatalf(`%s: StatusCode %v`, t.Name(), resp.StatusCode)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
func Test_OriginalURL(t *testing.T) {
|
func Test_OriginalURL(t *testing.T) {
|
||||||
app := New()
|
app := New()
|
||||||
|
@ -423,10 +482,13 @@ func Test_OriginalURL(t *testing.T) {
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
req, _ := http.NewRequest("GET", "http://google.com/test?search=demo", nil)
|
req, _ := http.NewRequest("GET", "http://google.com/test?search=demo", nil)
|
||||||
_, err := app.Test(req)
|
resp, err := app.Test(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf(`%s: %s`, t.Name(), err)
|
t.Fatalf(`%s: %s`, t.Name(), err)
|
||||||
}
|
}
|
||||||
|
if resp.StatusCode != 200 {
|
||||||
|
t.Fatalf(`%s: StatusCode %v`, t.Name(), resp.StatusCode)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
func Test_Params(t *testing.T) {
|
func Test_Params(t *testing.T) {
|
||||||
app := New()
|
app := New()
|
||||||
|
@ -445,15 +507,21 @@ func Test_Params(t *testing.T) {
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
req, _ := http.NewRequest("GET", "/test/john", nil)
|
req, _ := http.NewRequest("GET", "/test/john", nil)
|
||||||
_, err := app.Test(req)
|
resp, err := app.Test(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf(`%s: %s`, t.Name(), err)
|
t.Fatalf(`%s: %s`, t.Name(), err)
|
||||||
}
|
}
|
||||||
|
if resp.StatusCode != 200 {
|
||||||
|
t.Fatalf(`%s: StatusCode %v`, t.Name(), resp.StatusCode)
|
||||||
|
}
|
||||||
req, _ = http.NewRequest("GET", "/test2/im/a/cookie", nil)
|
req, _ = http.NewRequest("GET", "/test2/im/a/cookie", nil)
|
||||||
_, err = app.Test(req)
|
resp, err = app.Test(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf(`%s: %s`, t.Name(), err)
|
t.Fatalf(`%s: %s`, t.Name(), err)
|
||||||
}
|
}
|
||||||
|
if resp.StatusCode != 200 {
|
||||||
|
t.Fatalf(`%s: StatusCode %v`, t.Name(), resp.StatusCode)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
func Test_Path(t *testing.T) {
|
func Test_Path(t *testing.T) {
|
||||||
app := New()
|
app := New()
|
||||||
|
@ -465,10 +533,13 @@ func Test_Path(t *testing.T) {
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
req, _ := http.NewRequest("GET", "/test/john", nil)
|
req, _ := http.NewRequest("GET", "/test/john", nil)
|
||||||
_, err := app.Test(req)
|
resp, err := app.Test(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf(`%s: %s`, t.Name(), err)
|
t.Fatalf(`%s: %s`, t.Name(), err)
|
||||||
}
|
}
|
||||||
|
if resp.StatusCode != 200 {
|
||||||
|
t.Fatalf(`%s: StatusCode %v`, t.Name(), resp.StatusCode)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
func Test_Query(t *testing.T) {
|
func Test_Query(t *testing.T) {
|
||||||
app := New()
|
app := New()
|
||||||
|
@ -485,10 +556,13 @@ func Test_Query(t *testing.T) {
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
req, _ := http.NewRequest("GET", "/test?search=john&age=20", nil)
|
req, _ := http.NewRequest("GET", "/test?search=john&age=20", nil)
|
||||||
_, err := app.Test(req)
|
resp, err := app.Test(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf(`%s: %s`, t.Name(), err)
|
t.Fatalf(`%s: %s`, t.Name(), err)
|
||||||
}
|
}
|
||||||
|
if resp.StatusCode != 200 {
|
||||||
|
t.Fatalf(`%s: StatusCode %v`, t.Name(), resp.StatusCode)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
func Test_Range(t *testing.T) {
|
func Test_Range(t *testing.T) {
|
||||||
app := New()
|
app := New()
|
||||||
|
@ -511,10 +585,13 @@ func Test_Route(t *testing.T) {
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
req, _ := http.NewRequest("GET", "/test", nil)
|
req, _ := http.NewRequest("GET", "/test", nil)
|
||||||
_, err := app.Test(req)
|
resp, err := app.Test(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf(`%s: %s`, t.Name(), err)
|
t.Fatalf(`%s: %s`, t.Name(), err)
|
||||||
}
|
}
|
||||||
|
if resp.StatusCode != 200 {
|
||||||
|
t.Fatalf(`%s: StatusCode %v`, t.Name(), resp.StatusCode)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
func Test_SaveFile(t *testing.T) {
|
func Test_SaveFile(t *testing.T) {
|
||||||
// TODO
|
// TODO
|
||||||
|
@ -529,10 +606,13 @@ func Test_Secure(t *testing.T) {
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
req, _ := http.NewRequest("GET", "/test", nil)
|
req, _ := http.NewRequest("GET", "/test", nil)
|
||||||
_, err := app.Test(req)
|
resp, err := app.Test(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf(`%s: %s`, t.Name(), err)
|
t.Fatalf(`%s: %s`, t.Name(), err)
|
||||||
}
|
}
|
||||||
|
if resp.StatusCode != 200 {
|
||||||
|
t.Fatalf(`%s: StatusCode %v`, t.Name(), resp.StatusCode)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
func Test_SignedCookies(t *testing.T) {
|
func Test_SignedCookies(t *testing.T) {
|
||||||
app := New()
|
app := New()
|
||||||
|
@ -540,10 +620,13 @@ func Test_SignedCookies(t *testing.T) {
|
||||||
c.SignedCookies()
|
c.SignedCookies()
|
||||||
})
|
})
|
||||||
req, _ := http.NewRequest("GET", "/test", nil)
|
req, _ := http.NewRequest("GET", "/test", nil)
|
||||||
_, err := app.Test(req)
|
resp, err := app.Test(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf(`%s: %s`, t.Name(), err)
|
t.Fatalf(`%s: %s`, t.Name(), err)
|
||||||
}
|
}
|
||||||
|
if resp.StatusCode != 200 {
|
||||||
|
t.Fatalf(`%s: StatusCode %v`, t.Name(), resp.StatusCode)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
func Test_Stale(t *testing.T) {
|
func Test_Stale(t *testing.T) {
|
||||||
app := New()
|
app := New()
|
||||||
|
@ -566,10 +649,13 @@ func Test_Subdomains(t *testing.T) {
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
req, _ := http.NewRequest("GET", "http://john.doe.google.com/test", nil)
|
req, _ := http.NewRequest("GET", "http://john.doe.google.com/test", nil)
|
||||||
_, err := app.Test(req)
|
resp, err := app.Test(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf(`%s: %s`, t.Name(), err)
|
t.Fatalf(`%s: %s`, t.Name(), err)
|
||||||
}
|
}
|
||||||
|
if resp.StatusCode != 200 {
|
||||||
|
t.Fatalf(`%s: StatusCode %v`, t.Name(), resp.StatusCode)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
func Test_XHR(t *testing.T) {
|
func Test_XHR(t *testing.T) {
|
||||||
app := New()
|
app := New()
|
||||||
|
@ -583,8 +669,11 @@ func Test_XHR(t *testing.T) {
|
||||||
})
|
})
|
||||||
req, _ := http.NewRequest("GET", "/test", nil)
|
req, _ := http.NewRequest("GET", "/test", nil)
|
||||||
req.Header.Set("X-Requested-With", "XMLHttpRequest")
|
req.Header.Set("X-Requested-With", "XMLHttpRequest")
|
||||||
_, err := app.Test(req)
|
resp, err := app.Test(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf(`%s: %s`, t.Name(), err)
|
t.Fatalf(`%s: %s`, t.Name(), err)
|
||||||
}
|
}
|
||||||
|
if resp.StatusCode != 200 {
|
||||||
|
t.Fatalf(`%s: StatusCode %v`, t.Name(), resp.StatusCode)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,7 +2,6 @@ package fiber
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -13,11 +12,14 @@ func Test_Append(t *testing.T) {
|
||||||
c.Append("X-Test", "lo", "world")
|
c.Append("X-Test", "lo", "world")
|
||||||
})
|
})
|
||||||
req, _ := http.NewRequest("GET", "/test", nil)
|
req, _ := http.NewRequest("GET", "/test", nil)
|
||||||
res, err := app.Test(req)
|
resp, err := app.Test(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf(`%s: %s`, t.Name(), err)
|
t.Fatalf(`%s: %s`, t.Name(), err)
|
||||||
}
|
}
|
||||||
if !strings.Contains(res, "X-Test: hel, lo, world") {
|
if resp.StatusCode != 200 {
|
||||||
|
t.Fatalf(`%s: StatusCode %v`, t.Name(), resp.StatusCode)
|
||||||
|
}
|
||||||
|
if resp.Header.Get("X-Test") != "hel, lo, world" {
|
||||||
t.Fatalf(`%s: Expecting %s`, t.Name(), "X-Test: hel, lo, world")
|
t.Fatalf(`%s: Expecting %s`, t.Name(), "X-Test: hel, lo, world")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
132
utils.go
132
utils.go
|
@ -8,6 +8,7 @@
|
||||||
package fiber
|
package fiber
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bufio"
|
||||||
"bytes"
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
@ -21,8 +22,6 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
"unsafe"
|
"unsafe"
|
||||||
|
|
||||||
"github.com/valyala/fasthttp"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func getParams(path string) (params []string) {
|
func getParams(path string) (params []string) {
|
||||||
|
@ -77,6 +76,9 @@ func getFiles(root string) (files []string, isDir bool, err error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func getType(ext string) (mime string) {
|
func getType(ext string) (mime string) {
|
||||||
|
if ext == "" {
|
||||||
|
return mime
|
||||||
|
}
|
||||||
if ext[0] == '.' {
|
if ext[0] == '.' {
|
||||||
ext = ext[1:]
|
ext = ext[1:]
|
||||||
}
|
}
|
||||||
|
@ -109,108 +111,68 @@ func getBytes(s string) (b []byte) {
|
||||||
return b
|
return b
|
||||||
}
|
}
|
||||||
|
|
||||||
// FakeRequest is the same as Test
|
// Test takes a http.Request and execute a fake connection to the application
|
||||||
func (r *Fiber) FakeRequest(req interface{}) (string, error) {
|
// It returns a http.Response when the connection was successfull
|
||||||
return r.Test(req)
|
func (r *Fiber) Test(req *http.Request) (*http.Response, error) {
|
||||||
}
|
// Get raw http request
|
||||||
|
reqRaw, err := httputil.DumpRequest(req, true)
|
||||||
// Test creates a readWriter and calls ServeConn on local servver
|
if err != nil {
|
||||||
func (r *Fiber) Test(req interface{}) (string, error) {
|
return nil, err
|
||||||
raw := ""
|
|
||||||
switch r := req.(type) {
|
|
||||||
case string:
|
|
||||||
raw = r
|
|
||||||
case *http.Request:
|
|
||||||
d, err := httputil.DumpRequest(r, true)
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
raw = getString(d)
|
|
||||||
}
|
}
|
||||||
server := &fasthttp.Server{
|
// Setup a fiber server struct
|
||||||
Handler: r.handler,
|
server := r.setupServer()
|
||||||
Name: r.Server,
|
// Create fake connection
|
||||||
Concurrency: r.Engine.Concurrency,
|
conn := &conn{}
|
||||||
DisableKeepalive: r.Engine.DisableKeepAlive,
|
// Pass HTTP request to conn
|
||||||
ReadBufferSize: r.Engine.ReadBufferSize,
|
conn.r.Write(reqRaw)
|
||||||
WriteBufferSize: r.Engine.WriteBufferSize,
|
// Serve conn to server
|
||||||
ReadTimeout: r.Engine.ReadTimeout,
|
channel := make(chan error)
|
||||||
WriteTimeout: r.Engine.WriteTimeout,
|
|
||||||
IdleTimeout: r.Engine.IdleTimeout,
|
|
||||||
MaxConnsPerIP: r.Engine.MaxConnsPerIP,
|
|
||||||
MaxRequestsPerConn: r.Engine.MaxRequestsPerConn,
|
|
||||||
TCPKeepalive: r.Engine.TCPKeepalive,
|
|
||||||
TCPKeepalivePeriod: r.Engine.TCPKeepalivePeriod,
|
|
||||||
MaxRequestBodySize: r.Engine.MaxRequestBodySize,
|
|
||||||
ReduceMemoryUsage: r.Engine.ReduceMemoryUsage,
|
|
||||||
GetOnly: r.Engine.GetOnly,
|
|
||||||
DisableHeaderNamesNormalizing: r.Engine.DisableHeaderNamesNormalizing,
|
|
||||||
SleepWhenConcurrencyLimitsExceeded: r.Engine.SleepWhenConcurrencyLimitsExceeded,
|
|
||||||
NoDefaultServerHeader: r.Server == "",
|
|
||||||
NoDefaultContentType: r.Engine.NoDefaultContentType,
|
|
||||||
KeepHijackedConns: r.Engine.KeepHijackedConns,
|
|
||||||
}
|
|
||||||
rw := &readWriter{}
|
|
||||||
rw.r.WriteString(raw)
|
|
||||||
|
|
||||||
ch := make(chan error)
|
|
||||||
go func() {
|
go func() {
|
||||||
ch <- server.ServeConn(rw)
|
channel <- server.ServeConn(conn)
|
||||||
}()
|
}()
|
||||||
|
// Wait for callback
|
||||||
select {
|
select {
|
||||||
case err := <-ch:
|
case err := <-channel:
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
// Throw timeout error after 200ms
|
||||||
case <-time.After(200 * time.Millisecond):
|
case <-time.After(200 * time.Millisecond):
|
||||||
return "", fmt.Errorf("Timeout")
|
return nil, fmt.Errorf("Timeout")
|
||||||
}
|
}
|
||||||
|
// Get raw HTTP response
|
||||||
err := server.ServeConn(rw)
|
respRaw, err := ioutil.ReadAll(&conn.w)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return nil, err
|
||||||
}
|
}
|
||||||
resp, err := ioutil.ReadAll(&rw.w)
|
// Create buffer
|
||||||
|
reader := strings.NewReader(getString(respRaw))
|
||||||
|
buffer := bufio.NewReader(reader)
|
||||||
|
// Convert raw HTTP response to http.Response
|
||||||
|
resp, err := http.ReadResponse(buffer, req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return nil, err
|
||||||
}
|
}
|
||||||
return getString(resp), nil
|
// Return *http.Response
|
||||||
|
return resp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Readwriter for test cases
|
// https://golang.org/src/net/net.go#L113
|
||||||
type readWriter struct {
|
type conn struct {
|
||||||
net.Conn
|
net.Conn
|
||||||
r bytes.Buffer
|
r bytes.Buffer
|
||||||
w bytes.Buffer
|
w bytes.Buffer
|
||||||
}
|
}
|
||||||
|
|
||||||
func (rw *readWriter) Close() error {
|
func (c *conn) RemoteAddr() net.Addr {
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (rw *readWriter) Read(b []byte) (int, error) {
|
|
||||||
return rw.r.Read(b)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (rw *readWriter) Write(b []byte) (int, error) {
|
|
||||||
return rw.w.Write(b)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (rw *readWriter) RemoteAddr() net.Addr {
|
|
||||||
return &net.TCPAddr{
|
return &net.TCPAddr{
|
||||||
IP: net.IPv4zero,
|
IP: net.IPv4(0, 0, 0, 0),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
func (c *conn) LocalAddr() net.Addr { return c.LocalAddr() }
|
||||||
func (rw *readWriter) LocalAddr() net.Addr {
|
func (c *conn) Read(b []byte) (int, error) { return c.r.Read(b) }
|
||||||
return rw.RemoteAddr()
|
func (c *conn) Write(b []byte) (int, error) { return c.w.Write(b) }
|
||||||
}
|
func (c *conn) Close() error { return nil }
|
||||||
|
func (c *conn) SetDeadline(t time.Time) error { return nil }
|
||||||
func (rw *readWriter) SetReadDeadline(t time.Time) error {
|
func (c *conn) SetReadDeadline(t time.Time) error { return nil }
|
||||||
return nil
|
func (c *conn) SetWriteDeadline(t time.Time) error { return nil }
|
||||||
}
|
|
||||||
|
|
||||||
func (rw *readWriter) SetWriteDeadline(t time.Time) error {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in New Issue