-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnaming_scheme.go
66 lines (57 loc) · 2.08 KB
/
naming_scheme.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package hypert
import (
"fmt"
"os"
"path"
"strconv"
"sync"
)
// NamingScheme defines an interface that is used by hypert's test client to store or retrieve files with HTTP requests.
//
// FileNames returns a pair of filenames that request and response should be stored in,
// when Record Mode is active, and retrieved from when Replay Mode is active.
//
// File names should be fully qualified (not relative).
//
// Each invocation should yield a pair of file names that
// wasn't yielded before during lifetime of given hypert's http client.
//
// This method should be safe for concurrent use.
// This requirement can be skipped, if you are the user of the package,
// and know, that all invocations would be sequential.
type NamingScheme interface {
FileNames(RequestData) (reqFile, respFile string)
}
// SequentialNamingScheme should be initialized using NewSequentialNamingScheme function.
// It names the files following (<dir>/0.req.http, <dir>/1.resp.http), (<dir>/1.req.http, <dir>/1.resp.http) convention.
type SequentialNamingScheme struct {
dir string
requestIndex int
requestIndexMx sync.Mutex
}
// NewSequentialNamingScheme initializes SequentialNamingScheme naming scheme, that implements NamingScheme interface.
//
// 'dir' parameter indicates, in which directory should the sequential requests and responses be placed.
// The directory is created with 0760 permissions if doesn't exists.
// You can use DefaultTestdataDir function for a sane default directory name.
func NewSequentialNamingScheme(dir string) (*SequentialNamingScheme, error) {
err := os.MkdirAll(dir, 0o760)
if err != nil {
return nil, fmt.Errorf("error creating directory: %w", err)
}
return &SequentialNamingScheme{
dir: dir,
}, nil
}
func (s *SequentialNamingScheme) FileNames(_ RequestData) (reqFile, respFile string) {
s.requestIndexMx.Lock()
requestIndex := strconv.Itoa(s.requestIndex)
defer func() {
s.requestIndex++
s.requestIndexMx.Unlock()
}()
withDir := func(name string) string {
return path.Join(s.dir, name)
}
return withDir(requestIndex + ".req.http"), withDir(requestIndex + ".resp.http")
}