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

mendersoftware / workflows / 1565759653

29 Nov 2024 07:56AM UTC coverage: 67.786% (-14.5%) from 82.255%
1565759653

push

gitlab-ci

web-flow
Merge pull request #336 from alfrunes/2.6.x

chore(deps): Update golang builder images to latest

1050 of 1549 relevant lines covered (67.79%)

5.01 hits per line

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

83.33
/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/ioutil"
21
        "net/http"
22
        "net/url"
23
        "strings"
24
        "time"
25

26
        "github.com/mendersoftware/go-lib-micro/log"
27

28
        "github.com/mendersoftware/workflows/app/processor"
29
        "github.com/mendersoftware/workflows/model"
30
)
31

32
var makeHTTPRequest = func(req *http.Request, timeout time.Duration) (*http.Response, error) {
×
33
        var httpClient = &http.Client{
×
34
                Timeout: timeout,
×
35
        }
×
36
        res, err := httpClient.Do(req)
×
37
        if err != nil {
×
38
                return nil, err
×
39
        }
×
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) {
13✔
49
        uri := ps.ProcessJobString(httpTask.URI)
13✔
50
        l.Infof("processHTTPTask: starting with: method=%s uri=%s",
13✔
51
                httpTask.Method,
13✔
52
                uri,
13✔
53
        )
13✔
54

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

13✔
79
        req, err := http.NewRequest(httpTask.Method, uri, payload)
13✔
80
        if err != nil {
13✔
81
                return nil, err
×
82
        }
×
83

84
        if httpTask.ContentType != "" {
15✔
85
                req.Header.Add("Content-Type", httpTask.ContentType)
2✔
86
        }
2✔
87

88
        var headersToBeSent []string
13✔
89
        for name, value := range httpTask.Headers {
19✔
90
                headerValue := ps.ProcessJobString(value)
6✔
91
                req.Header.Add(name, headerValue)
6✔
92
                headersToBeSent = append(headersToBeSent,
6✔
93
                        fmt.Sprintf("%s: %s", name, headerValue))
6✔
94
        }
6✔
95

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

103
        defer res.Body.Close()
13✔
104
        resBody, _ := ioutil.ReadAll(res.Body)
13✔
105

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

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

13✔
136
        return result, nil
13✔
137
}
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