1
0
mirror of https://github.com/taigrr/wasm-experiments synced 2025-01-18 04:03:21 -08:00

Working WASM Fetch test case

This commit is contained in:
Johan Brandhorst 2019-05-28 22:40:09 +01:00
parent 1edc3d9fc5
commit 1fecaeb312
3 changed files with 45 additions and 30 deletions

View File

@ -36,6 +36,9 @@ fetch: clean
cp $$(go env GOROOT)/misc/wasm/wasm_exec.js ./html/wasm_exec.js
sed -i -e 's;</button>;</button>\n\t<div id=\"target\"></div>;' ./html/index.html
test: clean
GOOS=js GOARCH=wasm go test -c -o ./html/test.wasm ./test/
clean:
rm -f ./html/*

42
test/fetch_test.go Normal file
View File

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

View File

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