From 1edc3d9fc502de035930b8ad8fc28dd69a583732 Mon Sep 17 00:00:00 2001 From: Johan Brandhorst Date: Sun, 26 May 2019 15:43:09 +0100 Subject: [PATCH] Add experimental browser test --- Makefile | 16 +++++++++++----- test/wasm_test.go | 30 ++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 5 deletions(-) create mode 100644 test/wasm_test.go diff --git a/Makefile b/Makefile index db3f309..869400b 100644 --- a/Makefile +++ b/Makefile @@ -6,11 +6,13 @@ hello: clean .PHONY: tinygo tinygo: clean - docker run --rm -v $$(pwd):/go/src/github.com/johanbrandhorst/wasm-experiments tinygo/tinygo:0.5.0 /bin/bash -c "\ - cd /go/src/github.com/johanbrandhorst/wasm-experiments && \ - tinygo build -o ./html/test.wasm -target wasm --no-debug ./$(target)/main.go && \ - cp /usr/local/tinygo/targets/wasm_exec.js ./html/wasm_exec.js\ - " + #docker run --rm -v $$(pwd):/go/src/github.com/johanbrandhorst/wasm-experiments tinygo/tinygo:0.5.0 /bin/bash -c "\ + # cd /go/src/github.com/johanbrandhorst/wasm-experiments && \ + # tinygo build -o ./html/test.wasm -target wasm --no-debug ./$(target)/main.go && \ + # cp /usr/local/tinygo/targets/wasm_exec.js ./html/wasm_exec.js\ + #" + tinygo build -o ./html/test.wasm -target wasm --no-debug ./$(target)/main.go + cp ~/go/src/github.com/tinygo-org/tinygo/targets/wasm_exec.js ./html/wasm_exec.js cp $$(go env GOROOT)/misc/wasm/wasm_exec.html ./html/index.html sed -i -e 's;;\n\t
;' ./html/index.html @@ -39,3 +41,7 @@ clean: serve: go run main.go + +install-test: + go get github.com/agnivade/wasmbrowsertest + mv $$GOPATH/bin/wasmbrowsertest $$GOPATH/bin/go_js_wasm_exec diff --git a/test/wasm_test.go b/test/wasm_test.go new file mode 100644 index 0000000..ea7bf2a --- /dev/null +++ b/test/wasm_test.go @@ -0,0 +1,30 @@ +//+build js,wasm + +package wasm_test + +import ( + "io/ioutil" + "net/http" + "testing" +) + +func TestFetch(t *testing.T) { + resp, err := http.Get("http://example.com/") + if err != nil { + t.Fatal(err) + } + defer resp.Body.Close() + + if resp.StatusCode != http.StatusOK { + t.Errorf("Unexpected StatusCode %d", resp.StatusCode) + } + + rb, err := ioutil.ReadAll(resp.Body) + if err != nil { + t.Fatal(err) + } + + if string(rb) != "stuff" { + t.Errorf("Unexpected Body: %q", string(rb)) + } +}