Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Beego_test and add fiberEngine Test. #86

Merged
merged 5 commits into from
Jun 27, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions _example/beego_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,25 @@ package example

import (
"fmt"
"net/http"
"testing"

"github.com/appleboy/gofight/v2"
"github.com/astaxie/beego"
"github.com/stretchr/testify/assert"
"net/http"
"testing"
)
ralic marked this conversation as resolved.
Show resolved Hide resolved

func TestSayHelloWorld(t *testing.T) {
uri := "/say"

// 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
21 changes: 21 additions & 0 deletions _example/fiberEng.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
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("/", fiberRoute)
return e
}

func fiberRoute(c *fiber.Ctx) error {
msg := fmt.Sprintf("God Love the World ! 👴 %s is %s years old~", c.Query("name"), c.Query("age"))
c.Status(200).SendString(msg) // =>God Love the World ! 👴 john is 75 years old~
return nil
}
37 changes: 37 additions & 0 deletions _example/fiberEng_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
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~",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
r := gofight.New()
r.GET(tt.path).SetQueryD(gofight.D{
"name": "john",
"age": "75",
}).
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)
})
})
}
}
21 changes: 20 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,23 @@ module github.com/appleboy/gofight/v2

go 1.13

require github.com/stretchr/testify v1.4.0
require (
gitea.com/lunny/tango v0.6.5
github.com/andybalholm/brotli v1.0.1 // indirect
github.com/astaxie/beego v1.12.3
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/gin-gonic/gin v1.6.3
github.com/gofiber/fiber/v2 v2.3.3
github.com/gorilla/context v1.1.1 // indirect
github.com/gorilla/mux v1.8.0
github.com/gorilla/pat v1.0.1
github.com/julienschmidt/httprouter v1.2.0
github.com/klauspost/compress v1.11.7 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/labstack/echo/v4 v4.1.17
github.com/stretchr/testify v1.7.0
github.com/valyala/fasthttp v1.19.0 // indirect
golang.org/x/sys v0.0.0-20210113181707-4bcb84eeeb78 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

revert these changes.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1.To add fiber support might have to add fiber itself into the go module file, since fiber server is based on the fasthttp, which is different from the golang's original net/http package.
Please see following reference for performance benchmarking.
fasthttp: (https://github.com/valyala/fasthttp)
fiber: (https://github.com/gofiber/fiber)
2. It would be better to keep the version of beego used in testing indirectly to keep the test working.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I know your point. I remove all packages from go.mod since renaming the example to _example. So we don't need all the framework package if you want to use the package.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See the reference: #81

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any feedbacks?

259 changes: 256 additions & 3 deletions go.sum

Large diffs are not rendered by default.

43 changes: 43 additions & 0 deletions gofight.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ package gofight
import (
"bytes"
"encoding/json"
"github.com/gofiber/fiber/v2"
ralic marked this conversation as resolved.
Show resolved Hide resolved
"io"
"io/ioutil"
"log"
"mime/multipart"
"net/http"
Expand Down Expand Up @@ -265,6 +267,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 @@ -363,3 +393,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.HeaderMap = 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