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
13func TestListen(t *testing.T) {
14 t.Log("Testing listener")
15
16 testSubmission := submission {
17 "package",
18 []byte("012345678"),
19 []byte("012345678"),
20 []byte("012345678"),
21 time.Now(),
22 }
23
24 go listen(8080)
25
26 url := "http://localhost:8080/submit"
27 fmt.Println("URL:>", url)
28
29 j, err := json.Marshal(testSubmission)
30 if err != nil {
31 fmt.Println(err)
32 return
33 }
34
35 req, err := http.NewRequest("POST", url, bytes.NewBuffer(j))
36 req.Header.Set("Content-Type", "application/json")
37
38 client := &http.Client{}
39 resp, err := client.Do(req)
40 if err != nil {
41 panic(err)
42 }
43 defer resp.Body.Close()
44
45 fmt.Println("response Status:", resp.Status)
46 fmt.Println("response Headers:", resp.Header)
47 body, _ := ioutil.ReadAll(resp.Body)
48 fmt.Println("response Body:", string(body))
49}