Merge pull request #64 from Fenny/master

Add tests for 85.1% coverage
pull/66/head
Fenny 2020-02-06 10:32:26 -05:00 committed by GitHub
commit 91ec284a42
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 38 additions and 7 deletions

View File

@ -132,6 +132,12 @@ func (ctx *Ctx) AcceptsLanguages(offers ...string) string {
return ""
}
// BaseUrl : https://gofiber.github.io/fiber/#/context?id=baseurl
func (ctx *Ctx) BaseUrl() string {
fmt.Println("Fiber deprecated c.BaseUrl(), this will be removed in v2: Use c.BaseURL() instead")
return ctx.BaseURL()
}
// BaseURL : https://gofiber.github.io/fiber/#/context?id=baseurl
func (ctx *Ctx) BaseURL() string {
return ctx.Protocol() + "://" + ctx.Hostname()
@ -314,10 +320,6 @@ func (ctx *Ctx) OriginalURL() string {
// Params : https://gofiber.github.io/fiber/#/context?id=params
func (ctx *Ctx) Params(key string) string {
if ctx.params == nil {
return ""
}
for i := 0; i < len(*ctx.params); i++ {
if (*ctx.params)[i] == key {
return ctx.values[i]

View File

@ -14,6 +14,7 @@ import (
func Test_Accepts(t *testing.T) {
app := New()
app.Get("/test", func(c *Ctx) {
c.Accepts()
expect := ".xml"
result := c.Accepts(expect)
if result != expect {
@ -30,6 +31,8 @@ func Test_Accepts(t *testing.T) {
func Test_AcceptsCharsets(t *testing.T) {
app := New()
app.Get("/test", func(c *Ctx) {
c.AcceptsCharsets()
expect := "utf-8"
result := c.AcceptsCharsets(expect)
if result != expect {
@ -46,6 +49,7 @@ func Test_AcceptsCharsets(t *testing.T) {
func Test_AcceptsEncodings(t *testing.T) {
app := New()
app.Get("/test", func(c *Ctx) {
c.AcceptsEncodings()
expect := "gzip"
result := c.AcceptsEncodings(expect)
if result != expect {
@ -62,6 +66,7 @@ func Test_AcceptsEncodings(t *testing.T) {
func Test_AcceptsLanguages(t *testing.T) {
app := New()
app.Get("/test", func(c *Ctx) {
c.AcceptsLanguages()
expect := "fr"
result := c.AcceptsLanguages(expect)
if result != expect {
@ -78,6 +83,7 @@ func Test_AcceptsLanguages(t *testing.T) {
func Test_BaseURL(t *testing.T) {
app := New()
app.Get("/test", func(c *Ctx) {
c.BaseUrl() // deprecated
expect := "http://google.com"
result := c.BaseURL()
if result != expect {
@ -113,6 +119,7 @@ func Test_BasicAuth(t *testing.T) {
func Test_Body(t *testing.T) {
app := New()
app.Post("/test", func(c *Ctx) {
c.Body(1)
expect := "john=doe"
result := c.Body()
if result != expect {
@ -123,6 +130,11 @@ func Test_Body(t *testing.T) {
if result != expect {
t.Fatalf(`%s: Expecting %s, got %s`, t.Name(), expect, result)
}
expect = "doe"
result = c.Body([]byte("john"))
if result != expect {
t.Fatalf(`%s: Expecting %s, got %s`, t.Name(), expect, result)
}
c.Body(func(k, v string) {
expect = "john"
if k != "john" {
@ -147,6 +159,7 @@ func Test_Body(t *testing.T) {
func Test_Cookies(t *testing.T) {
app := New()
app.Get("/test", func(c *Ctx) {
c.Cookies(1)
expect := "john=doe"
result := c.Cookies()
if result != expect {
@ -157,6 +170,11 @@ func Test_Cookies(t *testing.T) {
if result != expect {
t.Fatalf(`%s: Expecting %s, got %s`, t.Name(), expect, result)
}
expect = "doe"
result = c.Cookies([]byte("john"))
if result != expect {
t.Fatalf(`%s: Expecting %s, got %s`, t.Name(), expect, result)
}
c.Cookies(func(k, v string) {
expect = "john"
if k != "john" {
@ -229,9 +247,15 @@ func Test_Get(t *testing.T) {
if result != expect {
t.Fatalf(`%s: Expecting %s, got %s`, t.Name(), expect, result)
}
expect = "Cookie"
result = c.Get("referrer")
if result != expect {
t.Fatalf(`%s: Expecting %s, got %s`, t.Name(), expect, result)
}
})
req, _ := http.NewRequest("GET", "/test", nil)
req.Header.Set("Accept-Charset", "utf-8, iso-8859-1;q=0.5")
req.Header.Set("Referer", "Cookie")
_, err := app.Test(req)
if err != nil {
t.Fatalf(`%s: %s`, t.Name(), err)
@ -255,6 +279,7 @@ func Test_Hostname(t *testing.T) {
func Test_IP(t *testing.T) {
app := New()
app.Get("/test", func(c *Ctx) {
c.Ip() // deprecated
expect := "0.0.0.0"
result := c.IP()
if result != expect {
@ -270,6 +295,7 @@ func Test_IP(t *testing.T) {
func Test_IPs(t *testing.T) {
app := New()
app.Get("/test", func(c *Ctx) {
c.Ips() // deprecated
expect := []string{"0.0.0.0", "1.1.1.1"}
result := c.IPs()
if result[0] != expect[0] && result[1] != expect[1] {
@ -286,6 +312,7 @@ func Test_IPs(t *testing.T) {
func Test_Is(t *testing.T) {
app := New()
app.Get("/test", func(c *Ctx) {
c.Is(".json")
expect := true
result := c.Is("html")
if result != expect {
@ -388,6 +415,7 @@ func Test_MultipartForm(t *testing.T) {
func Test_OriginalURL(t *testing.T) {
app := New()
app.Get("/test", func(c *Ctx) {
c.OriginalUrl() // deprecated
expect := "/test?search=demo"
result := c.OriginalURL()
if result != expect {
@ -546,10 +574,11 @@ func Test_Subdomains(t *testing.T) {
func Test_XHR(t *testing.T) {
app := New()
app.Get("/test", func(c *Ctx) {
expect := "XMLHttpRequest"
result := c.Get("X-Requested-With")
c.Xhr() // deprecated
expect := true
result := c.XHR()
if result != expect {
t.Fatalf(`%s: Expecting %s, got %s`, t.Name(), expect, result)
t.Fatalf(`%s: Expecting %v, got %v`, t.Name(), expect, result)
}
})
req, _ := http.NewRequest("GET", "/test", nil)