Files
wazero/internal/gojs/http_test.go
Crypt Keeper 5bd521eb3c Moves host function imports into their own directory (#784)
Our root directory is getting crowded and it is also difficult to
organize the "host imports" concept due to this.

This moves common and language-specific imports into their own
directory. This will break go import signatures on the next release, but
is more sustainable overall.

Signed-off-by: Adrian Cole <adrian@tetrate.io>
2022-08-31 10:22:15 +08:00

53 lines
1.3 KiB
Go

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)
}