/
http.go
69 lines (61 loc) · 1.68 KB
/
http.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
package services
import (
"bytes"
"errors"
"fmt"
"io"
"log"
"net/http"
"strings"
"github.com/ScoreTrak/ScoreTrak/pkg/exec"
)
type HTTP struct {
ExpectedOutput string
Scheme string
Port string
Path string
Subdomain string
// Todo: Implement Expected response code
}
func NewHTTP() *HTTP {
f := HTTP{Scheme: "http"}
return &f
}
func (h *HTTP) Validate() error {
return nil
}
var ErrInvalidResponseCodeReceived = errors.New("invalid response code received")
func (h *HTTP) Execute(e exec.Exec) (passed bool, logOutput string, err error) {
baseURL := ConstructURI(h.Port, h.Subdomain, e.Host, h.Path, h.Scheme)
req, err := http.NewRequest("GET", baseURL.String(), nil)
if err != nil {
return false, "", fmt.Errorf("unable to craft the request: %w", err)
}
req = req.WithContext(e.Context)
httpClient := http.DefaultClient
resp, err := httpClient.Do(req) //nolint:bodyclose
if err != nil {
return false, "", fmt.Errorf("unable to craft the request: %w", err)
}
defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {
log.Println(fmt.Errorf("unable to close body: %w", err))
}
}(resp.Body)
if !(resp.StatusCode >= 200 && resp.StatusCode < 400) {
return false, "", fmt.Errorf("%w: %d", ErrInvalidResponseCodeReceived, resp.StatusCode)
}
if h.ExpectedOutput != "" {
buf := new(bytes.Buffer)
_, err = buf.ReadFrom(resp.Body)
if err != nil {
return false, "", fmt.Errorf("unable to read response body: %w", err)
}
newStr := buf.String()
if !strings.Contains(newStr, h.ExpectedOutput) {
return false, "", fmt.Errorf("%w. Output Received: %s", ErrDidNotMatchExpectedOutput, newStr)
}
}
return true, Success, nil
}