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

fogfish / gurl / 11768693122

10 Nov 2024 09:35PM UTC coverage: 82.962% (-4.3%) from 87.236%
11768693122

Pull #67

github

fogfish
test error path for New
Pull Request #67: Use opts lib to config http stack

32 of 36 new or added lines in 3 files covered. (88.89%)

969 of 1168 relevant lines covered (82.96%)

0.92 hits per line

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

84.62
/http/options.go
1
//
2
// Copyright (C) 2019 - 2024 Dmitry Kolesnikov
3
//
4
// This file may be modified and distributed under the terms
5
// of the MIT license.  See the LICENSE file for details.
6
// https://github.com/fogfish/gurl
7
//
8

9
package http
10

11
import (
12
        "crypto/tls"
13
        "fmt"
14
        "net/http"
15
        "net/http/cookiejar"
16

17
        "github.com/fogfish/opts"
18
        "golang.org/x/net/publicsuffix"
19
)
20

21
// HTTP Stack config option
22
type Option = opts.Option[Protocol]
23

24
var (
25
        // Set custom implementation of HTTP client.
26
        // It requires anything that implements Socket interface (aka http.Client)
27
        //
28
        //        type Socket interface {
29
        //          Do(req *http.Request) (*http.Response, error)
30
        //        }
31
        WithClient = opts.ForType[Protocol, Socket]()
32

33
        // Set the default host for http stack.
34
        // The host is used when request URI does not contain any host.
35
        WithHost = opts.ForName[Protocol, string]("Host")
36

37
        // Enables HTTP Response buffering
38
        WithMemento = opts.ForName[Protocol, bool]("Memento")
39

40
        // Buffers HTTP Response Payload into context.
41
        WithMementoPayload = WithMemento(true)
42

43
        // Disables TLS certificate validation for HTTP(S) sessions.
44
        WithInsecureTLS = opts.From(withInsecureTLS)
45

46
        // Enables automated cookie handling across requests originated from the session.
47
        WithCookieJar = opts.From(withCookieJar)
48

49
        // Disables default [gurl] redirect policy to Golang's one.
50
        // It enables the HTTP stack automatically follows redirects
51
        WithRedirects = opts.From(withRedirects)
52

53
        // Enable log level
54
        WithLogLevel = opts.ForName[Protocol, int]("LogLevel")
55

56
        // Enables debug logging.
57
        // The logger outputs HTTP requests only.
58
        WithDebugRequest = WithLogLevel(1)
59

60
        // Enables debug logging.
61
        // The logger outputs HTTP requests and responses.
62
        WithDebugResponse = WithLogLevel(2)
63

64
        // Enable debug logging.
65
        WithDebugPayload = WithLogLevel(3)
66
)
67

68
func withInsecureTLS(cat *Protocol) error {
1✔
69
        if cli, ok := cat.Socket.(*http.Client); ok {
2✔
70
                switch t := cli.Transport.(type) {
1✔
71
                case *http.Transport:
1✔
72
                        if t.TLSClientConfig == nil {
2✔
73
                                t.TLSClientConfig = &tls.Config{}
1✔
74
                        }
1✔
75
                        t.TLSClientConfig.InsecureSkipVerify = true
1✔
NEW
76
                default:
×
NEW
77
                        return fmt.Errorf("unsupported transport type %T", t)
×
78
                }
79
        }
80
        return nil
1✔
81
}
82

83
func withCookieJar(cat *Protocol) error {
1✔
84
        if cli, ok := cat.Socket.(*http.Client); ok {
2✔
85
                jar, err := cookiejar.New(&cookiejar.Options{
1✔
86
                        PublicSuffixList: publicsuffix.List,
1✔
87
                })
1✔
88
                if err != nil {
1✔
NEW
89
                        return err
×
NEW
90
                }
×
91
                cli.Jar = jar
1✔
92
        }
93
        return nil
1✔
94
}
95

96
func withRedirects(cat *Protocol) error {
1✔
97
        if cli, ok := cat.Socket.(*http.Client); ok {
2✔
98
                cli.CheckRedirect = nil
1✔
99
        }
1✔
100
        return nil
1✔
101
}
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