Skip to content

Commit ec4039c

Browse files
authored
Fix Beego_test and add fiberEngine Test. (#86)
1 parent 8bfe2d5 commit ec4039c

File tree

7 files changed

+380
-8
lines changed

7 files changed

+380
-8
lines changed

_example/beego_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@ func TestSayHelloWorld(t *testing.T) {
1515

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

2121
r := gofight.New()
2222
r.GET(uri).
2323
SetDebug(true).
24-
Run(c, func(rp gofight.HTTPResponse, rq gofight.HTTPRequest) {
24+
Run(c.Handlers, func(rp gofight.HTTPResponse, rq gofight.HTTPRequest) {
2525
fmt.Println(rp.Code)
2626
assert.Equal(t, "Hello, World", rp.Body.String())
2727
assert.Equal(t, http.StatusOK, rp.Code)

_example/fiberEng.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package example
2+
3+
import (
4+
"fmt"
5+
"github.com/gofiber/fiber/v2"
6+
)
7+
8+
// FiberEngine is fiber web server and handlers routes
9+
func FiberEngine() *fiber.App {
10+
// Fiber instance
11+
e := fiber.New()
12+
// Routes
13+
e.Get("/helloCouple", fiberRoute)
14+
return e
15+
}
16+
17+
func fiberRoute(c *fiber.Ctx) error {
18+
names := c.Context().QueryArgs().PeekMulti("names")
19+
ages := c.Context().QueryArgs().PeekMulti("ages")
20+
msg := ""
21+
for i := range names {
22+
msg += fmt.Sprintf("God Love the World ! 👴 %s is %s years old~\n", string(names[i]), string(ages[i]))
23+
}
24+
c.Status(200).SendString(msg)
25+
// =>God Love the World ! 👴 john is 75 years old~
26+
// God Love the World ! 👴 mary is 25 years old~
27+
return nil
28+
}

_example/fiberEng_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package example
2+
3+
import (
4+
"github.com/appleboy/gofight/v2"
5+
"github.com/stretchr/testify/assert"
6+
"net/http"
7+
"testing"
8+
)
9+
10+
func TestFiberEngine(t *testing.T) {
11+
12+
tests := []struct {
13+
name string
14+
path string
15+
want string
16+
}{
17+
{
18+
name: "TestHelloWorld",
19+
path: "/",
20+
want: `God Love the World ! 👴 john is 75 years old~
21+
God Love the World ! 👴 mary is 25 years old~
22+
`,
23+
},
24+
}
25+
26+
for _, tt := range tests {
27+
t.Run(tt.name, func(t *testing.T) {
28+
r := gofight.New()
29+
r.GET(tt.path).
30+
SetPath("helloCouple").
31+
SetQueryD(gofight.D{
32+
"names": []string{"john", "mary"},
33+
"ages": []string{"75", "25"},
34+
}).
35+
SetDebug(true).
36+
RunX(FiberEngine(), func(res gofight.HTTPResponse, req gofight.HTTPRequest) {
37+
assert.Equal(t, tt.want, res.Body.String())
38+
assert.Equal(t, http.StatusOK, res.Code)
39+
})
40+
})
41+
}
42+
}

go.mod

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,8 @@ module github.com/appleboy/gofight/v2
22

33
go 1.13
44

5-
require github.com/stretchr/testify v1.4.0
5+
require (
6+
github.com/astaxie/beego v1.12.3 // indirect
7+
github.com/gofiber/fiber/v2 v2.3.3
8+
github.com/stretchr/testify v1.7.0
9+
)

go.sum

Lines changed: 256 additions & 3 deletions
Large diffs are not rendered by default.

gofight.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ import (
5353
"bytes"
5454
"encoding/json"
5555
"io"
56+
"io/ioutil"
5657
"log"
5758
"mime/multipart"
5859
"net/http"
@@ -61,6 +62,8 @@ import (
6162
"os"
6263
"path/filepath"
6364
"strings"
65+
66+
"github.com/gofiber/fiber/v2"
6467
)
6568

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

271+
// SetPath supply new request path to deal with path variable request
272+
// ex. /reqpath/:book/:apple , usage: r.POST("/reqpath/").SetPath("book1/apple2")...
273+
func (rc *RequestConfig) SetPath(str string) *RequestConfig {
274+
rc.Path += str
275+
return rc
276+
}
277+
278+
// SetQueryD supply query string, support query using string array input.
279+
// ex. /reqpath/?Ids[]=E&Ids[]=M usage: IDArray:=[]string{"E","M"} r.GET("reqpath").SetQueryD(gofight.D{`Ids[]`: IDArray})
280+
func (rc *RequestConfig) SetQueryD(query D) *RequestConfig {
281+
var buf strings.Builder
282+
buf.WriteString("?")
283+
for k, v := range query {
284+
switch v.(type) {
285+
case string:
286+
buf.WriteString(k + "=" + v.(string))
287+
buf.WriteString("&")
288+
case []string:
289+
for _, info := range v.([]string) {
290+
buf.WriteString(k + "=" + info)
291+
buf.WriteString("&")
292+
}
293+
}
294+
}
295+
rc.Path = rc.Path + buf.String()[:len(buf.String())-1]
296+
return rc
297+
}
298+
268299
// SetQuery supply query string.
269300
func (rc *RequestConfig) SetQuery(query H) *RequestConfig {
270301
f := make(url.Values)
@@ -345,6 +376,7 @@ func (rc *RequestConfig) initTest() (*http.Request, *httptest.ResponseRecorder)
345376
}
346377

347378
if rc.Debug {
379+
log.Printf("Request QueryString: %s", qs)
348380
log.Printf("Request Method: %s", rc.Method)
349381
log.Printf("Request Path: %s", rc.Path)
350382
log.Printf("Request Body: %s", rc.Body)
@@ -364,3 +396,16 @@ func (rc *RequestConfig) Run(r http.Handler, response ResponseFunc) {
364396
r.ServeHTTP(w, req)
365397
response(w, req)
366398
}
399+
400+
// RunX is introduced to support FiberEngine
401+
func (rc *RequestConfig) RunX(app *fiber.App, response ResponseFunc) {
402+
req, w := rc.initTest()
403+
resp, err1 := app.Test(req)
404+
w.Code = resp.StatusCode
405+
w.Result().Header = resp.Header.Clone()
406+
body, _ := ioutil.ReadAll(resp.Body)
407+
w.Body.Write(body)
408+
if err1 == nil {
409+
response(w, req)
410+
}
411+
}

gofight_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func TestBasicHelloWorld(t *testing.T) {
2424
version := "0.0.1"
2525

2626
r.GET("/").
27-
// trun on the debug mode.
27+
// turn on the debug mode.
2828
SetDebug(true).
2929
SetHeader(H{
3030
"X-Version": version,

0 commit comments

Comments
 (0)