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

jeffotoni / quick / 258

18 Mar 2025 08:57PM UTC coverage: 53.46% (+0.05%) from 53.414%
258

push

circleci

jaquelineabreu
docs:add godoc comment in client

3 of 3 new or added lines in 1 file covered. (100.0%)

124 existing lines in 5 files now uncovered.

2704 of 5058 relevant lines covered (53.46%)

1975.98 hits per line

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

86.44
/quicktest.go
1
package quick
2

3
import (
4
        "bytes"
5
        "io"
6
        "net/http"
7
        "net/http/httptest"
8
)
9

10
const logDelimiter = "====================="
11

12
// QuickTestReturn defines the interface for handling HTTP test responses.
13
type QuickTestReturn interface {
14
        Body() []byte             // Returns the raw response body as a byte slice.
15
        BodyStr() string          // Returns the response body as a string.
16
        StatusCode() int          // Returns the HTTP status code.
17
        Response() *http.Response // Returns the full HTTP response object.
18
}
19

20
type (
21
        qTest struct {
22
                body       []byte
23
                bodyStr    string
24
                statusCode int
25
                response   *http.Response
26
        }
27

28
        // QuickMockTestServer defines a mock server configuration for testing.
29
        QuickMockTestServer struct {
30
                Client  *http.Client      // HTTP client to interact with the mock server.
31
                Port    int               // Port on which the mock server runs.
32
                URI     string            // The request URI for the test.
33
                Method  string            // The HTTP method (GET, POST, etc.).
34
                Headers map[string]string // Headers to be included in the request.
35
                Body    []byte            // Request body content.
36
        }
37
)
38

39
// QuickTest is a helper function to perform HTTP tests quickly.
40
//
41
// It creates an HTTP request using the specified method, URI, and optional body,
42
// then executes the request within Quick's internal router and returns the response.
43
//
44
// Parameters:
45
//   - method (string): HTTP method (e.g., "GET", "POST").
46
//   - URI (string): The request path (e.g., "/test/:param").
47
//   - headers (map[string]string): Optional headers to include in the request.
48
//   - body (optional []byte): Request body, used for methods like POST.
49
//
50
// Returns:
51
//   - QuickTestReturn: The response object containing the body, status code, and full HTTP response.
52
//   - error: Any error encountered during the request creation or execution.
53
//
54
// Example Usage:
55
//
56
//        resp, err := q.QuickTest("GET", "/api/user", nil)
57
//        if err != nil {
58
//            log.Fatal(err)
59
//        }
60
//        fmt.Println("Status Code:", resp.StatusCode())
61
//        fmt.Println("Response Body:", resp.BodyStr())
62
func (q Quick) QuickTest(method, URI string, headers map[string]string, body ...[]byte) (QuickTestReturn, error) {
76✔
63
        requestBody := []byte{}
76✔
64
        if len(body) > 0 {
123✔
65
                requestBody = body[0]
47✔
66
        }
47✔
67

68
        // Log request details for debugging purposes.
69
        logRequestDetails(method, URI, len(requestBody))
76✔
70

76✔
71
        // Create the HTTP request.
76✔
72
        req, err := createHTTPRequest(method, URI, headers, requestBody)
76✔
73
        if err != nil {
76✔
74
                return nil, err
×
75
        }
×
76

77
        // Create a new HTTP response recorder to capture the output.
78
        rec := httptest.NewRecorder()
76✔
79
        q.ServeHTTP(rec, req)
76✔
80

76✔
81
        // Retrieve the recorded response.
76✔
82
        resp := rec.Result()
76✔
83
        responseBody, err := readResponseBody(resp.Body)
76✔
84
        if err != nil {
76✔
85
                return nil, err
×
86
        }
×
87

88
        return &qTest{
76✔
89
                body:       responseBody,
76✔
90
                bodyStr:    string(responseBody),
76✔
91
                statusCode: resp.StatusCode,
76✔
92
                response:   resp,
76✔
93
        }, nil
76✔
94
}
95

96
// createHTTPRequest constructs an HTTP request with the specified method, URI, headers, and body.
97
//
98
// Parameters:
99
//   - method (string): HTTP method (e.g., "GET", "POST").
100
//   - URI (string): The request path (e.g., "/api/test").
101
//   - headers (map[string]string): Headers to include in the request.
102
//   - body ([]byte): The request body content.
103
//
104
// Returns:
105
//   - *http.Request: The constructed HTTP request.
106
//   - error: Any error encountered while creating the request.
107
func createHTTPRequest(method, URI string, headers map[string]string, body []byte) (*http.Request, error) {
76✔
108
        req, err := http.NewRequest(method, URI, io.NopCloser(bytes.NewBuffer(body)))
76✔
109
        if err != nil {
76✔
UNCOV
110
                return nil, err
×
UNCOV
111
        }
×
112
        for key, value := range headers {
97✔
113
                req.Header.Set(key, value)
21✔
114
        }
21✔
115
        return req, nil
76✔
116
}
117

118
// readResponseBody safely reads and returns the response body as a byte slice.
119
//
120
// Parameters:
121
//   - body (io.ReadCloser): The response body stream.
122
//
123
// Returns:
124
//   - []byte: The content of the response body.
125
//   - error: Any error encountered while reading the body.
126
func readResponseBody(body io.ReadCloser) ([]byte, error) {
76✔
127
        if body == nil {
76✔
UNCOV
128
                return nil, nil
×
UNCOV
129
        }
×
130
        defer body.Close()
76✔
131
        return io.ReadAll(body)
76✔
132
}
133

134
// logRequestDetails logs the details of an HTTP request.
135
//
136
// This function prints the HTTP method, URI, and body length for debugging purposes.
137
//
138
// Parameters:
139
//   - method (string): The HTTP method used in the request.
140
//   - URI (string): The request path.
141
//   - bodyLen (int): The length of the request body.
142
func logRequestDetails(method, URI string, bodyLen int) {
76✔
143
        println(logDelimiter)
76✔
144
        println("Method:", method, "| URI:", URI, "| Body Length:", bodyLen)
76✔
145
        println(logDelimiter)
76✔
146
}
76✔
147

148
// Body returns the raw response body as a byte slice.
149
func (qt *qTest) Body() []byte {
16✔
150
        return qt.body
16✔
151
}
16✔
152

153
// BodyStr returns the response body as a string.
154
func (qt *qTest) BodyStr() string {
111✔
155
        return qt.bodyStr
111✔
156
}
111✔
157

158
// StatusCode returns the HTTP status code of the response.
159
func (qt *qTest) StatusCode() int {
78✔
160
        return qt.statusCode
78✔
161
}
78✔
162

163
// Response returns the full HTTP response object.
164
func (qt *qTest) Response() *http.Response {
28✔
165
        return qt.response
28✔
166
}
28✔
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