-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathfetch_test.go
More file actions
147 lines (117 loc) · 3.54 KB
/
fetch_test.go
File metadata and controls
147 lines (117 loc) · 3.54 KB
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package fetch
import (
"encoding/json"
"testing"
. "gopkg.in/check.v1"
"github.com/gin-gonic/gin"
"github.com/parnurzeal/gorequest"
"gopkg.in/olebedev/go-duktape.v2"
)
// Hook up gocheck into the "go test" runner.
func Test(t *testing.T) { TestingT(t) }
type FetchSuite struct {
ctx *duktape.Context
goFetch func(*duktape.Context) int
}
func (s *FetchSuite) SetUpSuite(c *C) {
gin.SetMode(gin.ReleaseMode)
}
func (s *FetchSuite) SetUpTest(c *C) {
s.goFetch = goFetchSync(nil)
s.ctx = duktape.New()
PushGlobal(s.ctx, nil)
}
var _ = Suite(&FetchSuite{})
func (s *FetchSuite) TestStackAroundGoFetchSync(c *C) {
s.ctx.PushString("http://ya.ru")
s.ctx.PushObject() // options
s.ctx.PushObject() // headers => [ url options headers ]
s.ctx.PutPropString(-2, "headers")
s.ctx.PushContextDump()
c.Assert(s.ctx.SafeToString(-1), Equals, "ctx: top=2, stack=[\"http://ya.ru\",{headers:{}}]")
c.Assert(s.goFetch(s.ctx), Equals, 1)
c.Assert(s.ctx.GetTop(), Equals, 1)
resp := response{}
c.Assert(json.Unmarshal([]byte(s.ctx.JsonEncode(-1)), &resp), IsNil)
}
func (s *FetchSuite) TestGoFetchSyncExternal(c *C) {
s.ctx.PushString("http://ya.ru")
s.ctx.PushObject() // options => [ url {} ]
s.ctx.PushObject() // headers => [ url {} {} ]
s.ctx.PutPropString(-2, "headers") // => [ url {headers: {}} ]
c.Assert(s.goFetch(s.ctx), Equals, 1)
resp := response{}
c.Assert(json.Unmarshal([]byte(s.ctx.JsonEncode(-1)), &resp), IsNil)
c.Assert(resp.Method, Equals, gorequest.GET)
c.Assert(resp.Status, Equals, 200)
c.Assert(resp.StatusText, Equals, "200 Ok")
c.Assert(resp.Errors, HasLen, 0)
c.Assert(resp.Body[:15], Equals, "<!DOCTYPE html>")
c.Assert(resp.Headers.Get("Content-Type"), Equals, "text/html; charset=UTF-8")
}
func (s *FetchSuite) TestGoFetchInternal404(c *C) {
PushGlobal(s.ctx, gin.Default())
c.Assert(s.ctx.PevalString(`
fetch.goFetchSync('/404', {});
`), IsNil)
s.ctx.JsonEncode(-1)
respString := s.ctx.SafeToString(-1)
resp := response{}
json.Unmarshal([]byte(respString), &resp)
c.Assert(resp.Status, Equals, 404)
c.Assert(resp.Method, Equals, gorequest.GET)
c.Assert(resp.Body, Equals, "404 page not found")
}
func (s *FetchSuite) TestGoFetchPromise(c *C) {
PushGlobal(s.ctx, gin.Default())
ch := make(chan string)
s.ctx.PushGlobalGoFunction("cbk", func(co *duktape.Context) int {
ch <- co.SafeToString(-1)
return 0
})
c.Assert(s.ctx.PevalString(`
fetch('/404')
.then(function(resp){
return resp.text();
}).then(cbk);
`), IsNil)
c.Assert(<-ch, Equals, "404 page not found")
}
func (s *FetchSuite) TestGoFetchJson(c *C) {
r := gin.Default()
r.GET("/", func(c *gin.Context) {
c.JSON(201, map[string]interface{}{
"hello": "world",
})
})
PushGlobal(s.ctx, r)
ch := make(chan bool)
s.ctx.PushGlobalGoFunction("cbk", func(co *duktape.Context) int {
ch <- co.GetType(-1).IsObject()
return 0
})
c.Assert(s.ctx.PevalString(`
fetch('/')
.then(function(resp){
return resp.json();
}).then(cbk);
`), IsNil)
c.Assert(<-ch, Equals, true)
}
func (s *FetchSuite) TestGlobals(c *C) {
// fetch
c.Assert(s.ctx.PevalString(`typeof fetch;`), IsNil)
c.Assert(s.ctx.SafeToString(-1), Equals, "function")
s.ctx.Pop()
// fetch.goFetchSync
c.Assert(s.ctx.PevalString(`typeof fetch.goFetchSync;`), IsNil)
c.Assert(s.ctx.SafeToString(-1), Equals, "function")
s.ctx.Pop()
// fetch.Promise
c.Assert(s.ctx.PevalString(`typeof fetch.Promise;`), IsNil)
c.Assert(s.ctx.SafeToString(-1), Equals, "function")
s.ctx.Pop()
}
func (s *FetchSuite) TearDownTest(c *C) {
s.ctx.DestroyHeap()
}