-
Notifications
You must be signed in to change notification settings - Fork 9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve tests by adding tiny go test cases #90
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,7 @@ package wasi_http | |
import ( | ||
"context" | ||
"fmt" | ||
"io/ioutil" | ||
"io" | ||
"net/http" | ||
"net/http/httptest" | ||
"os" | ||
|
@@ -19,21 +19,23 @@ import ( | |
) | ||
|
||
type handler struct { | ||
urls []string | ||
bodies []string | ||
urls []string | ||
bodies []string | ||
methods []string | ||
} | ||
|
||
func (h *handler) reset() { | ||
h.bodies = []string{} | ||
h.urls = []string{} | ||
h.methods = []string{} | ||
Comment on lines
28
to
+30
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Mostly curious, are the empty slices semantically different than using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think so, I can use |
||
} | ||
|
||
func (h *handler) ServeHTTP(res http.ResponseWriter, req *http.Request) { | ||
body := "" | ||
|
||
if req.Body != nil { | ||
defer req.Body.Close() | ||
data, err := ioutil.ReadAll(req.Body) | ||
data, err := io.ReadAll(req.Body) | ||
if err != nil { | ||
panic(err.Error()) | ||
} | ||
|
@@ -45,15 +47,13 @@ func (h *handler) ServeHTTP(res http.ResponseWriter, req *http.Request) { | |
|
||
h.urls = append(h.urls, req.URL.String()) | ||
h.bodies = append(h.bodies, body) | ||
h.methods = append(h.methods, req.Method) | ||
} | ||
|
||
func TestHttpClient(t *testing.T) { | ||
filePaths, _ := filepath.Glob("../../testdata/c/http/http*.wasm") | ||
for _, file := range filePaths { | ||
fmt.Printf("%v\n", file) | ||
} | ||
filePaths, _ := filepath.Glob("../../testdata/*/http/http*.wasm") | ||
if len(filePaths) == 0 { | ||
t.Log("nothing to test") | ||
t.Error("nothing to test") | ||
} | ||
|
||
h := handler{} | ||
|
@@ -64,13 +64,38 @@ func TestHttpClient(t *testing.T) { | |
{ | ||
"/get?some=arg&goes=here", | ||
"/post", | ||
"/put", | ||
}, | ||
{ | ||
"/get?some=arg&goes=here", | ||
"/post", | ||
"/put", | ||
}, | ||
} | ||
|
||
expectedBodies := [][]string{ | ||
{ | ||
"", | ||
"{\"foo\": \"bar\"}", | ||
"{\"baz\": \"blah\"}", | ||
}, | ||
{ | ||
"", | ||
"{\"foo\": \"bar\"}", | ||
"{\"baz\": \"blah\"}", | ||
}, | ||
} | ||
|
||
expectedMethods := [][]string{ | ||
{ | ||
"GET", | ||
"POST", | ||
"PUT", | ||
}, | ||
{ | ||
"GET", | ||
"POST", | ||
"PUT", | ||
}, | ||
} | ||
|
||
|
@@ -128,19 +153,19 @@ func TestHttpClient(t *testing.T) { | |
if !reflect.DeepEqual(expectedBodies[testIx], h.bodies) { | ||
t.Errorf("Unexpected paths: %v vs %v", h.bodies, expectedBodies[testIx]) | ||
} | ||
if !reflect.DeepEqual(expectedMethods[testIx], h.methods) { | ||
t.Errorf("Unexpected paths: %v vs %v", h.methods, expectedMethods[testIx]) | ||
} | ||
|
||
h.reset() | ||
}) | ||
} | ||
} | ||
|
||
func TestServer(t *testing.T) { | ||
filePaths, _ := filepath.Glob("../../testdata/c/http/server*.wasm") | ||
for _, file := range filePaths { | ||
fmt.Printf("%v\n", file) | ||
} | ||
filePaths, _ := filepath.Glob("../../testdata/*/http/server*.wasm") | ||
if len(filePaths) == 0 { | ||
t.Log("nothing to test") | ||
t.Error("nothing to test") | ||
} | ||
|
||
for _, test := range filePaths { | ||
|
@@ -192,12 +217,12 @@ func TestServer(t *testing.T) { | |
for i := 0; i < 3; i++ { | ||
res, err := http.Get(s.URL) | ||
if err != nil { | ||
t.Error("Failed to read from server.") | ||
t.Errorf("Failed to read from server: %s", err.Error()) | ||
continue | ||
} | ||
defer res.Body.Close() | ||
|
||
data, err := ioutil.ReadAll(res.Body) | ||
data, err := io.ReadAll(res.Body) | ||
if err != nil { | ||
t.Error("Failed to read body.") | ||
continue | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module github.com/stealthrocket/wasi-go/testdata/tinygo/http | ||
|
||
go 1.20 | ||
|
||
require github.com/dev-wasm/dev-wasm-go/http v0.0.0-20230720212318-cb04411741fd // indirect |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
github.com/dev-wasm/dev-wasm-go/http v0.0.0-20230720212318-cb04411741fd h1:fLCnhqCMaCHXhA6Ozb7sfTyqt/g5mp1dHOjbHo/PacM= | ||
github.com/dev-wasm/dev-wasm-go/http v0.0.0-20230720212318-cb04411741fd/go.mod h1:K7HKbanDfLH3UL/hvExI1hVPetqnn9OZ4IOFcD5Pzw0= |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"io/ioutil" | ||
"net/http" | ||
"os" | ||
|
||
wasiclient "github.com/dev-wasm/dev-wasm-go/http/client" | ||
) | ||
|
||
func printResponse(r *http.Response) { | ||
fmt.Printf("Status: %d\n", r.StatusCode) | ||
for k, v := range r.Header { | ||
fmt.Printf("%s: %s\n", k, v[0]) | ||
} | ||
body, err := ioutil.ReadAll(r.Body) | ||
if err != nil { | ||
fmt.Println(err.Error()) | ||
os.Exit(4) | ||
} | ||
fmt.Printf("Body: \n%s\n", body) | ||
} | ||
|
||
func main() { | ||
server := os.Getenv("SERVER") | ||
client := http.Client{ | ||
Transport: wasiclient.WasiRoundTripper{}, | ||
} | ||
res, err := client.Get("http://" + server + "/get?some=arg&goes=here") | ||
if err != nil { | ||
fmt.Println(err.Error()) | ||
os.Exit(1) | ||
} | ||
printResponse(res) | ||
res.Body.Close() | ||
|
||
res, err = client.Post("http://"+server+"/post", "application/json", wasiclient.BodyReaderCloser([]byte("{\"foo\": \"bar\"}"))) | ||
if err != nil { | ||
fmt.Println(err.Error()) | ||
os.Exit(2) | ||
} | ||
printResponse(res) | ||
res.Body.Close() | ||
|
||
res, err = wasiclient.Put(&client, "http://"+server+"/put", "application/json", wasiclient.BodyReaderCloser([]byte("{\"baz\": \"blah\"}"))) | ||
if err != nil { | ||
fmt.Println(err.Error()) | ||
os.Exit(3) | ||
} | ||
printResponse(res) | ||
res.Body.Close() | ||
|
||
os.Exit(0) | ||
} |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm a bit concerned about introducing such large files in the source tree, do you think we could have them regenerated by the Makefile to avoid checking them in? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm happy to make this driven by Makefile, though it will mean that wdyt? I can edit the tests to skip the tests if There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we can have TinyGo installed in CI without having to recompile it I think it would be the right trade off 👍 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/dev-wasm/dev-wasm-go/http/server" | ||
) | ||
|
||
var count int = 0 | ||
|
||
func main() { | ||
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { | ||
w.Header().Add("Server", "Wasirun 0.0.1") | ||
w.WriteHeader(200) | ||
body := fmt.Sprintf("Hello from WASM! (%d)", count) | ||
count = count + 1 | ||
w.Write([]byte(body)) | ||
}) | ||
server.ListenAndServe(nil) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for documenting this!