Skip to content

Commit

Permalink
Fix Beego_test and add fiberEngine Test. (#86)
Browse files Browse the repository at this point in the history
  • Loading branch information
ralic authored Jun 27, 2021
1 parent 8bfe2d5 commit ec4039c
Show file tree
Hide file tree
Showing 7 changed files with 380 additions and 8 deletions.
6 changes: 3 additions & 3 deletions _example/beego_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ func TestSayHelloWorld(t *testing.T) {

// LoadAppConfig allow developer to apply a config file
// beego.LoadAppConfig("ini", "../conf/app.conf")
c := beego.NewControllerRegister()
c.Add(uri, &UserController{}, "get:SayHelloWorld")
c := beego.NewApp()
c.Handlers.Add(uri, &UserController{}, "get:SayHelloWorld")

r := gofight.New()
r.GET(uri).
SetDebug(true).
Run(c, func(rp gofight.HTTPResponse, rq gofight.HTTPRequest) {
Run(c.Handlers, func(rp gofight.HTTPResponse, rq gofight.HTTPRequest) {
fmt.Println(rp.Code)
assert.Equal(t, "Hello, World", rp.Body.String())
assert.Equal(t, http.StatusOK, rp.Code)
Expand Down
28 changes: 28 additions & 0 deletions _example/fiberEng.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package example

import (
"fmt"
"github.com/gofiber/fiber/v2"
)

// FiberEngine is fiber web server and handlers routes
func FiberEngine() *fiber.App {
// Fiber instance
e := fiber.New()
// Routes
e.Get("/helloCouple", fiberRoute)
return e
}

func fiberRoute(c *fiber.Ctx) error {
names := c.Context().QueryArgs().PeekMulti("names")
ages := c.Context().QueryArgs().PeekMulti("ages")
msg := ""
for i := range names {
msg += fmt.Sprintf("God Love the World ! 👴 %s is %s years old~\n", string(names[i]), string(ages[i]))
}
c.Status(200).SendString(msg)
// =>God Love the World ! 👴 john is 75 years old~
// God Love the World ! 👴 mary is 25 years old~
return nil
}
42 changes: 42 additions & 0 deletions _example/fiberEng_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package example

import (
"github.com/appleboy/gofight/v2"
"github.com/stretchr/testify/assert"
"net/http"
"testing"
)

func TestFiberEngine(t *testing.T) {

tests := []struct {
name string
path string
want string
}{
{
name: "TestHelloWorld",
path: "/",
want: `God Love the World ! 👴 john is 75 years old~
God Love the World ! 👴 mary is 25 years old~
`,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
r := gofight.New()
r.GET(tt.path).
SetPath("helloCouple").
SetQueryD(gofight.D{
"names": []string{"john", "mary"},
"ages": []string{"75", "25"},
}).
SetDebug(true).
RunX(FiberEngine(), func(res gofight.HTTPResponse, req gofight.HTTPRequest) {
assert.Equal(t, tt.want, res.Body.String())
assert.Equal(t, http.StatusOK, res.Code)
})
})
}
}
6 changes: 5 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,8 @@ module github.com/appleboy/gofight/v2

go 1.13

require github.com/stretchr/testify v1.4.0
require (
github.com/astaxie/beego v1.12.3 // indirect
github.com/gofiber/fiber/v2 v2.3.3
github.com/stretchr/testify v1.7.0
)
259 changes: 256 additions & 3 deletions go.sum

Large diffs are not rendered by default.

45 changes: 45 additions & 0 deletions gofight.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ import (
"bytes"
"encoding/json"
"io"
"io/ioutil"
"log"
"mime/multipart"
"net/http"
Expand All @@ -61,6 +62,8 @@ import (
"os"
"path/filepath"
"strings"

"github.com/gofiber/fiber/v2"
)

// Media types
Expand Down Expand Up @@ -265,6 +268,34 @@ func (rc *RequestConfig) SetFileFromPath(uploads []UploadFile, params ...H) *Req
return rc
}

// SetPath supply new request path to deal with path variable request
// ex. /reqpath/:book/:apple , usage: r.POST("/reqpath/").SetPath("book1/apple2")...
func (rc *RequestConfig) SetPath(str string) *RequestConfig {
rc.Path += str
return rc
}

// SetQueryD supply query string, support query using string array input.
// ex. /reqpath/?Ids[]=E&Ids[]=M usage: IDArray:=[]string{"E","M"} r.GET("reqpath").SetQueryD(gofight.D{`Ids[]`: IDArray})
func (rc *RequestConfig) SetQueryD(query D) *RequestConfig {
var buf strings.Builder
buf.WriteString("?")
for k, v := range query {
switch v.(type) {
case string:
buf.WriteString(k + "=" + v.(string))
buf.WriteString("&")
case []string:
for _, info := range v.([]string) {
buf.WriteString(k + "=" + info)
buf.WriteString("&")
}
}
}
rc.Path = rc.Path + buf.String()[:len(buf.String())-1]
return rc
}

// SetQuery supply query string.
func (rc *RequestConfig) SetQuery(query H) *RequestConfig {
f := make(url.Values)
Expand Down Expand Up @@ -345,6 +376,7 @@ func (rc *RequestConfig) initTest() (*http.Request, *httptest.ResponseRecorder)
}

if rc.Debug {
log.Printf("Request QueryString: %s", qs)
log.Printf("Request Method: %s", rc.Method)
log.Printf("Request Path: %s", rc.Path)
log.Printf("Request Body: %s", rc.Body)
Expand All @@ -364,3 +396,16 @@ func (rc *RequestConfig) Run(r http.Handler, response ResponseFunc) {
r.ServeHTTP(w, req)
response(w, req)
}

// RunX is introduced to support FiberEngine
func (rc *RequestConfig) RunX(app *fiber.App, response ResponseFunc) {
req, w := rc.initTest()
resp, err1 := app.Test(req)
w.Code = resp.StatusCode
w.Result().Header = resp.Header.Clone()
body, _ := ioutil.ReadAll(resp.Body)
w.Body.Write(body)
if err1 == nil {
response(w, req)
}
}
2 changes: 1 addition & 1 deletion gofight_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func TestBasicHelloWorld(t *testing.T) {
version := "0.0.1"

r.GET("/").
// trun on the debug mode.
// turn on the debug mode.
SetDebug(true).
SetHeader(H{
"X-Version": version,
Expand Down

0 comments on commit ec4039c

Please sign in to comment.