-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_test.go
More file actions
104 lines (83 loc) · 2.32 KB
/
Copy pathmain_test.go
File metadata and controls
104 lines (83 loc) · 2.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package main
import (
"bytes"
"crypto/tls"
"crypto/x509"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"testing"
"time"
"github.com/Cori1109/degeneres-test/ballpark"
"github.com/Cori1109/degeneres-test/data"
"github.com/Cori1109/degeneres-test/server"
log "github.com/sirupsen/logrus"
)
const (
host = "localhost"
port = 8080
certsPath = "./certs"
keyName = "server.key"
certName = "server.cer"
serverProtocol = "https" // TODO: Set https if secure middleware is added
)
var x509CertPool *x509.CertPool
func TestMain(m *testing.M) {
// This is supposed to be the CA Cert, but the key/cert is self signed
// so passing in the server cert instead as single node chain of trust
certBytes, err := ioutil.ReadFile(filepath.Join(certsPath, certName)) // TODO: Only read file if secure middleware is added
if err != nil {
fmt.Println("Failed reading cert:", err)
return
}
x509CertPool = x509.NewCertPool()
x509CertPool.AppendCertsFromPEM(certBytes) // TODO: Only add if secure middleware is added
log.SetLevel(log.DebugLevel)
go server.Ballpark(server.Config{
Host: host,
Port: port,
CertsPath: certsPath,
KeyName: keyName,
CertName: certName,
}, ballpark.Config{})
time.Sleep(500 * time.Millisecond)
os.Exit(m.Run())
}
func TestTicket(t *testing.T) {
url := fmt.Sprintf("%s://%s:%d/ticket", serverProtocol, host, port)
jsonBytes := []byte(`{"id":"912391-123-123-8182123"}`)
resp := doReq(t, url, jsonBytes)
ticketOut := &data.TicketOut{}
if err := json.NewDecoder(resp.Body).Decode(ticketOut); err != nil {
t.Error("Failed to decode response:", err)
t.FailNow()
}
fmt.Println("Ticket Response:", *ticketOut)
}
func doReq(t *testing.T, url string, jsonBytes []byte) *http.Response {
req, err := http.NewRequest(http.MethodPost, url, bytes.NewReader(jsonBytes))
if err != nil {
t.Error("Failed creating request:", err)
t.FailNow()
}
req.Header.Add("Origin", fmt.Sprintf("%s://localhost", serverProtocol))
tr := &http.Transport{
TLSClientConfig: &tls.Config{
RootCAs: x509CertPool,
},
}
client := &http.Client{Transport: tr}
resp, err := client.Do(req)
if err != nil {
t.Error("Failed doing req:", err)
t.FailNow()
}
if resp.StatusCode != http.StatusOK {
t.Error("Bad status code:", resp.StatusCode)
t.FailNow()
}
return resp
}