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

fogfish / gurl / 11768627734

10 Nov 2024 09:25PM UTC coverage: 82.705% (-4.5%) from 87.236%
11768627734

Pull #67

github

fogfish
Simplify testing
Pull Request #67: Use opts lib to config http stack

30 of 36 new or added lines in 3 files covered. (83.33%)

1 existing line in 1 file now uncovered.

966 of 1168 relevant lines covered (82.71%)

0.92 hits per line

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

86.54
/http/stack.go
1
//
2
// Copyright (C) 2019 - 2023 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
        "context"
13
        "net"
14
        "net/http"
15
        "time"
16

17
        "github.com/fogfish/opts"
18
)
19

20
//
21
// The file implements the protocol stack, type owning HTTP client
22
//
23

24
// Creates instance of HTTP Request
25
func NewRequest(method, url string) (*http.Request, error) {
1✔
26
        return http.NewRequest(method, url, nil)
1✔
27
}
1✔
28

29
// Stack is HTTP protocol stack
30
type Stack interface {
31
        WithContext(context.Context) *Context
32
        IO(context.Context, ...Arrow) error
33
}
34

35
type Socket interface {
36
        Do(req *http.Request) (*http.Response, error)
37
}
38

39
// Protocol is an instance of Stack
40
type Protocol struct {
41
        Socket
42
        Host     string
43
        LogLevel int
44
        Memento  bool
45
}
46

47
// New instance of HTTP Stack
48
func New(opt ...Option) Stack {
1✔
49
        cat, err := NewStack(opt...)
1✔
50
        if err != nil {
1✔
NEW
51
                panic(err)
×
52
        }
53

54
        return cat
1✔
55
}
56

57
// New instance of HTTP Stack
58
func NewStack(opt ...Option) (Stack, error) {
1✔
59
        cat := &Protocol{Socket: Client()}
1✔
60

1✔
61
        if err := opts.Apply(cat, opt); err != nil {
1✔
NEW
62
                return nil, err
×
UNCOV
63
        }
×
64

65
        return cat, nil
1✔
66
}
67

68
// WithContext create instance of I/O Context
69
func (stack *Protocol) WithContext(ctx context.Context) *Context {
1✔
70
        return &Context{
1✔
71
                Context:  ctx,
1✔
72
                Host:     stack.Host,
1✔
73
                Method:   http.MethodGet,
1✔
74
                Request:  nil,
1✔
75
                Response: nil,
1✔
76
                stack:    stack,
1✔
77
        }
1✔
78
}
1✔
79

80
func (stack *Protocol) IO(ctx context.Context, arrows ...Arrow) error {
1✔
81
        c := stack.WithContext(ctx)
1✔
82

1✔
83
        for _, f := range arrows {
2✔
84
                if err := f(c); err != nil {
2✔
85
                        c.discardBody()
1✔
86
                        return err
1✔
87
                }
1✔
88
                if err := c.discardBody(); err != nil {
1✔
89
                        return err
×
90
                }
×
91
        }
92

93
        return nil
1✔
94
}
95

96
// Creates default HTTP client
97
func Client() *http.Client {
1✔
98
        return &http.Client{
1✔
99
                Timeout: 60 * time.Second,
1✔
100
                Transport: &http.Transport{
1✔
101
                        ReadBufferSize: 128 * 1024,
1✔
102
                        DialContext: (&net.Dialer{
1✔
103
                                Timeout: 10 * time.Second,
1✔
104
                        }).DialContext,
1✔
105
                        // TLSClientConfig:     &tls.Config{InsecureSkipVerify: true},
1✔
106
                        MaxIdleConns:        100,
1✔
107
                        MaxIdleConnsPerHost: 100,
1✔
108
                },
1✔
109
                CheckRedirect: func(req *http.Request, via []*http.Request) error {
1✔
110
                        return http.ErrUseLastResponse
×
111
                },
×
112
        }
113
}
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