This repository has been archived by the owner on Oct 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenlight_test.go
81 lines (66 loc) · 1.43 KB
/
enlight_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package enlight
import (
ctx "context"
"fmt"
"io/ioutil"
"net"
"net/http"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/valyala/fasthttp"
"github.com/valyala/fasthttp/fasthttputil"
)
type (
user struct {
ID int `json:"id" xml:"id" form:"id" query:"id" param:"id"`
Name string `json:"name" xml:"name" form:"name" query:"name" param:"name"`
}
)
const (
userJSON = `{"id":1,"name":"Jon Snow"}`
)
func serve(handler fasthttp.RequestHandler, req *http.Request) (*http.Response, error) {
ln := fasthttputil.NewInmemoryListener()
defer ln.Close()
go func() {
err := fasthttp.Serve(ln, handler)
if err != nil {
panic(fmt.Errorf("failed to serve: %v", err))
}
}()
client := http.Client{
Transport: &http.Transport{
DialContext: func(ctx ctx.Context, network, addr string) (net.Conn, error) {
return ln.Dial()
},
},
}
return client.Do(req)
}
func TestEnlightStart(t *testing.T) {
e := New()
go func() {
assert.NoError(t, e.Start(":0"))
}()
time.Sleep(200 * time.Millisecond)
}
func TestEnlightHandler(t *testing.T) {
e := New()
e.GET("/", func(c Context) error {
return c.String(200, "Hello")
})
r, err := http.NewRequest("GET", "http://test/", nil)
if err != nil {
t.Error(err)
}
res, err := serve(e.ServeHTTP, r)
if err != nil {
t.Error(err)
}
body, err := ioutil.ReadAll(res.Body)
if err != nil {
t.Error(err)
}
assert.Equal(t, string(body), "Hello")
}