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

mendersoftware / mender-server / 1495380963

14 Oct 2024 03:35PM UTC coverage: 70.373% (-2.5%) from 72.904%
1495380963

Pull #101

gitlab-ci

mineralsfree
feat: tenant list added

Ticket: MEN-7568
Changelog: None

Signed-off-by: Mikita Pilinka <mikita.pilinka@northern.tech>
Pull Request #101: feat: tenant list added

4406 of 6391 branches covered (68.94%)

Branch coverage included in aggregate %.

88 of 183 new or added lines in 10 files covered. (48.09%)

2623 existing lines in 65 files now uncovered.

36673 of 51982 relevant lines covered (70.55%)

31.07 hits per line

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

80.46
/backend/services/workflows/app/worker/http.go
1
// Copyright 2022 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 worker
16

17
import (
18
        "encoding/json"
19
        "fmt"
20
        "io"
21
        "net/http"
22
        "net/url"
23
        "strings"
24
        "time"
25

26
        "github.com/mendersoftware/mender-server/pkg/log"
27

28
        "github.com/mendersoftware/mender-server/services/workflows/app/processor"
29
        "github.com/mendersoftware/mender-server/services/workflows/model"
30
)
31

UNCOV
32
var makeHTTPRequest = func(req *http.Request, timeout time.Duration) (*http.Response, error) {
×
UNCOV
33
        var httpClient = &http.Client{
×
UNCOV
34
                Timeout: timeout,
×
UNCOV
35
        }
×
UNCOV
36
        res, err := httpClient.Do(req)
×
UNCOV
37
        if err != nil {
×
UNCOV
38
                return nil, err
×
UNCOV
39
        }
×
UNCOV
40
        return res, nil
×
41
}
42

43
func processHTTPTask(
44
        httpTask *model.HTTPTask,
45
        ps *processor.JobStringProcessor,
46
        jp *processor.JobProcessor,
47
        l *log.Logger,
48
) (*model.TaskResult, error) {
1✔
49
        uri := ps.ProcessJobString(httpTask.URI)
1✔
50
        l.Infof("processHTTPTask: starting with: method=%s uri=%s",
1✔
51
                httpTask.Method,
1✔
52
                uri,
1✔
53
        )
1✔
54

1✔
55
        var payloadString string
1✔
56
        if len(httpTask.FormData) > 0 {
2✔
57
                form := url.Values{}
1✔
58
                for key, value := range httpTask.FormData {
2✔
59
                        key = ps.ProcessJobString(key)
1✔
60
                        value = ps.ProcessJobString(value)
1✔
61
                        form.Add(key, value)
1✔
62
                }
1✔
63
                payloadString = form.Encode()
1✔
64
        } else if httpTask.JSON != nil {
2✔
65
                payloadJSON := jp.ProcessJSON(httpTask.JSON, ps)
1✔
66
                payloadBytes, err := json.Marshal(payloadJSON)
1✔
67
                if err != nil {
1✔
68
                        return nil, err
×
69
                }
×
70
                payloadString = string(payloadBytes)
1✔
71
        } else {
1✔
72
                payloadString = ps.ProcessJobString(httpTask.Body)
1✔
73
        }
1✔
74
        payload := strings.NewReader(payloadString)
1✔
75

1✔
76
        req, err := http.NewRequest(httpTask.Method, uri, payload)
1✔
77
        if err != nil {
1✔
78
                return nil, err
×
79
        }
×
80

81
        if httpTask.ContentType != "" {
1✔
UNCOV
82
                req.Header.Add("Content-Type", httpTask.ContentType)
×
UNCOV
83
        }
×
84

85
        var headersToBeSent []string
1✔
86
        for name, value := range httpTask.Headers {
2✔
87
                headerValue := ps.ProcessJobString(value)
1✔
88
                req.Header.Add(name, headerValue)
1✔
89
                headersToBeSent = append(headersToBeSent,
1✔
90
                        fmt.Sprintf("%s: %s", name, headerValue))
1✔
91
        }
1✔
92

93
        l.Debugf("processHTTPTask makeHTTPRequest '%v'", req)
1✔
94
        res, err := makeHTTPRequest(req, time.Duration(httpTask.ReadTimeOut)*time.Second)
1✔
95
        l.Debugf("processHTTPTask makeHTTPRequest returned '%v','%v'", res, err)
1✔
96
        if err != nil {
1✔
UNCOV
97
                return nil, err
×
UNCOV
98
        }
×
99

100
        defer res.Body.Close()
1✔
101
        resBody, _ := io.ReadAll(res.Body)
1✔
102

1✔
103
        var success bool
1✔
104
        if len(httpTask.StatusCodes) == 0 {
2✔
105
                success = true
1✔
106
                if res.StatusCode >= 400 {
2✔
107
                        success = false
1✔
108
                }
1✔
109
        } else {
1✔
110
                success = false
1✔
111
                for _, statusCode := range httpTask.StatusCodes {
2✔
112
                        if statusCode == res.StatusCode {
2✔
113
                                success = true
1✔
114
                                break
1✔
115
                        }
116
                }
117
        }
118

119
        result := &model.TaskResult{
1✔
120
                Success: success,
1✔
121
                HTTPRequest: &model.TaskResultHTTPRequest{
1✔
122
                        URI:     uri,
1✔
123
                        Method:  httpTask.Method,
1✔
124
                        Body:    payloadString,
1✔
125
                        Headers: headersToBeSent,
1✔
126
                },
1✔
127
                HTTPResponse: &model.TaskResultHTTPResponse{
1✔
128
                        StatusCode: res.StatusCode,
1✔
129
                        Body:       string(resBody),
1✔
130
                },
1✔
131
        }
1✔
132

1✔
133
        return result, nil
1✔
134
}
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