-
Notifications
You must be signed in to change notification settings - Fork 0
/
http_fixture.go
193 lines (178 loc) · 5.5 KB
/
http_fixture.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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
"strconv"
"github.com/pkg/errors"
)
// logLine represents log line with request or response data
type logLine struct {
RequestID string `json:"request_id"`
Message string `json:"msg"`
Url string `json:"url"`
Method string `json:"method"`
BodyString string `json:"body_string"`
Headers http.Header `json:"headers"`
StatusCode int `json:"status_code"`
Status string `json:"status"`
}
// bodyWithHeaders represents request or response body and headers
type bodyWithHeaders struct {
Headers http.Header `json:"headers"`
BodyString string `json:"body_string,omitempty"`
//BodyData interface{} `json:"body_data"`
BodyData interface{} `json:"-"`
}
// requestResponse represents request-response pair for fixture
type requestResponse struct {
RequestID string `json:"request_id"`
Url string `json:"url"`
Method string `json:"method"`
Request bodyWithHeaders `json:"request"`
SOAPRequest *winrmRequest `json:"soap_request"`
StatusCode int `json:"status_code"`
Status string `json:"status"`
Response bodyWithHeaders `json:"response"`
SOAPResponse *winrmResponse `json:"soap_response"`
}
type commandResponse struct {
Command string `json:"command"`
Response interface{} `json:"response,omitempty"`
ResponseString string `json:"response_string,omitempty"`
ResponseStderr string `json:"response_stderr,omitempty"`
ExitCode int `json:"exit_code,omitempty"`
//ResponseMultiline []string `json:"response_multiline,omitempty"`
}
// fixture is a list of request-response pairs
type fixture struct {
data []*requestResponse
requestDict map[string]*requestResponse
commandResponses []*commandResponse
currentCommand string
}
func newFixture() *fixture {
return &fixture{
data: []*requestResponse{},
requestDict: make(map[string]*requestResponse),
}
}
func (f *fixture) processLine(line []byte) {
var l logLine
err := json.Unmarshal(line, &l)
if err != nil {
return
}
if l.RequestID == "" {
return
}
if l.BodyString == "" {
return
}
switch l.Message {
case "http_request":
bodyString := ""
n, err := decodeXML(l.BodyString)
if err != nil {
fmt.Fprintf(os.Stderr, "error parse %s xml request body in %+v\n", err, l)
bodyString = l.BodyString
}
r, err := parseRequest(l.BodyString)
if err != nil {
fmt.Fprintf(os.Stderr, "error parse %s winrm soap request body in %+v\n", err, l)
}
pair := &requestResponse{
RequestID: l.RequestID,
Url: l.Url,
Method: l.Method,
Request: bodyWithHeaders{
Headers: l.Headers,
BodyString: bodyString,
BodyData: n,
},
SOAPRequest: r,
}
f.requestDict[l.RequestID] = pair
f.data = append(f.data, pair)
f.processRequest(r)
case "http_response":
found, ok := f.requestDict[l.RequestID]
if !ok {
fmt.Fprintf(os.Stderr, "not found request %s for %+v", l.RequestID, l)
return
}
n, err := decodeXML(l.BodyString)
if err != nil {
fmt.Fprintf(os.Stderr, "invalid xml response body in %+v\n", l)
}
r, err := parseResponse(l.BodyString)
if err != nil {
fmt.Fprintf(os.Stderr, "error parse %s winrm soap response body in %+v\n", err, l)
}
found.SOAPResponse = r
found.Status = l.Status
found.StatusCode = l.StatusCode
found.Response = bodyWithHeaders{
Headers: l.Headers,
BodyString: l.BodyString,
BodyData: n,
}
f.processResponse(r)
}
}
func (f *fixture) processRequest(r *winrmRequest) {
if r.Action == "http://schemas.microsoft.com/wbem/wsman/1/windows/shell/Command" {
if f.currentCommand != "" {
fmt.Fprintf(os.Stderr, "Incorrect http fixture state on %s: %s\n", r.CommandKey, f.currentCommand)
}
f.currentCommand = r.CommandKey
}
if r.Action == "http://schemas.microsoft.com/wbem/wsman/1/windows/shell/Signal" {
f.currentCommand = ""
}
}
func (f *fixture) processResponse(r *winrmResponse) {
if r.Action == "http://schemas.microsoft.com/wbem/wsman/1/windows/shell/ReceiveResponse" {
if f.currentCommand == "" {
fmt.Fprintf(os.Stderr, "Incorrect http fixture state on %s\n", r.Action)
return
}
responseString := r.CommandStdout
if r.CommandStdoutJSON != nil {
responseString = ""
}
exitCode, err := strconv.Atoi(r.ExitCode)
if err != nil {
fmt.Fprintf(os.Stderr, "Invalid exit code %s\n", r.ExitCode)
}
f.commandResponses = append(f.commandResponses, &commandResponse{
Command: f.currentCommand,
Response: r.CommandStdoutJSON,
ResponseString: responseString,
ResponseStderr: r.CommandStderr,
ExitCode: exitCode,
//ResponseMultiline: strings.Split(responseString, "\n"),
})
}
}
func (f *fixture) SaveToFile(filename string) error {
data, err := json.MarshalIndent(f.data, "", " ")
if err != nil {
return errors.Wrap(err, "MarshalIndent failed")
}
err = ioutil.WriteFile(filename, data, 0660)
if err != nil {
return errors.Wrap(err, "WriteFile failed")
}
data, err = json.MarshalIndent(f.commandResponses, "", " ")
if err != nil {
return errors.Wrap(err, "MarshalIndent failed")
}
err = ioutil.WriteFile(filename+"_responses.json", data, 0660)
if err != nil {
return errors.Wrap(err, "WriteFile failed")
}
return nil
}