(req *HttpTestReq)
| 35 | } |
| 36 | |
| 37 | func (s *HttpTestService) Do(req *HttpTestReq) (*HttpTestResp, error) { |
| 38 | if strings.TrimSpace(req.URL) == "" { |
| 39 | return nil, errors.New("url is empty") |
| 40 | } |
| 41 | |
| 42 | method := strings.ToUpper(strings.TrimSpace(req.Method)) |
| 43 | if method == "" { |
| 44 | method = http.MethodGet |
| 45 | } |
| 46 | |
| 47 | var bodyReader io.Reader |
| 48 | if req.Body != "" && method != http.MethodGet { |
| 49 | bodyReader = strings.NewReader(req.Body) |
| 50 | } |
| 51 | |
| 52 | httpReq, err := http.NewRequest(method, req.URL, bodyReader) |
| 53 | if err != nil { |
| 54 | return nil, err |
| 55 | } |
| 56 | |
| 57 | for k, v := range req.Header { |
| 58 | httpReq.Header.Set(k, v) |
| 59 | } |
| 60 | |
| 61 | resp, err := s.client.Do(httpReq) |
| 62 | if err != nil { |
| 63 | return nil, err |
| 64 | } |
| 65 | defer resp.Body.Close() |
| 66 | |
| 67 | respBodyBytes, err := io.ReadAll(resp.Body) |
| 68 | if err != nil { |
| 69 | return nil, err |
| 70 | } |
| 71 | |
| 72 | return &HttpTestResp{ |
| 73 | Status: resp.Status, |
| 74 | StatusCode: resp.StatusCode, |
| 75 | Header: resp.Header, |
| 76 | Body: string(respBodyBytes), |
| 77 | }, nil |
| 78 | } |
nothing calls this directly
no outgoing calls
no test coverage detected