-
Notifications
You must be signed in to change notification settings - Fork 243
/
http_test.go
52 lines (44 loc) · 1.32 KB
/
http_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
package gojs_test
import (
"errors"
"io"
"net/http"
"strings"
"testing"
"github.com/tetratelabs/wazero"
gojs "github.com/tetratelabs/wazero/imports/go"
"github.com/tetratelabs/wazero/internal/testing/require"
)
type roundTripperFunc func(r *http.Request) (*http.Response, error)
func (f roundTripperFunc) RoundTrip(r *http.Request) (*http.Response, error) {
return f(r)
}
func Test_http(t *testing.T) {
t.Parallel()
ctx := gojs.WithRoundTripper(testCtx, roundTripperFunc(func(req *http.Request) (*http.Response, error) {
if req.URL.Path == "/error" {
return nil, errors.New("error")
}
if req.Body != nil {
require.Equal(t, http.MethodPost, req.Method)
bytes, err := io.ReadAll(req.Body)
require.NoError(t, err)
require.Equal(t, "ice cream", string(bytes))
}
return &http.Response{
StatusCode: http.StatusOK,
Status: http.StatusText(http.StatusOK),
Header: http.Header{"Custom": {"1"}},
Body: io.NopCloser(strings.NewReader("abcdef")),
ContentLength: 6,
}, nil
}))
stdout, stderr, err := compileAndRun(ctx, "http", wazero.NewModuleConfig().
WithEnv("BASE_URL", "http://host"))
require.EqualError(t, err, `module "" closed with exit_code(0)`)
require.Zero(t, stderr)
require.Equal(t, `Get "http://host/error": net/http: fetch() failed: error
1
abcdef
`, stdout)
}