aboutsummaryrefslogtreecommitdiff
path: root/http.go
blob: 75f657b9318191e079f9b4b760e4e41d97c3bed4 (plain) (blame)
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
package checkup

import (
	"errors"
	"fmt"
	"net/http"
	"time"
)

func httpReq(client *http.Client, url string, timeout, status int) error {
	resp, err := client.Get(url)

	if err != nil {
		return err
	}

	defer resp.Body.Close()

	if resp.StatusCode != status {
		return fmt.Errorf("Bad status code, expected %d got: %d",
			status, resp.StatusCode)
	}

	return nil
}

func HttpStatusOK(url string, timeout, status int) error {
	client := &http.Client{Timeout: time.Duration(timeout) * time.Second}
	return httpReq(client, url, timeout, status)
}

func HttpNoRedirectStatusOK(url string, timeout, status int) error {
	client := &http.Client{
		Timeout:       time.Duration(timeout) * time.Second,
		CheckRedirect: httpNoRedirectfunc,
	}
	return httpReq(client, url, timeout, status)
}

func httpNoRedirectfunc(req *http.Request, via []*http.Request) error {
	return errors.New("Redirected")
}