-
Notifications
You must be signed in to change notification settings - Fork 1
/
capture.go
47 lines (42 loc) · 1.38 KB
/
capture.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
package spectest
import (
"net/http"
"time"
)
// capture is used to capture the inbound request and final response.
type capture struct {
// inboundRequest is the inbound http request
inboundRequest *http.Request
// finalResponse is the final http response
finalResponse *http.Response
// mockInteractions is the list of mock interactions
mockInteractions []*mockInteraction
}
// newCapture creates a new capture
func newCapture() *capture {
return &capture{
mockInteractions: []*mockInteraction{},
}
}
// appendObserver appends the observer to the list of observers
func (c *capture) appendObserver(observers []Observe) []Observe {
return append(observers, func(finalRes *http.Response, inboundReq *http.Request, a *SpecTest) {
c.finalResponse = copyHTTPResponse(finalRes)
defer func() {
if err := c.finalResponse.Body.Close(); err != nil {
panic(err) // FIXME: handle error
}
}()
c.inboundRequest = copyHTTPRequest(inboundReq)
})
}
// appendMockObservers appends the mock observer to the list of observers
func (c *capture) appendMockObservers(mocksObservers []Observe) []Observe {
return append(mocksObservers, func(mockRes *http.Response, mockReq *http.Request, a *SpecTest) {
c.mockInteractions = append(c.mockInteractions, &mockInteraction{
request: copyHTTPRequest(mockReq),
response: copyHTTPResponse(mockRes),
timestamp: time.Now().UTC(),
})
})
}