package main
import (
"bytes"
"fmt"
"io"
"mime/multipart"
"net/http"
"net/url"
"os"
"strings"
"testing"
)
func request(method, url string, body string, head map[string]string) (*http.Response, error) {
req, err := http.NewRequest(method, url, strings.NewReader(body))
if err != nil {
return nil, err
}
for k, v := range head {
req.Header.Add(k, v)
}
return http.DefaultClient.Do(req)
}
func form(method, urls string, head map[string]string) (*http.Response, error) {
body, types, err := createReqBody("/Users/jalyzhang/Documents/t3/add.tar", map[string]string{"target": "add.wasm", "vmtype": "fabric", "algo": ""})
if err != nil {
return nil, err
}
f := make(url.Values)
f.Add("target", "add.wasm")
f.Add("vmtype", "fabric")
f.Add("algo", "")
req, err := http.NewRequest(method, urls, body) // strings.NewReader(f.Encode())
if err != nil {
return nil, err
}
for k, v := range head {
req.Header.Add(k, v)
}
req.Header.Add("Content-Type", types)
return http.DefaultClient.Do(req)
}
func createReqBody(filePath string, params map[string]string) (io.Reader, string, error) {
var err error
buf := new(bytes.Buffer)
bw := multipart.NewWriter(buf) // body writer
f, err := os.Open(filePath)
if err != nil {
return nil, "", err
}
defer f.Close()
for k, v := range params {
_ = bw.WriteField(k, v)
}
p, err := bw.CreateFormFile("file", "add.tar")
if err != nil {
return nil, "", err
}
_, _ = io.Copy(p, f)
bw.Close()
return buf, bw.FormDataContentType(), nil
}
func TestBuild(t *testing.T) {
res, err := form("POST", "http://localhost:8080/v1/sgx/wasm/build", map[string]string{})
if err != nil {
t.Fatal(err)
}
io.Copy(os.Stdout, res.Body)
fmt.Println()
res.Body.Close()
}
func TestAttest(t *testing.T) {
tem := `{
"id": "7a2964e92ed65989628836605e81248f231256b3901a0057b980747bbf9d045e",
"input": "[\"4\",\"15\"]",
"task_id": "#T",
"url": "http://127.0.0.1:8888",
"body_pattern": "result: ${result}, proof: ${proof}"
}`
for i := 0; i < 1000; i++ {
body := strings.Replace(tem, "#T", "ad"+fmt.Sprint(i), 1)
res, err := request("POST", "http://localhost:8080/v1/sgx/wasm/computeandprove", body, map[string]string{"Content-Type": "application/json"})
if err != nil {
t.Fatal(err)
}
io.Copy(os.Stdout, res.Body)
fmt.Println()
res.Body.Close()
}
}