Files
wazero/internal/gojs/http_test.go
Crypt Keeper 25493fe271 gojs: makes experimental status explicit (#1200)
Before, our README said gojs `GOOS=js compiled wasm` was experimental.
However, as we head to 1.0 we should be more explicit about that.

When we started gojs, there was no likely future where `GOOS=wasi` would
happen in the standard go compiler. This has changed, so we'll only keep
the gojs package around until wasi is usable for two Go releases. Being
in an experimental package helps others know to watch out for this.

Signed-off-by: Adrian Cole <adrian@tetrate.io>
2023-03-06 09:22:58 +08:00

53 lines
1.3 KiB
Go

package gojs_test
import (
"errors"
"io"
"net/http"
"strings"
"testing"
"github.com/tetratelabs/wazero"
"github.com/tetratelabs/wazero/experimental/gojs"
"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)
}