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

mendersoftware / mender-cli / 1762341830

10 Apr 2025 12:40PM UTC coverage: 31.802%. Remained the same
1762341830

Pull #276

gitlab-ci

alfrunes
chore: Remove deprecated library `io/ioutil`

Signed-off-by: Alf-Rune Siqveland <alf.rune@northern.tech>
Pull Request #276: chore: bump the golang-dependencies group with 2 updates

4 of 6 new or added lines in 6 files covered. (66.67%)

20 existing lines in 1 file now uncovered.

810 of 2547 relevant lines covered (31.8%)

1.68 hits per line

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

71.43
/client/common.go
1
// Copyright 2023 Northern.tech AS
2
//
3
//        Licensed under the Apache License, Version 2.0 (the "License");
4
//        you may not use this file except in compliance with the License.
5
//        You may obtain a copy of the License at
6
//
7
//            http://www.apache.org/licenses/LICENSE-2.0
8
//
9
//        Unless required by applicable law or agreed to in writing, software
10
//        distributed under the License is distributed on an "AS IS" BASIS,
11
//        WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
//        See the License for the specific language governing permissions and
13
//        limitations under the License.
14
package client
15

16
import (
17
        "crypto/tls"
18
        "fmt"
19
        "io"
20
        "net/http"
21
        "net/http/httputil"
22
        "strings"
23

24
        "github.com/pkg/errors"
25

26
        "github.com/mendersoftware/mender-cli/log"
27
)
28

29
const (
30
        httpErrorBoundary = 300
31
)
32

33
func NewHttpClient(skipVerify bool) *http.Client {
16✔
34
        tr := &http.Transport{
16✔
35
                TLSClientConfig: &tls.Config{InsecureSkipVerify: skipVerify},
16✔
36
        }
16✔
37

16✔
38
        return &http.Client{
16✔
39
                Transport: tr,
16✔
40
        }
16✔
41
}
16✔
42

43
func JoinURL(base, url string) string {
16✔
44
        url = strings.TrimPrefix(url, "/")
16✔
45
        if !strings.HasSuffix(base, "/") {
32✔
46
                base = base + "/"
16✔
47
        }
16✔
48
        return base + url
16✔
49
}
50

51
func DoGetRequest(token, urlPath string, client *http.Client) ([]byte, error) {
5✔
52
        req, err := http.NewRequest(http.MethodGet, urlPath, nil)
5✔
53
        if err != nil {
5✔
UNCOV
54
                return nil, errors.Wrap(err, "Failed to create HTTP request")
×
UNCOV
55
        }
×
56
        req.Header.Set("Authorization", "Bearer "+string(token))
5✔
57

5✔
58
        reqDump, err := httputil.DumpRequest(req, false)
5✔
59
        if err != nil {
5✔
UNCOV
60
                return nil, err
×
UNCOV
61
        }
×
62
        log.Verbf("sending request: \n%s", string(reqDump))
5✔
63

5✔
64
        rsp, err := client.Do(req)
5✔
65
        if err != nil {
5✔
UNCOV
66
                return nil, errors.Wrap(err, fmt.Sprintf("Get %s request failed", urlPath))
×
UNCOV
67
        }
×
68
        if rsp.StatusCode != 200 {
5✔
UNCOV
69
                return nil, fmt.Errorf("Get %s request failed with status %d\n", urlPath, rsp.StatusCode)
×
UNCOV
70
        }
×
71

72
        defer rsp.Body.Close()
5✔
73

5✔
74
        body, err := io.ReadAll(rsp.Body)
5✔
75
        if err != nil {
5✔
UNCOV
76
                return nil, err
×
UNCOV
77
        }
×
78

79
        return body, nil
5✔
80
}
81

82
func DoPostRequest(
83
        token, urlPath string,
84
        client *http.Client,
85
        requestBody io.Reader,
86
) ([]byte, error) {
1✔
87
        req, err := http.NewRequest(http.MethodPost, urlPath, requestBody)
1✔
88
        if err != nil {
1✔
UNCOV
89
                return nil, errors.Wrap(err, "Failed to create HTTP request")
×
UNCOV
90
        }
×
91
        req.Header.Set("Authorization", "Bearer "+string(token))
1✔
92
        req.Header.Set("Content-Type", "application/json; charset=UTF-8")
1✔
93

1✔
94
        reqDump, err := httputil.DumpRequest(req, false)
1✔
95
        if err != nil {
1✔
UNCOV
96
                return nil, err
×
UNCOV
97
        }
×
98
        log.Verbf("sending request: \n%s", string(reqDump))
1✔
99

1✔
100
        rsp, err := client.Do(req)
1✔
101
        if err != nil {
1✔
UNCOV
102
                return nil, errors.Wrap(err, fmt.Sprintf("Post %s request failed", urlPath))
×
UNCOV
103
        }
×
104
        if rsp.StatusCode > httpErrorBoundary {
1✔
UNCOV
105
                return nil, fmt.Errorf("Post %s request failed with status %d\n", urlPath, rsp.StatusCode)
×
UNCOV
106
        }
×
107

108
        defer rsp.Body.Close()
1✔
109

1✔
110
        body, err := io.ReadAll(rsp.Body)
1✔
111
        if err != nil {
1✔
UNCOV
112
                return nil, err
×
UNCOV
113
        }
×
114

115
        return body, nil
1✔
116
}
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

© 2025 Coveralls, Inc