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

jeffotoni / quick / 214

04 Mar 2025 07:11AM UTC coverage: 49.209% (-26.0%) from 75.221%
214

push

circleci

jeffotoni
Merge branch 'feature/refactory_http_client'

new version http client

32 of 1275 new or added lines in 44 files covered. (2.51%)

15 existing lines in 1 file now uncovered.

1803 of 3664 relevant lines covered (49.21%)

2724.12 hits per line

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

0.0
/example/quick.http.client/full.client/main.go
1
package main
2

3
import (
4
        "context"
5
        "crypto/tls"
6
        "encoding/json"
7
        "fmt"
8
        "io"
9
        "net/http"
10
        "net/http/httptest"
11
        "net/url"
12
        "time"
13

14
        "github.com/jeffotoni/quick"
15
        "github.com/jeffotoni/quick/http/client"
16
)
17

NEW
18
func main() {
×
NEW
19
        // Initialize the quick framework.
×
NEW
20
        q := quick.New()
×
NEW
21

×
NEW
22
        // Define routes using quick.
×
NEW
23
        q.Get("/get", func(c *quick.Ctx) error {
×
NEW
24
                return c.Status(200).SendString("GET OK")
×
NEW
25
        })
×
NEW
26
        q.Post("/post", func(c *quick.Ctx) error {
×
NEW
27
                body, err := io.ReadAll(c.Request.Body)
×
NEW
28
                if err != nil {
×
NEW
29
                        return c.Status(400).SendString(err.Error())
×
NEW
30
                }
×
NEW
31
                return c.Status(201).SendString("POST: " + string(body))
×
32
        })
NEW
33
        q.Put("/put", func(c *quick.Ctx) error {
×
NEW
34
                body, err := io.ReadAll(c.Request.Body)
×
NEW
35
                if err != nil {
×
NEW
36
                        return c.Status(400).SendString(err.Error())
×
NEW
37
                }
×
NEW
38
                return c.Status(200).SendString("PUT: " + string(body))
×
39
        })
NEW
40
        q.Delete("/delete", func(c *quick.Ctx) error {
×
NEW
41
                return c.Status(200).SendString("DELETE OK")
×
NEW
42
        })
×
NEW
43
        q.Post("/postform", func(c *quick.Ctx) error {
×
NEW
44
                // Assume FormValues returns map[string][]string.
×
NEW
45
                form := c.FormValues()
×
NEW
46
                vals := url.Values(form)
×
NEW
47
                return c.Status(200).SendString("POSTFORM: " + vals.Encode())
×
NEW
48
        })
×
49

50
        // Create a test server using the quick handler.
NEW
51
        ts := httptest.NewServer(q)
×
NEW
52
        defer ts.Close()
×
NEW
53

×
NEW
54
        // Creating a custom HTTP transport with advanced settings.
×
NEW
55
        customTransport := &http.Transport{
×
NEW
56
                Proxy: http.ProxyFromEnvironment, // Uses system proxy settings if available.
×
NEW
57
                TLSClientConfig: &tls.Config{
×
NEW
58
                        InsecureSkipVerify: true,             // Allows insecure TLS connections (not recommended for production).
×
NEW
59
                        MinVersion:         tls.VersionTLS12, // Enforces a minimum TLS version for security.
×
NEW
60
                },
×
NEW
61
                MaxIdleConns:        50,    // Maximum number of idle connections across all hosts.
×
NEW
62
                MaxConnsPerHost:     30,    // Maximum simultaneous connections per host.
×
NEW
63
                MaxIdleConnsPerHost: 10,    // Maximum number of idle connections per host.
×
NEW
64
                DisableKeepAlives:   false, // Enables persistent connections (Keep-Alive).
×
NEW
65
        }
×
NEW
66

×
NEW
67
        // Creating a fully custom *http.Client with the transport and timeout settings.
×
NEW
68
        customHTTPClient := &http.Client{
×
NEW
69
                Timeout:   15 * time.Second, // Global timeout for all requests.
×
NEW
70
                Transport: customTransport,  // Uses the custom transport.
×
NEW
71
        }
×
NEW
72

×
NEW
73
        // Create a client with extended options.
×
NEW
74
        cClient := client.New(
×
NEW
75
                client.WithTimeout(5*time.Second),
×
NEW
76
                client.WithDisableKeepAlives(false),
×
NEW
77
                client.WithMaxIdleConns(20),
×
NEW
78
                client.WithMaxConnsPerHost(20),
×
NEW
79
                client.WithMaxIdleConnsPerHost(20),
×
NEW
80
                client.WithContext(context.Background()),
×
NEW
81
                client.WithCustomHTTPClient(customHTTPClient),
×
NEW
82
                client.WithHeaders(map[string]string{
×
NEW
83
                        "Content-Type":  "application/json",
×
NEW
84
                        "Authorization": "Bearer EXAMPLE_TOKEN",
×
NEW
85
                }),
×
NEW
86
                // Set transport options via WithTransportConfig.
×
NEW
87

×
NEW
88
                // WithTransportConfig(&http.Transport{
×
NEW
89
                //         Proxy:               http.ProxyFromEnvironment,
×
NEW
90
                //         TLSClientConfig:     &tls.Config{InsecureSkipVerify: true},
×
NEW
91
                //         ForceAttemptHTTP2:   true,
×
NEW
92
                //         MaxIdleConns:        20,
×
NEW
93
                //         MaxIdleConnsPerHost: 10,
×
NEW
94
                //         MaxConnsPerHost:     20,
×
NEW
95
                //         DisableKeepAlives:   false,
×
NEW
96
                // }),
×
NEW
97

×
NEW
98
                // Override transport with retry RoundTripper.
×
NEW
99
                // WithRetryRoundTripper(RetryConfig{
×
NEW
100
                //         MaxRetries: 2,
×
NEW
101
                //         Delay:      1 * time.Second,
×
NEW
102
                //         UseBackoff: true,
×
NEW
103
                //         Statuses:   []int{500},
×
NEW
104
                //         EnableLog:  false,
×
NEW
105
                // }),
×
NEW
106

×
NEW
107
                // Also configure client retry settings (for manual retry logic).
×
NEW
108
                client.WithRetry(client.RetryConfig{
×
NEW
109
                        MaxRetries: 2,
×
NEW
110
                        Delay:      1 * time.Second,
×
NEW
111
                        UseBackoff: true,
×
NEW
112
                        Statuses:   []int{500},
×
NEW
113
                        EnableLog:  false,
×
NEW
114
                }),
×
NEW
115
        )
×
NEW
116

×
NEW
117
        // GET request.
×
NEW
118
        resp, err := cClient.Get(ts.URL + "/get")
×
NEW
119
        if err != nil {
×
NEW
120
                fmt.Println("GET Error:", err)
×
NEW
121
                return
×
NEW
122
        }
×
NEW
123
        fmt.Println("GET:", string(resp.Body))
×
NEW
124

×
NEW
125
        // POST request with a string body.
×
NEW
126
        resp, err = cClient.Post(ts.URL+"/post", "Hello, extended POST!")
×
NEW
127
        if err != nil {
×
NEW
128
                fmt.Println("POST Error:", err)
×
NEW
129
                return
×
NEW
130
        }
×
NEW
131
        fmt.Println("POST:", string(resp.Body))
×
NEW
132

×
NEW
133
        // PUT request with a struct body (marshaled to JSON).
×
NEW
134
        data := struct {
×
NEW
135
                Data string `json:"data"`
×
NEW
136
        }{
×
NEW
137
                Data: "Hello, extended PUT!",
×
NEW
138
        }
×
NEW
139
        resp, err = cClient.Put(ts.URL+"/put", data)
×
NEW
140
        if err != nil {
×
NEW
141
                fmt.Println("PUT Error:", err)
×
NEW
142
                return
×
NEW
143
        }
×
144
        // To display the JSON response as a string, unmarshal and marshal it back.
NEW
145
        var putResult map[string]string
×
NEW
146
        _ = json.Unmarshal(resp.Body, &putResult)
×
NEW
147
        putJSON, _ := json.Marshal(putResult)
×
NEW
148
        fmt.Println("PUT:", string(putJSON))
×
NEW
149

×
NEW
150
        // DELETE request.
×
NEW
151
        resp, err = cClient.Delete(ts.URL + "/delete")
×
NEW
152
        if err != nil {
×
NEW
153
                fmt.Println("DELETE Error:", err)
×
NEW
154
                return
×
NEW
155
        }
×
NEW
156
        fmt.Println("DELETE:", string(resp.Body))
×
NEW
157

×
NEW
158
        // POSTFORM request.
×
NEW
159
        formData := url.Values{}
×
NEW
160
        formData.Set("key", "value")
×
NEW
161
        resp, err = cClient.PostForm(ts.URL+"/postform", formData)
×
NEW
162
        if err != nil {
×
NEW
163
                fmt.Println("POSTFORM Error:", err)
×
NEW
164
                return
×
NEW
165
        }
×
NEW
166
        fmt.Println("POSTFORM:", string(resp.Body))
×
167

168
        // Output:
169
        // GET: GET OK
170
        // POST: POST: Hello, extended POST!
171
        // PUT: PUT: {"data":"Hello, extended PUT!"}
172
        // DELETE: DELETE OK
173
        // POSTFORM: POSTFORM: key=value
174
}
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