-
Notifications
You must be signed in to change notification settings - Fork 0
/
smoke.go
73 lines (69 loc) · 1.91 KB
/
smoke.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
// Smoke crawls web links starting with the smoketest and urls arguments.
//
// This version uses bounded parallelism.
//
package controllers
import (
"fmt"
"net/http"
"net/url"
"strings"
"io/ioutil"
"github.com/alainglez/go-smoke/models"
)
//!+
func Smoke(smoketest *models.SmokeTest, testurls []models.TestUrl) {
// Create visit goroutines to fetch each link.
for i := 0; i < len(testurls)-1; i++ {
//go func() {
smoketest.UrlResults[i].Url = testurls[i].Url
statusCode, err := visit(smoketest.HostIp, &testurls[i])
smoketest.UrlResults[i].StatusCode = statusCode
smoketest.UrlResults[i].Err = err.Error()
if smoketest.PassFail == "FAIL" {
continue
} else {
if statusCode != http.StatusOK {
smoketest.PassFail = "FAIL"
} else {
smoketest.PassFail = "PASS"
}
}
//}()
}
}
// visit makes an HTTP GET request to the specified URL, parses
// the response as HTML, and returns the links in the HTML document.
func visit(hostIp string, testurl *models.TestUrl) (int, error) {
//hostip string, decodedurl string, htmlfragment string
var statuscode int
// replace domain with ip :80 | 443 depending if url has https or not
u, err := url.Parse(testurl.Url)
if err != nil {
return statuscode, err
}
if u.Scheme == "http" {
u.Host = hostIp + ":80"
}
if u.Scheme == "https" {
u.Host = hostIp + ":443"
}
resp, err := http.Get(u.String())
if err != nil {
return statuscode, err
}
statuscode = resp.StatusCode
defer resp.Body.Close()
if statuscode != http.StatusOK {
return statuscode, fmt.Errorf("getting %s: %s", u.String(), resp.Status)
}
contents, err := ioutil.ReadAll(resp.Body)
if err != nil {
return statuscode, fmt.Errorf("%s", err)
}
if strings.Contains(string(contents),testurl.HtmlFragment) {
return statuscode, nil
}
return statuscode, fmt.Errorf("couldn't find %s in %s", testurl.HtmlFragment, u.String())
}
//!-