HTTPRequestToMap Converts HTTP Request to Matcher Map
(req *http.Request)
| 12 | |
| 13 | // HTTPRequestToMap Converts HTTP Request to Matcher Map |
| 14 | func HTTPRequestToMap(req *http.Request) (map[string]interface{}, error) { |
| 15 | m := make(map[string]interface{}) |
| 16 | var headers string |
| 17 | for k, v := range req.Header { |
| 18 | k = strings.ToLower(strings.TrimSpace(strings.ReplaceAll(k, "-", "_"))) |
| 19 | vv := strings.Join(v, " ") |
| 20 | m[k] = strings.Join(v, " ") |
| 21 | headers += fmt.Sprintf("%s: %s", k, vv) |
| 22 | } |
| 23 | |
| 24 | m["all_headers"] = headers |
| 25 | |
| 26 | body, err := io.ReadAll(req.Body) |
| 27 | if err != nil { |
| 28 | return nil, err |
| 29 | } |
| 30 | req.Body = io.NopCloser(bytes.NewBuffer(body)) |
| 31 | m["body"] = string(body) |
| 32 | |
| 33 | reqdump, err := httputil.DumpRequest(req, true) |
| 34 | if err != nil { |
| 35 | return nil, err |
| 36 | } |
| 37 | reqdumpString := string(reqdump) |
| 38 | m["raw"] = reqdumpString |
| 39 | m["request"] = reqdumpString |
| 40 | m["method"] = req.Method |
| 41 | m["path"] = req.URL.Path |
| 42 | m["host"] = req.URL.Host |
| 43 | m["scheme"] = req.URL.Scheme |
| 44 | m["url"] = req.URL.String() |
| 45 | m["query"] = req.URL.Query().Encode() |
| 46 | |
| 47 | return m, nil |
| 48 | } |
| 49 | |
| 50 | // HTTPResponseToMap Converts HTTP Response to Matcher Map |
| 51 | func HTTPResponseToMap(resp *http.Response) (map[string]interface{}, error) { |