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

mendersoftware / mender-server / 1861958901

10 Jun 2025 08:53AM UTC coverage: 65.74%. First build
1861958901

push

gitlab-ci

web-flow
Merge pull request #711 from mendersoftware/MEN-8235

Merge MEN-8235 into main

285 of 347 new or added lines in 13 files covered. (82.13%)

32499 of 49436 relevant lines covered (65.74%)

1.39 hits per line

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

0.0
/backend/pkg/testing/response.go
1
// Copyright 2024 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 testing
15

16
import (
17
        "encoding/json"
18
        "mime"
19
        "net/http/httptest"
20
        "strings"
21
        "testing"
22

23
        "github.com/ant0ine/go-json-rest/rest/test"
24
        "github.com/stretchr/testify/assert"
25
)
26

27
type Recorded struct {
28
        T        *testing.T
29
        Recorder *httptest.ResponseRecorder
30
}
31

32
// temporary workaround to not break deployments service
NEW
33
func CheckResponse(t *testing.T, want ResponseChecker, h *test.Recorded) {
×
NEW
34
        have := Recorded(*h)
×
NEW
35
        want.CheckStatus(t, &have)
×
NEW
36
        want.CheckHeaders(t, &have)
×
NEW
37
        want.CheckContentType(t, &have)
×
NEW
38
        want.CheckBody(t, &have)
×
NEW
39
}
×
40

NEW
41
func CheckHTTPResponse(t *testing.T, want ResponseChecker, have *Recorded) {
×
42
        want.CheckStatus(t, have)
×
43
        want.CheckHeaders(t, have)
×
44
        want.CheckContentType(t, have)
×
45
        want.CheckBody(t, have)
×
46
}
×
47

48
// ResponseChecker is a generic response checker, regardless of content-type.
49
type ResponseChecker interface {
50
        CheckStatus(t *testing.T, recorded *Recorded)
51
        CheckHeaders(t *testing.T, recorded *Recorded)
52
        CheckContentType(t *testing.T, recorded *Recorded)
53
        CheckBody(t *testing.T, recorded *Recorded)
54
}
55

56
// BaseResponse is used for testing any response with a selected content type.
57
// Implements ResponseChecker, provides base methods for common tests.
58
type BaseResponse struct {
59
        Status      int
60
        ContentType string
61
        Headers     map[string]string
62
        Body        interface{}
63
}
64

NEW
65
func (b *BaseResponse) CheckStatus(t *testing.T, recorded *Recorded) {
×
NEW
66
        assert.Equal(t, b.Status, recorded.Recorder.Code)
×
67
}
×
68

NEW
69
func (b *BaseResponse) CheckContentType(t *testing.T, recorded *Recorded) {
×
NEW
70
        // skip requests that have no body
×
NEW
71
        if b.Body == nil {
×
NEW
72
                return
×
NEW
73
        }
×
74

75
        mediaType, params, _ := mime.ParseMediaType(recorded.Recorder.Header().Get("Content-Type"))
×
76
        charset := params["charset"]
×
77

×
78
        if mediaType != b.ContentType {
×
79
                t.Errorf(
×
80
                        "Content-Type media type: %s expected, got: %s",
×
81
                        b.ContentType,
×
82
                        mediaType,
×
83
                )
×
84
        }
×
85

86
        if charset != "" && strings.ToUpper(charset) != "UTF-8" {
×
87
                t.Errorf(
×
88
                        "Content-Type charset: must be empty or UTF-8, got: %s",
×
89
                        charset,
×
90
                )
×
91
        }
×
92
}
93

NEW
94
func (b *BaseResponse) CheckHeaders(t *testing.T, recorded *Recorded) {
×
95
        for name, value := range b.Headers {
×
96
                assert.Equal(t, value, recorded.Recorder.Header().Get(name))
×
97
        }
×
98
}
99

NEW
100
func (b *BaseResponse) CheckBody(t *testing.T, recorded *Recorded) {
×
101
        if b.Body != nil {
×
NEW
102
                assert.Equal(t, b.Body.(string), recorded.Recorder.Body.String())
×
103
        }
×
104
}
105

106
// JSONResponse is used for testing 'application/json' responses.
107
// Embeds the BaseResponse (implements ResponseChecker), and overrides relevant methods.
108
type JSONResponse struct {
109
        BaseResponse
110
}
111

112
func NewJSONResponse(status int, headers map[string]string, body interface{}) *JSONResponse {
×
113
        return &JSONResponse{
×
114
                BaseResponse: BaseResponse{
×
115
                        Status:      status,
×
116
                        ContentType: "application/json",
×
117
                        Headers:     headers,
×
118
                        Body:        body,
×
119
                },
×
120
        }
×
121
}
×
122

NEW
123
func (j *JSONResponse) CheckBody(t *testing.T, recorded *Recorded) {
×
124
        if j.Body != nil {
×
125
                assert.NotEmpty(t, recorded.Recorder.Body.String())
×
126
                expected, err := json.Marshal(j.Body)
×
127
                assert.NoError(t, err)
×
128
                assert.JSONEq(t, string(expected), recorded.Recorder.Body.String())
×
129
        } else {
×
130
                assert.Empty(t, recorded.Recorder.Body.String())
×
131
        }
×
132
}
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