2020-12-21 15:51:43 +05:30
|
|
|
package http
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"net/http"
|
|
|
|
|
"net/http/httputil"
|
|
|
|
|
"strings"
|
|
|
|
|
"time"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// responseToDSLMap converts a HTTP response to a map for use in DSL matching
|
|
|
|
|
func responseToDSLMap(resp *http.Response, body, headers string, duration time.Duration, extra map[string]interface{}) map[string]interface{} {
|
2020-12-24 12:13:18 +05:30
|
|
|
data := make(map[string]interface{}, len(extra)+6+len(resp.Header)+len(resp.Cookies()))
|
2020-12-21 15:51:43 +05:30
|
|
|
for k, v := range extra {
|
|
|
|
|
data[k] = v
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
data["content_length"] = resp.ContentLength
|
|
|
|
|
data["status_code"] = resp.StatusCode
|
|
|
|
|
|
2020-12-24 01:42:04 +05:30
|
|
|
data["body"] = body
|
2020-12-24 12:13:18 +05:30
|
|
|
for _, cookie := range resp.Cookies() {
|
|
|
|
|
data[cookie.Name] = cookie.Value
|
|
|
|
|
}
|
2020-12-21 15:51:43 +05:30
|
|
|
for k, v := range resp.Header {
|
|
|
|
|
k = strings.ToLower(strings.TrimSpace(strings.ReplaceAll(k, "-", "_")))
|
|
|
|
|
data[k] = strings.Join(v, " ")
|
|
|
|
|
}
|
2020-12-24 12:13:18 +05:30
|
|
|
data["header"] = headers
|
|
|
|
|
data["all_headers"] = headers
|
2020-12-21 15:51:43 +05:30
|
|
|
|
|
|
|
|
if r, err := httputil.DumpResponse(resp, true); err == nil {
|
2020-12-24 12:13:18 +05:30
|
|
|
rawString := string(r)
|
|
|
|
|
data["raw"] = rawString
|
|
|
|
|
data["all"] = rawString
|
2020-12-21 15:51:43 +05:30
|
|
|
}
|
|
|
|
|
data["duration"] = duration.Seconds()
|
|
|
|
|
return data
|
|
|
|
|
}
|