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

mendersoftware / deviceauth / 1467711994

13 Sep 2024 11:01AM UTC coverage: 81.326%. Remained the same
1467711994

push

gitlab-ci

web-flow
Merge pull request #727 from mzedel/chore/deprecate

Chore/deprecate

4834 of 5944 relevant lines covered (81.33%)

39.6 hits per line

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

100.0
/api/http/routing.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 http
15

16
import (
17
        "net/http"
18

19
        "github.com/ant0ine/go-json-rest/rest"
20

21
        "github.com/mendersoftware/deviceauth/utils"
22
)
23

24
type HttpOptionsGenerator func(methods []string) rest.HandlerFunc
25

26
func AllowHeaderOptionsGenerator(methods []string) rest.HandlerFunc {
1,769✔
27
        // return a dummy handler for now
1,769✔
28
        return func(w rest.ResponseWriter, r *rest.Request) {
1,770✔
29
                for _, m := range methods {
3✔
30
                        w.Header().Add("Allow", m)
2✔
31
                }
2✔
32
        }
33
}
34

35
func supportsMethod(method string, methods []string) bool {
1,772✔
36
        return utils.ContainsString(method, methods)
1,772✔
37
}
1,772✔
38

39
// Automatically add OPTIONS method support for each defined route,
40
// only if there's no OPTIONS handler for that route yet
41
func AutogenOptionsRoutes(routes []*rest.Route, gen HttpOptionsGenerator) []*rest.Route {
95✔
42

95✔
43
        methodGroups := make(map[string][]string, len(routes))
95✔
44

95✔
45
        for _, route := range routes {
2,425✔
46
                methods, ok := methodGroups[route.PathExp]
2,330✔
47
                if !ok {
4,100✔
48
                        methods = make([]string, 0)
1,770✔
49
                }
1,770✔
50

51
                methodGroups[route.PathExp] = append(methods, route.HttpMethod)
2,330✔
52
        }
53

54
        options := make([]*rest.Route, 0, len(methodGroups))
95✔
55
        for route, methods := range methodGroups {
1,865✔
56
                // skip if there's a handler for OPTIONS already
1,770✔
57
                if !supportsMethod(http.MethodOptions, methods) {
3,539✔
58
                        options = append(options,
1,769✔
59
                                rest.Options(route, gen(methods)))
1,769✔
60
                }
1,769✔
61
        }
62

63
        return append(routes, options...)
95✔
64
}
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