From 1fecaeb3125a4b13fb0f28c9ce1ede62df700d1e Mon Sep 17 00:00:00 2001 From: Johan Brandhorst Date: Tue, 28 May 2019 22:40:09 +0100 Subject: [PATCH] Working WASM Fetch test case --- Makefile | 3 +++ test/fetch_test.go | 42 ++++++++++++++++++++++++++++++++++++++++++ test/wasm_test.go | 30 ------------------------------ 3 files changed, 45 insertions(+), 30 deletions(-) create mode 100644 test/fetch_test.go delete mode 100644 test/wasm_test.go diff --git a/Makefile b/Makefile index 869400b..7783ba3 100644 --- a/Makefile +++ b/Makefile @@ -36,6 +36,9 @@ fetch: clean cp $$(go env GOROOT)/misc/wasm/wasm_exec.js ./html/wasm_exec.js sed -i -e 's;;\n\t
;' ./html/index.html +test: clean + GOOS=js GOARCH=wasm go test -c -o ./html/test.wasm ./test/ + clean: rm -f ./html/* diff --git a/test/fetch_test.go b/test/fetch_test.go new file mode 100644 index 0000000..220e73f --- /dev/null +++ b/test/fetch_test.go @@ -0,0 +1,42 @@ +package fetch_test + +import ( + "encoding/json" + "net/http" + "testing" +) + +type jsonResp struct { + Headers struct { + Accept string `json:"Accept"` + AcceptEncoding string `json:"Accept-Encoding"` + Host string `json:"Host"` + Origin string `json:"Origin"` + Referer string `json:"Referer"` + UserAgent string `json:"User-Agent"` + } `json:"headers"` + Origin string `json:"origin"` + URL string `json:"url"` +} + +func TestFetch(t *testing.T) { + resp, err := http.Get("https://httpbin.org/get") + if err != nil { + t.Fatal(err) + } + defer resp.Body.Close() + + if resp.StatusCode != http.StatusOK { + t.Fatalf("Unexpected StatusCode %d", resp.StatusCode) + } + + var r jsonResp + err = json.NewDecoder(resp.Body).Decode(&r) + if err != nil { + t.Fatal(err) + } + + if r.URL != "https://httpbin.org/get" { + t.Errorf("Unexpected request URL: %q", r.URL) + } +} diff --git a/test/wasm_test.go b/test/wasm_test.go deleted file mode 100644 index ea7bf2a..0000000 --- a/test/wasm_test.go +++ /dev/null @@ -1,30 +0,0 @@ -//+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)) - } -}