go-dvote/net/net_test.go
net/net_test.goRaw
1package net
2
3import (
4 "testing"
5 "encoding/json"
6 "net/http"
7 "fmt"
8 "time"
9 "bytes"
10 "io/ioutil"
11)
12
13//func generateSubmission() submission {
14
15//}
16
17func TestListen(t *testing.T) {
18 t.Log("Testing listener")
19
20 testSubmission := submission {
21 "package",
22 []byte("012345678"),
23 []byte("012345678"),
24 []byte("012345678"),
25 time.Now(),
26 }
27
28 listen("8080")
29
30 url := "http://localhost:8080/submit"
31 fmt.Println("URL:>", url)
32
33 j, err := json.Marshal(testSubmission)
34 if err != nil {
35 t.Errorf("Bad test JSON: %s", err)
36 }
37
38 req, err := http.NewRequest("POST", url, bytes.NewBuffer(j))
39 req.Header.Set("Content-Type", "application/json")
40
41 client := &http.Client{}
42 resp, err := client.Do(req)
43 if err != nil {
44 t.Errorf("Error in client: %s", err)
45 }
46 defer resp.Body.Close()
47
48 fmt.Println("response Status:", resp.Status)
49 fmt.Println("response Headers:", resp.Header)
50 body, _ := ioutil.ReadAll(resp.Body)
51 fmt.Println("response Body:", string(body))
52}