• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

philchia / agollo / 241

22 Mar 2023 11:51AM UTC coverage: 88.7% (-0.5%) from 89.237%
241

Pull #110

circleci

wzs
[fix] fix unit test
Pull Request #110: [feature] support timeout, retry. and fix http request error log

36 of 36 new or added lines in 4 files covered. (100.0%)

730 of 823 relevant lines covered (88.7%)

5.41 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

93.33
/request.go
1
package agollo
2

3
import (
4
        "errors"
5
        "fmt"
6
        "io"
7
        "io/ioutil"
8
        "net/http"
9
)
10

11
var ErrorStatusNotOK = errors.New("http resp code not ok")
12

13
// this is a static check
14
var _ requester = (*httpRequester)(nil)
15
var _ requester = (*httpSignRequester)(nil)
16

17
type requester interface {
18
        request(url string) ([]byte, error)
19
}
20

21
type httpRequester struct {
22
        client  *http.Client
23
        retries int
24
}
25

26
func newHTTPRequester(client *http.Client, retries int) requester {
3✔
27
        return &httpRequester{
3✔
28
                client:  client,
3✔
29
                retries: retries,
3✔
30
        }
3✔
31
}
3✔
32

33
func (r *httpRequester) request(url string) ([]byte, error) {
19✔
34
        return r.requestWithRetry(url, r.retries)
19✔
35
}
19✔
36

37
func (r *httpRequester) requestWithRetry(url string, retries int) ([]byte, error) {
22✔
38
        resp, err := r.client.Get(url)
22✔
39
        if err != nil {
26✔
40
                if retries > 0 {
7✔
41
                        return r.requestWithRetry(url, retries-1)
3✔
42
                }
3✔
43
                return nil, err
1✔
44
        }
45
        defer resp.Body.Close()
18✔
46

18✔
47
        if resp.StatusCode == http.StatusOK {
35✔
48
                return ioutil.ReadAll(resp.Body)
17✔
49
        }
17✔
50

51
        // Discard all body if status code is not 200
52
        _, _ = io.Copy(ioutil.Discard, resp.Body)
1✔
53
        return nil, fmt.Errorf("apollo return http resp code %d", resp.StatusCode)
1✔
54
}
55

56
type httpSignRequester struct {
57
        signature *signature
58
        client    *http.Client
59
        retries   int
60
}
61

62
func newHttpSignRequester(signature *signature, client *http.Client, retries int) requester {
1✔
63
        return &httpSignRequester{
1✔
64
                signature: signature,
1✔
65
                client:    client,
1✔
66
                retries:   retries,
1✔
67
        }
1✔
68
}
1✔
69

70
func (r *httpSignRequester) request(url string) ([]byte, error) {
3✔
71
        return r.requestWithRetry(url, r.retries)
3✔
72
}
3✔
73

74
func (r *httpSignRequester) requestWithRetry(url string, retries int) ([]byte, error) {
3✔
75

3✔
76
        req, err := http.NewRequest("GET", url, nil)
3✔
77
        if err != nil {
3✔
78
                if retries > 0 {
×
79
                        return r.requestWithRetry(url, retries-1)
×
80
                }
×
81
                return nil, err
×
82
        }
83

84
        timestamp := r.signature.getTimestamp()
3✔
85
        req.Header.Set(signHttpHeaderAuthorization, fmt.Sprintf(
3✔
86
                signAuthorizationFormat,
3✔
87
                r.signature.AppID,
3✔
88
                r.signature.getAuthorization(url, timestamp),
3✔
89
        ))
3✔
90
        req.Header.Set(signHttpHeaderTimestamp, timestamp)
3✔
91

3✔
92
        resp, err := r.client.Do(req)
3✔
93
        if err != nil {
4✔
94
                return nil, err
1✔
95
        }
1✔
96
        defer resp.Body.Close()
2✔
97

2✔
98
        if resp.StatusCode == http.StatusOK {
3✔
99
                return ioutil.ReadAll(resp.Body)
1✔
100
        }
1✔
101

102
        // Discard all body if status code is not 200
103
        _, _ = io.Copy(ioutil.Discard, resp.Body)
1✔
104
        return nil, fmt.Errorf("apollo return http resp code %d", resp.StatusCode)
1✔
105
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc