1
0
mirror of https://github.com/gofiber/fiber.git synced 2025-02-23 14:24:11 +00:00
fiber/request_test.go

678 lines
17 KiB
Go
Raw Normal View History

2020-02-05 17:37:04 +01:00
package fiber
import (
2020-02-06 16:06:35 +01:00
"bytes"
"fmt"
"mime/multipart"
2020-02-06 03:50:13 +01:00
"net/http"
2020-02-06 21:34:36 +01:00
"net/http/httptest"
2020-02-06 03:50:13 +01:00
"net/url"
"strconv"
2020-02-05 17:37:04 +01:00
"strings"
"testing"
)
func Test_Accepts(t *testing.T) {
app := New()
2020-02-06 03:50:13 +01:00
app.Get("/test", func(c *Ctx) {
2020-02-06 21:34:36 +01:00
expect := ""
2020-02-06 03:50:13 +01:00
result := c.Accepts(expect)
2020-02-06 21:34:36 +01:00
if c.Accepts() != "" {
t.Fatalf(`Expecting %s, got %s`, expect, result)
}
expect = ".xml"
result = c.Accepts(expect)
2020-02-06 03:50:13 +01:00
if result != expect {
2020-02-06 21:34:36 +01:00
t.Fatalf(`Expecting %s, got %s`, expect, result)
2020-02-05 17:37:04 +01:00
}
})
2020-02-06 21:34:36 +01:00
req := httptest.NewRequest("GET", "/test", nil)
2020-02-06 03:50:13 +01:00
req.Header.Set("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
2020-02-06 21:34:36 +01:00
resp, err := app.Test(req)
2020-02-06 03:50:13 +01:00
if err != nil {
t.Fatalf(`%s: %s`, t.Name(), err)
2020-02-05 17:37:04 +01:00
}
2020-02-06 21:34:36 +01:00
if resp.StatusCode != 200 {
t.Fatalf(`%s: StatusCode %v`, t.Name(), resp.StatusCode)
}
2020-02-05 17:37:04 +01:00
}
func Test_AcceptsCharsets(t *testing.T) {
app := New()
2020-02-06 03:50:13 +01:00
app.Get("/test", func(c *Ctx) {
2020-02-06 16:24:09 +01:00
c.AcceptsCharsets()
2020-02-06 03:50:13 +01:00
expect := "utf-8"
result := c.AcceptsCharsets(expect)
if result != expect {
t.Fatalf(`%s: Expecting %s, got %s`, t.Name(), expect, result)
2020-02-05 17:37:04 +01:00
}
})
2020-02-06 03:50:13 +01:00
req, _ := http.NewRequest("GET", "/test", nil)
req.Header.Set("Accept-Charset", "utf-8, iso-8859-1;q=0.5")
2020-02-06 21:34:36 +01:00
resp, err := app.Test(req)
2020-02-06 03:50:13 +01:00
if err != nil {
t.Fatalf(`%s: %s`, t.Name(), err)
2020-02-05 17:37:04 +01:00
}
2020-02-06 21:34:36 +01:00
if resp.StatusCode != 200 {
t.Fatalf(`%s: StatusCode %v`, t.Name(), resp.StatusCode)
}
2020-02-05 17:37:04 +01:00
}
func Test_AcceptsEncodings(t *testing.T) {
app := New()
2020-02-06 03:50:13 +01:00
app.Get("/test", func(c *Ctx) {
2020-02-06 16:24:09 +01:00
c.AcceptsEncodings()
2020-02-06 03:50:13 +01:00
expect := "gzip"
result := c.AcceptsEncodings(expect)
if result != expect {
t.Fatalf(`%s: Expecting %s, got %s`, t.Name(), expect, result)
2020-02-05 17:37:04 +01:00
}
})
2020-02-06 03:50:13 +01:00
req, _ := http.NewRequest("GET", "/test", nil)
req.Header.Set("Accept-Encoding", "deflate, gzip;q=1.0, *;q=0.5")
2020-02-06 21:34:36 +01:00
resp, err := app.Test(req)
2020-02-06 03:50:13 +01:00
if err != nil {
t.Fatalf(`%s: %s`, t.Name(), err)
2020-02-05 17:37:04 +01:00
}
2020-02-06 21:34:36 +01:00
if resp.StatusCode != 200 {
t.Fatalf(`%s: StatusCode %v`, t.Name(), resp.StatusCode)
}
2020-02-05 17:37:04 +01:00
}
func Test_AcceptsLanguages(t *testing.T) {
app := New()
2020-02-06 03:50:13 +01:00
app.Get("/test", func(c *Ctx) {
2020-02-06 16:24:09 +01:00
c.AcceptsLanguages()
2020-02-06 03:50:13 +01:00
expect := "fr"
result := c.AcceptsLanguages(expect)
if result != expect {
t.Fatalf(`%s: Expecting %s, got %s`, t.Name(), expect, result)
2020-02-05 17:37:04 +01:00
}
})
2020-02-06 03:50:13 +01:00
req, _ := http.NewRequest("GET", "/test", nil)
2020-02-06 16:06:35 +01:00
req.Header.Set("Accept-Language", "fr-CH, fr;q=0.9, en;q=0.8, de;q=0.7, *;q=0.5")
2020-02-06 21:34:36 +01:00
resp, err := app.Test(req)
2020-02-06 03:50:13 +01:00
if err != nil {
t.Fatalf(`%s: %s`, t.Name(), err)
2020-02-05 17:37:04 +01:00
}
2020-02-06 21:34:36 +01:00
if resp.StatusCode != 200 {
t.Fatalf(`%s: StatusCode %v`, t.Name(), resp.StatusCode)
}
2020-02-05 17:37:04 +01:00
}
func Test_BaseURL(t *testing.T) {
app := New()
2020-02-06 03:50:13 +01:00
app.Get("/test", func(c *Ctx) {
2020-02-06 16:24:09 +01:00
c.BaseUrl() // deprecated
2020-02-06 03:50:13 +01:00
expect := "http://google.com"
2020-02-05 17:37:04 +01:00
result := c.BaseURL()
2020-02-06 03:50:13 +01:00
if result != expect {
t.Fatalf(`%s: Expecting %s, got %s`, t.Name(), expect, result)
2020-02-05 17:37:04 +01:00
}
})
2020-02-06 03:50:13 +01:00
req, _ := http.NewRequest("GET", "http://google.com/test", nil)
2020-02-06 21:34:36 +01:00
resp, err := app.Test(req)
2020-02-06 03:50:13 +01:00
if err != nil {
t.Fatalf(`%s: %s`, t.Name(), err)
2020-02-05 17:37:04 +01:00
}
2020-02-06 21:34:36 +01:00
if resp.StatusCode != 200 {
t.Fatalf(`%s: StatusCode %v`, t.Name(), resp.StatusCode)
}
2020-02-05 17:37:04 +01:00
}
func Test_BasicAuth(t *testing.T) {
app := New()
2020-02-06 03:50:13 +01:00
app.Get("/test", func(c *Ctx) {
expect1 := "john"
expect2 := "doe"
result1, result2, _ := c.BasicAuth()
if result1 != expect1 {
t.Fatalf(`%s: Expecting %s, got %s`, t.Name(), expect1, expect1)
2020-02-05 17:37:04 +01:00
}
2020-02-06 03:50:13 +01:00
if result2 != expect2 {
t.Fatalf(`%s: Expecting %s, got %s`, t.Name(), result2, expect2)
2020-02-05 17:37:04 +01:00
}
})
2020-02-06 03:50:13 +01:00
req, _ := http.NewRequest("GET", "/test", nil)
req.SetBasicAuth("john", "doe")
2020-02-06 21:34:36 +01:00
resp, err := app.Test(req)
2020-02-06 03:50:13 +01:00
if err != nil {
t.Fatalf(`%s: %s`, t.Name(), err)
2020-02-05 17:37:04 +01:00
}
2020-02-06 21:34:36 +01:00
if resp.StatusCode != 200 {
t.Fatalf(`%s: StatusCode %v`, t.Name(), resp.StatusCode)
}
2020-02-05 17:37:04 +01:00
}
2020-02-05 22:17:35 +01:00
func Test_Body(t *testing.T) {
app := New()
app.Post("/test", func(c *Ctx) {
2020-02-06 16:24:09 +01:00
c.Body(1)
2020-02-06 03:50:13 +01:00
expect := "john=doe"
result := c.Body()
if result != expect {
t.Fatalf(`%s: Expecting %s, got %s`, t.Name(), expect, result)
2020-02-05 22:17:35 +01:00
}
2020-02-06 03:50:13 +01:00
expect = "doe"
result = c.Body("john")
if result != expect {
t.Fatalf(`%s: Expecting %s, got %s`, t.Name(), expect, result)
2020-02-05 22:17:35 +01:00
}
2020-02-06 16:24:09 +01:00
expect = "doe"
result = c.Body([]byte("john"))
if result != expect {
t.Fatalf(`%s: Expecting %s, got %s`, t.Name(), expect, result)
}
2020-02-05 22:17:35 +01:00
c.Body(func(k, v string) {
2020-02-06 03:50:13 +01:00
expect = "john"
if k != "john" {
t.Fatalf(`%s: Expecting %s, got %s`, t.Name(), expect, k)
2020-02-05 22:17:35 +01:00
}
2020-02-06 03:50:13 +01:00
expect = "doe"
if v != "doe" {
t.Fatalf(`%s: Expecting %s, got %s`, t.Name(), expect, v)
2020-02-05 22:17:35 +01:00
}
})
})
2020-02-06 03:50:13 +01:00
data := url.Values{}
data.Set("john", "doe")
req, _ := http.NewRequest("POST", "/test", strings.NewReader(data.Encode()))
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
req.Header.Add("Content-Length", strconv.Itoa(len(data.Encode())))
2020-02-06 21:34:36 +01:00
resp, err := app.Test(req)
2020-02-06 03:50:13 +01:00
if err != nil {
t.Fatalf(`%s: %s`, t.Name(), err)
2020-02-05 22:17:35 +01:00
}
2020-02-06 21:34:36 +01:00
if resp.StatusCode != 200 {
t.Fatalf(`%s: StatusCode %v`, t.Name(), resp.StatusCode)
}
2020-02-05 22:17:35 +01:00
}
func Test_Cookies(t *testing.T) {
app := New()
app.Get("/test", func(c *Ctx) {
2020-02-06 16:24:09 +01:00
c.Cookies(1)
2020-02-06 03:50:13 +01:00
expect := "john=doe"
result := c.Cookies()
if result != expect {
t.Fatalf(`%s: Expecting %s, got %s`, t.Name(), expect, result)
2020-02-05 22:17:35 +01:00
}
2020-02-06 03:50:13 +01:00
expect = "doe"
result = c.Cookies("john")
if result != expect {
t.Fatalf(`%s: Expecting %s, got %s`, t.Name(), expect, result)
2020-02-05 22:17:35 +01:00
}
2020-02-06 16:24:09 +01:00
expect = "doe"
result = c.Cookies([]byte("john"))
if result != expect {
t.Fatalf(`%s: Expecting %s, got %s`, t.Name(), expect, result)
}
2020-02-05 22:17:35 +01:00
c.Cookies(func(k, v string) {
2020-02-06 03:50:13 +01:00
expect = "john"
if k != "john" {
t.Fatalf(`%s: Expecting %s, got %s`, t.Name(), expect, k)
2020-02-05 22:17:35 +01:00
}
2020-02-06 03:50:13 +01:00
expect = "doe"
if v != "doe" {
t.Fatalf(`%s: Expecting %s, got %s`, t.Name(), expect, v)
2020-02-05 22:17:35 +01:00
}
})
})
2020-02-06 03:50:13 +01:00
req, _ := http.NewRequest("GET", "/test", nil)
req.AddCookie(&http.Cookie{Name: "john", Value: "doe"})
_, err := app.Test(req)
if err != nil {
t.Fatalf(`%s: %s`, t.Name(), err)
2020-02-05 22:17:35 +01:00
}
}
func Test_FormFile(t *testing.T) {
2020-02-06 16:06:35 +01:00
// TODO
2020-02-05 22:17:35 +01:00
}
2020-02-06 14:33:29 +01:00
func Test_FormValue(t *testing.T) {
2020-02-06 16:06:35 +01:00
app := New()
app.Post("/test", func(c *Ctx) {
expect := "john"
result := c.FormValue("name")
if result != expect {
t.Fatalf(`%s: Expecting %s, got %s`, t.Name(), expect, result)
}
})
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
2020-02-07 22:28:18 +03:00
if err := writer.WriteField("name", "john"); err != nil {
t.Fatalf(`%s: %s`, t.Name(), err)
}
2020-02-06 16:06:35 +01:00
writer.Close()
req, _ := http.NewRequest("POST", "/test", body)
contentType := fmt.Sprintf("multipart/form-data; boundary=%s", writer.Boundary())
2020-02-06 14:33:29 +01:00
2020-02-06 16:06:35 +01:00
req.Header.Set("Content-Type", contentType)
req.Header.Set("Content-Length", strconv.Itoa(len(body.Bytes())))
2020-02-06 21:34:36 +01:00
resp, err := app.Test(req)
2020-02-06 16:06:35 +01:00
if err != nil {
t.Fatalf(`%s: %s`, t.Name(), err)
}
2020-02-06 21:34:36 +01:00
if resp.StatusCode != 200 {
t.Fatalf(`%s: StatusCode %v`, t.Name(), resp.StatusCode)
}
2020-02-06 14:33:29 +01:00
}
func Test_Fresh(t *testing.T) {
2020-02-06 16:06:35 +01:00
app := New()
app.Get("/test", func(c *Ctx) {
c.Fresh()
})
req, _ := http.NewRequest("GET", "/test", nil)
_, err := app.Test(req)
if err != nil {
t.Fatalf(`%s: %s`, t.Name(), err)
}
2020-02-06 14:33:29 +01:00
}
func Test_Get(t *testing.T) {
app := New()
app.Get("/test", func(c *Ctx) {
expect := "utf-8, iso-8859-1;q=0.5"
result := c.Get("Accept-Charset")
if result != expect {
t.Fatalf(`%s: Expecting %s, got %s`, t.Name(), expect, result)
}
expect = "Monster"
2020-02-06 16:24:09 +01:00
result = c.Get("referrer")
if result != expect {
t.Fatalf(`%s: Expecting %s, got %s`, t.Name(), expect, result)
}
2020-02-06 14:33:29 +01:00
})
req, _ := http.NewRequest("GET", "/test", nil)
req.Header.Set("Accept-Charset", "utf-8, iso-8859-1;q=0.5")
req.Header.Set("Referer", "Monster")
2020-02-06 21:34:36 +01:00
resp, err := app.Test(req)
2020-02-06 14:33:29 +01:00
if err != nil {
t.Fatalf(`%s: %s`, t.Name(), err)
}
2020-02-06 21:34:36 +01:00
if resp.StatusCode != 200 {
t.Fatalf(`%s: StatusCode %v`, t.Name(), resp.StatusCode)
}
2020-02-06 14:33:29 +01:00
}
func Test_Hostname(t *testing.T) {
app := New()
app.Get("/test", func(c *Ctx) {
expect := "google.com"
result := c.Hostname()
if result != expect {
t.Fatalf(`%s: Expecting %s, got %s`, t.Name(), expect, result)
}
})
req, _ := http.NewRequest("GET", "http://google.com/test", nil)
2020-02-06 21:34:36 +01:00
resp, err := app.Test(req)
2020-02-06 14:33:29 +01:00
if err != nil {
t.Fatalf(`%s: %s`, t.Name(), err)
}
2020-02-06 21:34:36 +01:00
if resp.StatusCode != 200 {
t.Fatalf(`%s: StatusCode %v`, t.Name(), resp.StatusCode)
}
2020-02-06 14:33:29 +01:00
}
func Test_IP(t *testing.T) {
app := New()
app.Get("/test", func(c *Ctx) {
2020-02-06 16:24:09 +01:00
c.Ip() // deprecated
2020-02-06 14:33:29 +01:00
expect := "0.0.0.0"
result := c.IP()
if result != expect {
t.Fatalf(`%s: Expecting %s, got %s`, t.Name(), expect, result)
}
})
req, _ := http.NewRequest("GET", "http://google.com/test", nil)
2020-02-06 21:34:36 +01:00
resp, err := app.Test(req)
2020-02-06 14:33:29 +01:00
if err != nil {
t.Fatalf(`%s: %s`, t.Name(), err)
}
2020-02-06 21:34:36 +01:00
if resp.StatusCode != 200 {
t.Fatalf(`%s: StatusCode %v`, t.Name(), resp.StatusCode)
}
2020-02-06 14:33:29 +01:00
}
func Test_IPs(t *testing.T) {
app := New()
app.Get("/test", func(c *Ctx) {
2020-02-06 16:24:09 +01:00
c.Ips() // deprecated
2020-02-06 14:33:29 +01:00
expect := []string{"0.0.0.0", "1.1.1.1"}
result := c.IPs()
if result[0] != expect[0] && result[1] != expect[1] {
t.Fatalf(`%s: Expecting %s, got %s`, t.Name(), expect, result)
}
})
req, _ := http.NewRequest("GET", "/test", nil)
req.Header.Set("X-Forwarded-For", "0.0.0.0, 1.1.1.1")
2020-02-06 21:34:36 +01:00
resp, err := app.Test(req)
2020-02-06 14:33:29 +01:00
if err != nil {
t.Fatalf(`%s: %s`, t.Name(), err)
}
2020-02-06 21:34:36 +01:00
if resp.StatusCode != 200 {
t.Fatalf(`%s: StatusCode %v`, t.Name(), resp.StatusCode)
}
2020-02-06 14:33:29 +01:00
}
func Test_Is(t *testing.T) {
app := New()
app.Get("/test", func(c *Ctx) {
2020-02-06 16:24:09 +01:00
c.Is(".json")
2020-02-06 14:33:29 +01:00
expect := true
result := c.Is("html")
if result != expect {
t.Fatalf(`%s: Expecting %v, got %v`, t.Name(), expect, result)
}
})
req, _ := http.NewRequest("GET", "/test", nil)
req.Header.Set("Content-Type", "text/html")
2020-02-06 21:34:36 +01:00
resp, err := app.Test(req)
2020-02-06 14:33:29 +01:00
if err != nil {
t.Fatalf(`%s: %s`, t.Name(), err)
}
2020-02-06 21:34:36 +01:00
if resp.StatusCode != 200 {
t.Fatalf(`%s: StatusCode %v`, t.Name(), resp.StatusCode)
}
2020-02-06 14:33:29 +01:00
}
func Test_Locals(t *testing.T) {
app := New()
app.Use(func(c *Ctx) {
c.Locals("john", "doe")
c.Next()
})
app.Get("/test", func(c *Ctx) {
expect := "doe"
result := c.Locals("john")
if result != expect {
t.Fatalf(`%s: Expecting %s, got %s`, t.Name(), expect, result)
}
})
req, _ := http.NewRequest("GET", "/test", nil)
2020-02-06 21:34:36 +01:00
resp, err := app.Test(req)
2020-02-06 14:33:29 +01:00
if err != nil {
t.Fatalf(`%s: %s`, t.Name(), err)
}
2020-02-06 21:34:36 +01:00
if resp.StatusCode != 200 {
t.Fatalf(`%s: StatusCode %v`, t.Name(), resp.StatusCode)
}
2020-02-06 14:33:29 +01:00
}
func Test_Method(t *testing.T) {
app := New()
app.Get(func(c *Ctx) {
expect := "GET"
result := c.Method()
if result != expect {
t.Fatalf(`%s: Expecting %s, got %s`, t.Name(), expect, result)
}
})
app.Post(func(c *Ctx) {
expect := "POST"
result := c.Method()
if result != expect {
t.Fatalf(`%s: Expecting %s, got %s`, t.Name(), expect, result)
}
})
app.Put(func(c *Ctx) {
expect := "PUT"
result := c.Method()
if result != expect {
t.Fatalf(`%s: Expecting %s, got %s`, t.Name(), expect, result)
}
})
req, _ := http.NewRequest("GET", "/test", nil)
2020-02-06 21:34:36 +01:00
resp, err := app.Test(req)
2020-02-06 14:33:29 +01:00
if err != nil {
t.Fatalf(`%s: %s`, t.Name(), err)
}
2020-02-06 21:34:36 +01:00
if resp.StatusCode != 200 {
t.Fatalf(`%s: StatusCode %v`, t.Name(), resp.StatusCode)
}
2020-02-06 14:33:29 +01:00
req, _ = http.NewRequest("POST", "/test", nil)
2020-02-06 21:34:36 +01:00
resp, err = app.Test(req)
2020-02-06 14:33:29 +01:00
if err != nil {
t.Fatalf(`%s: %s`, t.Name(), err)
}
2020-02-06 21:34:36 +01:00
if resp.StatusCode != 200 {
t.Fatalf(`%s: StatusCode %v`, t.Name(), resp.StatusCode)
}
2020-02-06 14:33:29 +01:00
req, _ = http.NewRequest("PUT", "/test", nil)
2020-02-06 21:34:36 +01:00
resp, err = app.Test(req)
2020-02-06 14:33:29 +01:00
if err != nil {
t.Fatalf(`%s: %s`, t.Name(), err)
}
2020-02-06 21:34:36 +01:00
if resp.StatusCode != 200 {
t.Fatalf(`%s: StatusCode %v`, t.Name(), resp.StatusCode)
}
2020-02-06 14:33:29 +01:00
}
func Test_MultipartForm(t *testing.T) {
2020-02-06 16:06:35 +01:00
app := New()
app.Post("/test", func(c *Ctx) {
expect := "john"
result, err := c.MultipartForm()
if err != nil {
t.Fatalf(`%s: Expecting %s, got %s`, t.Name(), expect, err)
}
if result.Value["name"][0] != expect {
t.Fatalf(`%s: Expecting %s, got %v`, t.Name(), expect, result)
}
})
2020-02-06 14:33:29 +01:00
2020-02-06 16:06:35 +01:00
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
2020-02-07 22:28:18 +03:00
if err := writer.WriteField("name", "john"); err != nil {
t.Fatalf(`%s: %s`, t.Name(), err)
}
2020-02-06 16:06:35 +01:00
writer.Close()
req, _ := http.NewRequest("POST", "/test", body)
contentType := fmt.Sprintf("multipart/form-data; boundary=%s", writer.Boundary())
req.Header.Set("Content-Type", contentType)
req.Header.Set("Content-Length", strconv.Itoa(len(body.Bytes())))
2020-02-06 21:34:36 +01:00
resp, err := app.Test(req)
2020-02-06 16:06:35 +01:00
if err != nil {
t.Fatalf(`%s: %s`, t.Name(), err)
}
2020-02-06 21:34:36 +01:00
if resp.StatusCode != 200 {
t.Fatalf(`%s: StatusCode %v`, t.Name(), resp.StatusCode)
}
2020-02-06 14:33:29 +01:00
}
func Test_OriginalURL(t *testing.T) {
app := New()
app.Get("/test", func(c *Ctx) {
2020-02-06 16:24:09 +01:00
c.OriginalUrl() // deprecated
2020-02-06 14:33:29 +01:00
expect := "/test?search=demo"
result := c.OriginalURL()
if result != expect {
t.Fatalf(`%s: Expecting %s, got %s`, t.Name(), expect, result)
}
})
req, _ := http.NewRequest("GET", "http://google.com/test?search=demo", nil)
2020-02-06 21:34:36 +01:00
resp, err := app.Test(req)
2020-02-06 14:33:29 +01:00
if err != nil {
t.Fatalf(`%s: %s`, t.Name(), err)
}
2020-02-06 21:34:36 +01:00
if resp.StatusCode != 200 {
t.Fatalf(`%s: StatusCode %v`, t.Name(), resp.StatusCode)
}
2020-02-06 14:33:29 +01:00
}
func Test_Params(t *testing.T) {
app := New()
app.Get("/test/:user", func(c *Ctx) {
expect := "john"
result := c.Params("user")
if result != expect {
t.Fatalf(`%s: Expecting %s, got %s`, t.Name(), expect, result)
}
})
app.Get("/test2/*", func(c *Ctx) {
expect := "im/a/cookie"
result := c.Params("*")
if result != expect {
t.Fatalf(`%s: Expecting %s, got %s`, t.Name(), expect, result)
}
})
req, _ := http.NewRequest("GET", "/test/john", nil)
2020-02-06 21:34:36 +01:00
resp, err := app.Test(req)
2020-02-06 14:33:29 +01:00
if err != nil {
t.Fatalf(`%s: %s`, t.Name(), err)
}
2020-02-06 21:34:36 +01:00
if resp.StatusCode != 200 {
t.Fatalf(`%s: StatusCode %v`, t.Name(), resp.StatusCode)
}
2020-02-06 14:33:29 +01:00
req, _ = http.NewRequest("GET", "/test2/im/a/cookie", nil)
2020-02-06 21:34:36 +01:00
resp, err = app.Test(req)
2020-02-06 14:33:29 +01:00
if err != nil {
t.Fatalf(`%s: %s`, t.Name(), err)
}
2020-02-06 21:34:36 +01:00
if resp.StatusCode != 200 {
t.Fatalf(`%s: StatusCode %v`, t.Name(), resp.StatusCode)
}
2020-02-06 14:33:29 +01:00
}
func Test_Path(t *testing.T) {
app := New()
app.Get("/test/:user", func(c *Ctx) {
expect := "/test/john"
result := c.Path()
if result != expect {
t.Fatalf(`%s: Expecting %s, got %s`, t.Name(), expect, result)
}
})
req, _ := http.NewRequest("GET", "/test/john", nil)
2020-02-06 21:34:36 +01:00
resp, err := app.Test(req)
2020-02-06 14:33:29 +01:00
if err != nil {
t.Fatalf(`%s: %s`, t.Name(), err)
}
2020-02-06 21:34:36 +01:00
if resp.StatusCode != 200 {
t.Fatalf(`%s: StatusCode %v`, t.Name(), resp.StatusCode)
}
2020-02-06 14:33:29 +01:00
}
func Test_Query(t *testing.T) {
app := New()
app.Get("/test", func(c *Ctx) {
expect := "john"
result := c.Query("search")
if result != expect {
t.Fatalf(`%s: Expecting %s, got %s`, t.Name(), expect, result)
}
expect = "20"
result = c.Query("age")
if result != expect {
t.Fatalf(`%s: Expecting %s, got %s`, t.Name(), expect, result)
}
})
req, _ := http.NewRequest("GET", "/test?search=john&age=20", nil)
2020-02-06 21:34:36 +01:00
resp, err := app.Test(req)
2020-02-06 14:33:29 +01:00
if err != nil {
t.Fatalf(`%s: %s`, t.Name(), err)
}
2020-02-06 21:34:36 +01:00
if resp.StatusCode != 200 {
t.Fatalf(`%s: StatusCode %v`, t.Name(), resp.StatusCode)
}
2020-02-06 14:33:29 +01:00
}
func Test_Range(t *testing.T) {
2020-02-06 16:06:35 +01:00
app := New()
app.Get("/test", func(c *Ctx) {
c.Range()
})
req, _ := http.NewRequest("GET", "/test", nil)
_, err := app.Test(req)
if err != nil {
t.Fatalf(`%s: %s`, t.Name(), err)
}
2020-02-06 14:33:29 +01:00
}
func Test_Route(t *testing.T) {
2020-02-06 16:06:35 +01:00
app := New()
app.Get("/test", func(c *Ctx) {
expect := "/test"
result := c.Route().Path
if result != expect {
t.Fatalf(`%s: Expecting %s, got %s`, t.Name(), expect, result)
}
})
req, _ := http.NewRequest("GET", "/test", nil)
2020-02-06 21:34:36 +01:00
resp, err := app.Test(req)
2020-02-06 16:06:35 +01:00
if err != nil {
t.Fatalf(`%s: %s`, t.Name(), err)
}
2020-02-06 21:34:36 +01:00
if resp.StatusCode != 200 {
t.Fatalf(`%s: StatusCode %v`, t.Name(), resp.StatusCode)
}
2020-02-06 14:33:29 +01:00
}
func Test_SaveFile(t *testing.T) {
2020-02-06 16:06:35 +01:00
// TODO
2020-02-06 14:33:29 +01:00
}
func Test_Secure(t *testing.T) {
app := New()
app.Get("/test", func(c *Ctx) {
expect := false
result := c.Secure()
if result != expect {
t.Fatalf(`%s: Expecting %v, got %v`, t.Name(), expect, result)
}
})
req, _ := http.NewRequest("GET", "/test", nil)
2020-02-06 21:34:36 +01:00
resp, err := app.Test(req)
2020-02-06 14:33:29 +01:00
if err != nil {
t.Fatalf(`%s: %s`, t.Name(), err)
}
2020-02-06 21:34:36 +01:00
if resp.StatusCode != 200 {
t.Fatalf(`%s: StatusCode %v`, t.Name(), resp.StatusCode)
}
2020-02-06 14:33:29 +01:00
}
func Test_SignedCookies(t *testing.T) {
2020-02-06 16:06:35 +01:00
app := New()
app.Get("/test", func(c *Ctx) {
c.SignedCookies()
})
req, _ := http.NewRequest("GET", "/test", nil)
2020-02-06 21:34:36 +01:00
resp, err := app.Test(req)
2020-02-06 16:06:35 +01:00
if err != nil {
t.Fatalf(`%s: %s`, t.Name(), err)
}
2020-02-06 21:34:36 +01:00
if resp.StatusCode != 200 {
t.Fatalf(`%s: StatusCode %v`, t.Name(), resp.StatusCode)
}
2020-02-06 14:33:29 +01:00
}
func Test_Stale(t *testing.T) {
2020-02-06 16:06:35 +01:00
app := New()
app.Get("/test", func(c *Ctx) {
c.Stale()
})
req, _ := http.NewRequest("GET", "/test", nil)
_, err := app.Test(req)
if err != nil {
t.Fatalf(`%s: %s`, t.Name(), err)
}
2020-02-06 14:33:29 +01:00
}
func Test_Subdomains(t *testing.T) {
app := New()
app.Get("/test", func(c *Ctx) {
expect := []string{"john", "doe"}
result := c.Subdomains()
if result[0] != expect[0] && result[1] != expect[1] {
t.Fatalf(`%s: Expecting %s, got %s`, t.Name(), expect, result)
}
})
req, _ := http.NewRequest("GET", "http://john.doe.google.com/test", nil)
2020-02-06 21:34:36 +01:00
resp, err := app.Test(req)
2020-02-06 14:33:29 +01:00
if err != nil {
t.Fatalf(`%s: %s`, t.Name(), err)
}
2020-02-06 21:34:36 +01:00
if resp.StatusCode != 200 {
t.Fatalf(`%s: StatusCode %v`, t.Name(), resp.StatusCode)
}
2020-02-06 14:33:29 +01:00
}
func Test_XHR(t *testing.T) {
app := New()
app.Get("/test", func(c *Ctx) {
2020-02-06 16:24:09 +01:00
c.Xhr() // deprecated
expect := true
result := c.XHR()
2020-02-06 14:33:29 +01:00
if result != expect {
2020-02-06 16:24:09 +01:00
t.Fatalf(`%s: Expecting %v, got %v`, t.Name(), expect, result)
2020-02-06 14:33:29 +01:00
}
})
req, _ := http.NewRequest("GET", "/test", nil)
req.Header.Set("X-Requested-With", "XMLHttpRequest")
2020-02-06 21:34:36 +01:00
resp, err := app.Test(req)
2020-02-06 14:33:29 +01:00
if err != nil {
t.Fatalf(`%s: %s`, t.Name(), err)
}
2020-02-06 21:34:36 +01:00
if resp.StatusCode != 200 {
t.Fatalf(`%s: StatusCode %v`, t.Name(), resp.StatusCode)
}
2020-02-06 14:33:29 +01:00
}