This repository has been archived by the owner on Sep 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
shared.go
87 lines (78 loc) · 2.28 KB
/
shared.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
package main
import (
"encoding/json"
"net/http"
"sort"
"strconv"
"strings"
uuid "github.com/nu7hatch/gouuid"
)
// Header is the JSON representation of a header.
type Header struct {
Name string `json:"name"`
Value string `json:"value"`
}
// HTTPEvent is the JSON representation of an HTTP event.
type HTTPEvent struct {
PairID string `json:"event_pair_id"`
HTTPMethod string `json:"http_method"`
Endpoint string `json:"endpoint"`
ReqHeaders []Header `json:"req_headers"`
ReqBody string `json:"request_body"`
RespHeaders []Header `json:"resp_headers"`
RespBody string `json:"response_body"`
ResponseCode string `json:"http_response_code"`
}
type eventHandler interface {
handleEvent(HTTPEvent)
flushBuffer()
}
func parseHTTPEvent(raw string) HTTPEvent {
event := HTTPEvent{}
err := json.Unmarshal([]byte(raw), &event)
check(err)
return event
}
func httpEventToString(data HTTPEvent) string {
s, err := json.Marshal(data)
check(err)
return string(s)
}
// https://stackoverflow.com/a/37335777
// Process the largest indices marked for removal first.
// Processing smaller indices could make larger indices
// invalid during later processing
func removeAll(lst []Header, iv []int) []Header {
sort.Sort(sort.Reverse(sort.IntSlice(iv)))
for w, i := range iv {
lst[i] = lst[len(lst)-1-w]
lst = lst[:len(lst)-1-w]
}
return lst
}
// convertRequestResponse converts an HTTP `Request` and `Response` to the
// `HTTPEvent` struct for use in generating test strings.
func convertRequestResponse(request *http.Request, response *http.Response, reqBody, respBody string) (HTTPEvent, error) {
uuid, err := uuid.NewV4()
if err != nil {
return HTTPEvent{}, err
}
var requestHeaders []Header
for key, value := range request.Header {
requestHeaders = append(requestHeaders, Header{key, strings.Join(value, ",")})
}
var responseHeaders []Header
for key, value := range response.Header {
responseHeaders = append(responseHeaders, Header{key, strings.Join(value, ",")})
}
return HTTPEvent{
PairID: uuid.String(),
HTTPMethod: request.Method,
Endpoint: request.URL.Path,
ReqHeaders: requestHeaders,
ReqBody: reqBody,
RespHeaders: responseHeaders,
RespBody: respBody,
ResponseCode: strconv.Itoa(response.StatusCode),
}, nil
}