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

mendersoftware / mender-cli / 1779499080

22 Apr 2025 10:11AM UTC coverage: 1.737% (-30.1%) from 31.802%
1779499080

push

gitlab-ci

web-flow
Merge pull request #277 from alfrunes/MEN-7794

MEN-7794: Add support for pagination when listing devices

28 of 82 new or added lines in 4 files covered. (34.15%)

770 existing lines in 17 files now uncovered.

45 of 2590 relevant lines covered (1.74%)

0.04 hits per line

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

0.0
/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

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

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

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

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

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

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

UNCOV
72
        defer rsp.Body.Close()
×
UNCOV
73

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

UNCOV
79
        return body, nil
×
80
}
81

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

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

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

UNCOV
108
        defer rsp.Body.Close()
×
UNCOV
109

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

UNCOV
115
        return body, nil
×
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