mirror of https://github.com/gofiber/fiber.git
Add FakeRequest for testing
parent
bda065478c
commit
b2cdb7854e
2
go.mod
2
go.mod
|
@ -4,7 +4,5 @@ go 1.11
|
|||
|
||||
require (
|
||||
github.com/json-iterator/go v1.1.9
|
||||
github.com/klauspost/compress v1.9.8 // indirect
|
||||
github.com/klauspost/cpuid v1.2.3 // indirect
|
||||
github.com/valyala/fasthttp v1.9.0
|
||||
)
|
||||
|
|
|
@ -0,0 +1,151 @@
|
|||
package fiber
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func Test_Accepts(t *testing.T) {
|
||||
// Raw http request
|
||||
req := "GET / HTTP/1.1\r\nHost: localhost:8080\r\nAccept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n\r\n"
|
||||
// Create fiber app
|
||||
app := New()
|
||||
app.Get("/", func(c *Ctx) {
|
||||
expecting := "html"
|
||||
result := c.Accepts(expecting)
|
||||
if result != expecting {
|
||||
t.Fatalf(`%s: Expecting %s`, t.Name(), expecting)
|
||||
}
|
||||
|
||||
expecting = ".xml"
|
||||
result = c.Accepts(expecting)
|
||||
if result != expecting {
|
||||
t.Fatalf(`%s: Expecting %s`, t.Name(), expecting)
|
||||
}
|
||||
})
|
||||
// Send fake request
|
||||
res, err := app.FakeRequest(req)
|
||||
// Check for errors and if route was handled
|
||||
if err != nil || !strings.Contains(res, "HTTP/1.1 200 OK") {
|
||||
t.Fatalf(`%s: Error serving FakeRequest %s`, t.Name(), err)
|
||||
}
|
||||
}
|
||||
func Test_AcceptsCharsets(t *testing.T) {
|
||||
// Raw http request
|
||||
req := "GET / HTTP/1.1\r\nHost: localhost:8080\r\nAccept-Charset: utf-8, iso-8859-1;q=0.5\r\n\r\n"
|
||||
// Raw http request
|
||||
app := New()
|
||||
app.Get("/", func(c *Ctx) {
|
||||
expecting := "utf-8"
|
||||
result := c.AcceptsCharsets(expecting)
|
||||
if result != expecting {
|
||||
t.Fatalf(`%s: Expecting %s`, t.Name(), expecting)
|
||||
}
|
||||
|
||||
expecting = "iso-8859-1"
|
||||
result = c.AcceptsCharsets(expecting)
|
||||
if result != expecting {
|
||||
t.Fatalf(`%s: Expecting %s`, t.Name(), expecting)
|
||||
}
|
||||
})
|
||||
// Send fake request
|
||||
res, err := app.FakeRequest(req)
|
||||
// Check for errors and if route was handled
|
||||
if err != nil || !strings.Contains(res, "HTTP/1.1 200 OK") {
|
||||
t.Fatalf(`%s: Error serving FakeRequest %s`, t.Name(), err)
|
||||
}
|
||||
}
|
||||
func Test_AcceptsEncodings(t *testing.T) {
|
||||
// Raw http request
|
||||
req := "GET / HTTP/1.1\r\nHost: localhost:8080\r\nAccept-Encoding: deflate, gzip;q=1.0, *;q=0.5\r\n\r\n"
|
||||
// Raw http request
|
||||
app := New()
|
||||
app.Get("/", func(c *Ctx) {
|
||||
expecting := "gzip"
|
||||
result := c.AcceptsEncodings(expecting)
|
||||
if result != expecting {
|
||||
t.Fatalf(`%s: Expecting %s`, t.Name(), expecting)
|
||||
}
|
||||
|
||||
expecting = "*"
|
||||
result = c.AcceptsEncodings(expecting)
|
||||
if result != expecting {
|
||||
t.Fatalf(`%s: Expecting %s`, t.Name(), expecting)
|
||||
}
|
||||
})
|
||||
// Send fake request
|
||||
res, err := app.FakeRequest(req)
|
||||
// Check for errors and if route was handled
|
||||
if err != nil || !strings.Contains(res, "HTTP/1.1 200 OK") {
|
||||
t.Fatalf(`%s: Error serving FakeRequest %s`, t.Name(), err)
|
||||
}
|
||||
}
|
||||
func Test_AcceptsLanguages(t *testing.T) {
|
||||
// Raw http request
|
||||
req := "GET / HTTP/1.1\r\nHost: localhost:8080\r\nAccept-Language: fr-CH, fr;q=0.9, en;q=0.8, de;q=0.7, *;q=0.5\r\n\r\n"
|
||||
// Raw http request
|
||||
app := New()
|
||||
app.Get("/", func(c *Ctx) {
|
||||
expecting := "fr"
|
||||
result := c.AcceptsLanguages(expecting)
|
||||
if result != expecting {
|
||||
t.Fatalf(`%s: Expecting %s`, t.Name(), expecting)
|
||||
}
|
||||
|
||||
expecting = "en"
|
||||
result = c.AcceptsLanguages(expecting)
|
||||
if result != expecting {
|
||||
t.Fatalf(`%s: Expecting %s`, t.Name(), expecting)
|
||||
}
|
||||
})
|
||||
// Send fake request
|
||||
res, err := app.FakeRequest(req)
|
||||
// Check for errors and if route was handled
|
||||
if err != nil || !strings.Contains(res, "HTTP/1.1 200 OK") {
|
||||
t.Fatalf(`%s: Error serving FakeRequest %s`, t.Name(), err)
|
||||
}
|
||||
}
|
||||
func Test_BaseURL(t *testing.T) {
|
||||
// Raw http request
|
||||
req := "GET / HTTP/1.1\r\nHost: localhost:8080\r\n\r\n"
|
||||
// Raw http request
|
||||
app := New()
|
||||
app.Get("/", func(c *Ctx) {
|
||||
expecting := "http://localhost:8080"
|
||||
result := c.BaseURL()
|
||||
if result != expecting {
|
||||
t.Fatalf(`%s: Expecting %s`, t.Name(), expecting)
|
||||
}
|
||||
})
|
||||
// Send fake request
|
||||
res, err := app.FakeRequest(req)
|
||||
// Check for errors and if route was handled
|
||||
if err != nil || !strings.Contains(res, "HTTP/1.1 200 OK") {
|
||||
t.Fatalf(`%s: Error serving FakeRequest %s`, t.Name(), err)
|
||||
}
|
||||
}
|
||||
func Test_BasicAuth(t *testing.T) {
|
||||
// Raw http request
|
||||
req := "GET / HTTP/1.1\r\nHost: localhost:8080\r\nAuthorization: Basic am9objpkb2U=\r\n\r\n"
|
||||
// Raw http request
|
||||
app := New()
|
||||
app.Get("/", func(c *Ctx) {
|
||||
user, pass, ok := c.BasicAuth()
|
||||
if !ok {
|
||||
t.Fatalf(`%s: Expecting %s`, t.Name(), "ok")
|
||||
}
|
||||
if user != "john" || pass != "doe" {
|
||||
if !ok {
|
||||
t.Fatalf(`%s: Expecting john & doe`, t.Name())
|
||||
}
|
||||
}
|
||||
})
|
||||
// Send fake request
|
||||
res, err := app.FakeRequest(req)
|
||||
// Check for errors and if route was handled
|
||||
if err != nil || !strings.Contains(res, "HTTP/1.1 200 OK") {
|
||||
t.Fatalf(`%s: Error serving FakeRequest %s`, t.Name(), err)
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: add all functions from request.go
|
|
@ -27,7 +27,11 @@ func (ctx *Ctx) Append(field string, values ...string) {
|
|||
|
||||
h := ctx.Get(field)
|
||||
for i := range values {
|
||||
h += h + "," + values[i]
|
||||
if h == "" {
|
||||
h += values[i]
|
||||
} else {
|
||||
h += ", " + values[i]
|
||||
}
|
||||
}
|
||||
ctx.Set(field, h)
|
||||
}
|
||||
|
|
|
@ -0,0 +1,28 @@
|
|||
package fiber
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func Test_Append(t *testing.T) {
|
||||
// Raw http request
|
||||
req := "GET / HTTP/1.1\r\nHost: localhost:8080\r\n\r\n"
|
||||
// Create fiber app
|
||||
app := New()
|
||||
app.Get("/", func(c *Ctx) {
|
||||
c.Append("X-Test", "hello", "world")
|
||||
})
|
||||
// Send fake request
|
||||
res, err := app.FakeRequest(req)
|
||||
// Check for errors and if route was handled
|
||||
if err != nil || !strings.Contains(res, "HTTP/1.1 200 OK") {
|
||||
t.Fatalf(`%s: Error serving FakeRequest %s`, t.Name(), err)
|
||||
}
|
||||
// Check if function works correctly
|
||||
if !strings.Contains(res, "X-Test: hello, world") {
|
||||
t.Fatalf(`%s: Expecting %s`, t.Name(), "X-Test: hello, world")
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: add all functions from response.go
|
97
utils.go
97
utils.go
|
@ -8,12 +8,19 @@
|
|||
package fiber
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"regexp"
|
||||
"strings"
|
||||
"time"
|
||||
"unsafe"
|
||||
|
||||
"github.com/valyala/fasthttp"
|
||||
)
|
||||
|
||||
func getParams(path string) (params []string) {
|
||||
|
@ -99,3 +106,93 @@ func getBytes(s string) (b []byte) {
|
|||
bh.Data, bh.Len, bh.Cap = sh.Data, sh.Len, sh.Len
|
||||
return b
|
||||
}
|
||||
|
||||
// FakeRequest creates a readWriter and calls ServeConn on local servver
|
||||
func (r *Fiber) FakeRequest(raw string) (string, error) {
|
||||
server := &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,
|
||||
}
|
||||
rw := &readWriter{}
|
||||
rw.r.WriteString(raw)
|
||||
|
||||
ch := make(chan error)
|
||||
go func() {
|
||||
ch <- server.ServeConn(rw)
|
||||
}()
|
||||
|
||||
select {
|
||||
case err := <-ch:
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
case <-time.After(200 * time.Millisecond):
|
||||
return "", fmt.Errorf("Timeout")
|
||||
}
|
||||
|
||||
err := server.ServeConn(rw)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
resp, err := ioutil.ReadAll(&rw.w)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return getString(resp), nil
|
||||
}
|
||||
|
||||
// Readwriter for test cases
|
||||
type readWriter struct {
|
||||
net.Conn
|
||||
r bytes.Buffer
|
||||
w bytes.Buffer
|
||||
}
|
||||
|
||||
func (rw *readWriter) Close() error {
|
||||
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{
|
||||
IP: net.IPv4zero,
|
||||
}
|
||||
}
|
||||
|
||||
func (rw *readWriter) LocalAddr() net.Addr {
|
||||
return rw.RemoteAddr()
|
||||
}
|
||||
|
||||
func (rw *readWriter) SetReadDeadline(t time.Time) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (rw *readWriter) SetWriteDeadline(t time.Time) error {
|
||||
return nil
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue