forked from yisar/labor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
response.go
47 lines (37 loc) · 922 Bytes
/
response.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
package labor
import (
"io/ioutil"
"net/http/httptest"
"syscall/js"
)
type ResponseRecorder struct {
*httptest.ResponseRecorder
}
func NewResponseRecorder() ResponseRecorder {
return ResponseRecorder{httptest.NewRecorder()}
}
var _ js.Wrapper = ResponseRecorder{}
func (rr ResponseRecorder) JSValue() js.Value {
var res = rr.Result()
var body js.Value = js.Undefined()
if res.ContentLength != 0 {
var b, err = ioutil.ReadAll(res.Body)
if err != nil {
panic(err)
}
body = js.Global().Get("Uint8Array").New(len(b))
js.CopyBytesToJS(body, b)
}
var init = make(map[string]interface{}, 2)
if res.StatusCode != 0 {
init["status"] = res.StatusCode
}
if len(res.Header) != 0 {
var headers = make(map[string]interface{}, len(res.Header))
for k := range res.Header {
headers[k] = res.Header.Get(k)
}
init["headers"] = headers
}
return js.Global().Get("Response").New(body, init)
}