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

mendersoftware / mender-server / 1841158960

28 May 2025 02:31PM UTC coverage: 65.865% (+0.08%) from 65.783%
1841158960

Pull #696

gitlab-ci

alfrunes
test(deviceconnect): Add error context to failing test

Signed-off-by: Alf-Rune Siqveland <alf.rune@northern.tech>
Pull Request #696: ci: Debug mongodb in backend unit tests

32569 of 49448 relevant lines covered (65.87%)

1.39 hits per line

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

94.85
/backend/services/inventory/utils/http.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

15
package utils
16

17
import (
18
        "errors"
19
        "fmt"
20
        "math"
21
        "net/url"
22
        "strconv"
23
        "strings"
24

25
        "github.com/ant0ine/go-json-rest/rest"
26
)
27

28
// pagination constants
29
const (
30
        PageName       = "page"
31
        PerPageName    = "per_page"
32
        PageMin        = 1
33
        PageDefault    = 1
34
        PerPageMin     = 1
35
        PerPageMax     = 500
36
        PerPageDefault = 20
37
        LinkHdr        = "Link"
38
        LinkTmpl       = "<%s?%s>; rel=\"%s\""
39
        LinkPrev       = "prev"
40
        LinkNext       = "next"
41
        LinkFirst      = "first"
42
        DefaultScheme  = "http"
43
)
44

45
// error msgs
46
func MsgQueryParmInvalid(name string) string {
1✔
47
        return fmt.Sprintf("Can't parse param %s", name)
1✔
48
}
1✔
49

50
func MsgQueryParmMissing(name string) string {
1✔
51
        return fmt.Sprintf("Missing required param %s", name)
1✔
52
}
1✔
53

54
func MsgQueryParmLimit(name string) string {
1✔
55
        return fmt.Sprintf("Param %s is out of bounds", name)
1✔
56
}
1✔
57

58
func MsgQueryParmOneOf(name string, allowed []string) string {
1✔
59
        return fmt.Sprintf("Param %s must be one of %v", name, allowed)
1✔
60
}
1✔
61

62
// query param parsing/validation
63
func ParseQueryParmUInt(
64
        r *rest.Request,
65
        name string,
66
        required bool,
67
        min,
68
        max,
69
        def uint64,
70
) (uint64, error) {
3✔
71
        strVal := r.URL.Query().Get(name)
3✔
72

3✔
73
        if strVal == "" {
6✔
74
                if required {
4✔
75
                        return 0, errors.New(MsgQueryParmMissing(name))
1✔
76
                } else {
4✔
77
                        return def, nil
3✔
78
                }
3✔
79
        }
80

81
        uintVal, err := strconv.ParseUint(strVal, 10, 32)
3✔
82
        if err != nil {
4✔
83
                return 0, errors.New(MsgQueryParmInvalid(name))
1✔
84
        }
1✔
85

86
        if uintVal < min || uintVal > max {
4✔
87
                return 0, errors.New(MsgQueryParmLimit(name))
1✔
88
        }
1✔
89

90
        return uintVal, nil
3✔
91
}
92

93
func ParseQueryParmBool(r *rest.Request, name string, required bool, def *bool) (*bool, error) {
2✔
94
        strVal := r.URL.Query().Get(name)
2✔
95

2✔
96
        if strVal == "" {
4✔
97
                if required {
2✔
98
                        return nil, errors.New(MsgQueryParmMissing(name))
×
99
                } else {
2✔
100
                        return def, nil
2✔
101
                }
2✔
102
        }
103

104
        boolVal, err := strconv.ParseBool(strVal)
1✔
105
        if err != nil {
1✔
106
                return nil, errors.New(MsgQueryParmInvalid(name))
×
107
        }
×
108

109
        return &boolVal, nil
1✔
110
}
111

112
func ParseQueryParmStr(
113
        r *rest.Request,
114
        name string,
115
        required bool,
116
        allowed []string,
117
) (string, error) {
3✔
118
        val := r.URL.Query().Get(name)
3✔
119

3✔
120
        if val == "" {
6✔
121
                if required {
4✔
122
                        return "", errors.New(MsgQueryParmMissing(name))
1✔
123
                }
1✔
124
        } else {
3✔
125
                if allowed != nil && !ContainsString(val, allowed) {
4✔
126
                        return "", errors.New(MsgQueryParmOneOf(name, allowed))
1✔
127
                }
1✔
128
        }
129

130
        val, err := url.QueryUnescape(val)
3✔
131
        if err != nil {
3✔
132
                return "", errors.New(MsgQueryParmInvalid(name))
×
133
        }
×
134

135
        return val, nil
3✔
136
}
137

138
// pagination helpers
139
func ParsePagination(r *rest.Request) (uint64, uint64, error) {
3✔
140
        page, err := ParseQueryParmUInt(r, PageName, false, PageMin, math.MaxUint64, PageDefault)
3✔
141
        if err != nil {
4✔
142
                return 0, 0, err
1✔
143
        }
1✔
144

145
        per_page, err := ParseQueryParmUInt(
3✔
146
                r,
3✔
147
                PerPageName,
3✔
148
                false,
3✔
149
                PerPageMin,
3✔
150
                PerPageMax,
3✔
151
                PerPageDefault,
3✔
152
        )
3✔
153
        if err != nil {
4✔
154
                return 0, 0, err
1✔
155
        }
1✔
156

157
        return page, per_page, nil
3✔
158
}
159

160
func MakePageLinkHdrs(r *rest.Request, page, per_page uint64, has_next bool) []string {
3✔
161
        var links []string
3✔
162

3✔
163
        pathitems := strings.Split(r.URL.Path, "/")
3✔
164
        resource := pathitems[len(pathitems)-1]
3✔
165
        query := r.URL.Query()
3✔
166

3✔
167
        if page > 1 {
5✔
168
                links = append(links, MakeLink(LinkPrev, resource, query, page-1, per_page))
2✔
169
        }
2✔
170

171
        if has_next {
6✔
172
                links = append(links, MakeLink(LinkNext, resource, query, page+1, per_page))
3✔
173
        }
3✔
174

175
        links = append(links, MakeLink(LinkFirst, resource, query, 1, per_page))
3✔
176
        return links
3✔
177
}
178

179
func MakeLink(link_type string, resource string, query url.Values, page, per_page uint64) string {
3✔
180
        query.Set(PageName, strconv.Itoa(int(page)))
3✔
181
        query.Set(PerPageName, strconv.Itoa(int(per_page)))
3✔
182

3✔
183
        return fmt.Sprintf(LinkTmpl, resource, query.Encode(), link_type)
3✔
184
}
3✔
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