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

mendersoftware / mender-server / 1807452801

08 May 2025 01:22PM UTC coverage: 65.386% (+0.1%) from 65.27%
1807452801

Pull #653

gitlab-ci

bahaa-ghazal
refactor(inventory): Migrate from ant0nie/go-json-rest to gin-gonic/gin

Ticket: MEN-8236
Changelog: Title
Signed-off-by: Bahaa Aldeen Ghazal <bahaa.ghazal@northern.tech>
Pull Request #653: refactor(inventory): Migrate from ant0nie/go-json-rest to gin-gonic/gin

476 of 525 new or added lines in 6 files covered. (90.67%)

62 existing lines in 9 files now uncovered.

31949 of 48862 relevant lines covered (65.39%)

1.37 hits per line

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

90.57
/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
        "net/http"
21
        "net/url"
22
        "strconv"
23

24
        "github.com/mendersoftware/mender-server/pkg/rest.utils"
25
)
26

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

44
// error msgs
45
func MsgQueryParmMissing(name string) string {
1✔
46
        return fmt.Sprintf("Missing required param %s", name)
1✔
47
}
1✔
48

49
func MsgQueryParmOneOf(name string, allowed []string) string {
1✔
50
        return fmt.Sprintf("Param %s must be one of %v", name, allowed)
1✔
51
}
1✔
52

53
// query param parsing/validation
54
func ParseQueryParmUInt(
55
        r *http.Request,
56
        name string,
57
        required bool,
58
        min,
59
        max,
60
        def uint64,
61
) (uint64, error) {
1✔
62
        strVal := r.URL.Query().Get(name)
1✔
63

1✔
64
        if strVal == "" {
2✔
65
                if required {
2✔
66
                        return 0, errors.New(MsgQueryParmMissing(name))
1✔
67
                } else {
2✔
68
                        return def, nil
1✔
69
                }
1✔
70
        }
71

72
        uintVal, err := strconv.ParseUint(strVal, 10, 32)
1✔
73
        if err != nil {
2✔
74
                return 0, errors.New(rest.MsgQueryParmInvalid(name, strVal))
1✔
75
        }
1✔
76

77
        if uintVal < min || uintVal > max {
2✔
78
                return 0, errors.New(rest.MsgQueryParmLimit(name))
1✔
79
        }
1✔
80

81
        return uintVal, nil
1✔
82
}
83

84
func ParseQueryParmBool(r *http.Request, name string, required bool, def *bool) (*bool, error) {
2✔
85
        strVal := r.URL.Query().Get(name)
2✔
86

2✔
87
        if strVal == "" {
4✔
88
                if required {
2✔
89
                        return nil, errors.New(MsgQueryParmMissing(name))
×
90
                } else {
2✔
91
                        return def, nil
2✔
92
                }
2✔
93
        }
94

95
        boolVal, err := strconv.ParseBool(strVal)
1✔
96
        if err != nil {
1✔
NEW
97
                return nil, errors.New(rest.MsgQueryParmInvalid(name, strVal))
×
98
        }
×
99

100
        return &boolVal, nil
1✔
101
}
102

103
func ParseQueryParmStr(
104
        r *http.Request,
105
        name string,
106
        required bool,
107
        allowed []string,
108
) (string, error) {
3✔
109
        val := r.URL.Query().Get(name)
3✔
110

3✔
111
        if val == "" {
6✔
112
                if required {
4✔
113
                        return "", errors.New(MsgQueryParmMissing(name))
1✔
114
                }
1✔
115
        } else {
3✔
116
                if allowed != nil && !ContainsString(val, allowed) {
4✔
117
                        return "", errors.New(MsgQueryParmOneOf(name, allowed))
1✔
118
                }
1✔
119
        }
120

121
        val, err := url.QueryUnescape(val)
3✔
122
        if err != nil {
3✔
NEW
123
                return "", errors.New(rest.MsgQueryParmInvalid(name, val))
×
124
        }
×
125

126
        return val, nil
3✔
127
}
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