-
Notifications
You must be signed in to change notification settings - Fork 14
/
server.go
206 lines (182 loc) · 4.43 KB
/
server.go
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package goodman
import (
"bufio"
"encoding/json"
"fmt"
"io"
"net"
t "github.com/snikch/goodman/transaction"
)
const (
defaultPort = "61321"
defaultMessageDelimiter = "\n"
)
// Server is responsible for starting a server and running lifecycle callbacks.
type Server struct {
Runner []Runner
Port string
MessageDelimeter []byte
conn net.Conn
}
// NewServer returns a new server instance with the supplied runner. If no
// runner is supplied, a new one will be created.
func NewServer(runners []Runner) *Server {
return &Server{
Runner: runners,
Port: defaultPort,
MessageDelimeter: []byte(defaultMessageDelimiter),
}
}
// Run starts the server listening for events from dredd.
func (server *Server) Run() error {
fmt.Println("Starting")
ln, err := net.Listen("tcp", ":"+server.Port)
if err != nil {
return err
}
fmt.Println("Accepting connection")
conn, err := ln.Accept()
if err != nil {
return err
}
defer ln.Close()
defer conn.Close()
server.conn = conn
for {
body, err := bufio.
NewReader(conn).
ReadString('\n')
if err == io.EOF {
return nil
}
if err != nil {
return err
}
body = body[:len(body)-1]
m := &message{}
err = json.Unmarshal([]byte(body), m)
if err != nil {
return err
}
err = server.ProcessMessage(m)
if err != nil {
return err
}
}
}
// ProcessMessage handles a single event message.
func (server *Server) ProcessMessage(m *message) error {
switch m.Event {
case "beforeAll":
fallthrough
case "afterAll":
m.transactions = []*t.Transaction{}
err := json.Unmarshal(m.Data, &m.transactions)
if err != nil {
return err
}
default:
m.transaction = &t.Transaction{}
err := json.Unmarshal(m.Data, m.transaction)
if err != nil {
return err
}
}
switch m.Event {
case "beforeAll":
server.RunBeforeAll(&m.transactions)
break
case "beforeEach":
// before is run after beforeEach, as no separate event is fired.
server.RunBeforeEach(m.transaction)
server.RunBefore(m.transaction)
break
case "beforeEachValidation":
// beforeValidation is run after beforeEachValidation, as no separate event
// is fired.
server.RunBeforeEachValidation(m.transaction)
server.RunBeforeValidation(m.transaction)
break
case "afterEach":
// after is run before afterEach as no separate event is fired.
server.RunAfter(m.transaction)
server.RunAfterEach(m.transaction)
break
case "afterAll":
server.RunAfterAll(&m.transactions)
break
default:
return fmt.Errorf("Unknown event '%s'", m.Event)
}
switch m.Event {
case "beforeAll":
fallthrough
case "afterAll":
return server.sendResponse(m, m.transactions)
default:
return server.sendResponse(m, m.transaction)
}
}
func (server *Server) RunBeforeAll(trans *[]*t.Transaction) {
for _, runner := range server.Runner {
runner.RunBeforeAll(trans)
}
}
func (server *Server) RunBeforeEach(trans *t.Transaction) {
for _, runner := range server.Runner {
runner.RunBeforeEach(trans)
}
}
func (server *Server) RunBefore(trans *t.Transaction) {
for _, runner := range server.Runner {
runner.RunBefore(trans)
}
}
func (server *Server) RunBeforeEachValidation(trans *t.Transaction) {
for _, runner := range server.Runner {
runner.RunBeforeEachValidation(trans)
}
}
func (server *Server) RunBeforeValidation(trans *t.Transaction) {
for _, runner := range server.Runner {
runner.RunBeforeValidation(trans)
}
}
func (server *Server) RunAfterEach(trans *t.Transaction) {
for _, runner := range server.Runner {
runner.RunAfterEach(trans)
}
}
func (server *Server) RunAfter(trans *t.Transaction) {
for _, runner := range server.Runner {
runner.RunAfter(trans)
}
}
func (server *Server) RunAfterAll(trans *[]*t.Transaction) {
for _, runner := range server.Runner {
runner.RunAfterAll(trans)
}
}
// sendResponse submits the transaction(s) back to dredd.
func (server *Server) sendResponse(m *message, dataObj interface{}) error {
data, err := json.Marshal(dataObj)
if err != nil {
return err
}
m.Data = json.RawMessage(data)
response, err := json.Marshal(m)
if err != nil {
return err
}
server.conn.Write(response)
server.conn.Write(server.MessageDelimeter)
return nil
}
// message represents a single event received over the connection.
type message struct {
UUID string `json:"uuid"`
Event string `json:"event"`
Data json.RawMessage `json:"data"`
transaction *t.Transaction
transactions []*t.Transaction
}