Repository navigation
Expand file tree
/
Copy pathmain_test.go
More file actions
306 lines (280 loc) · 9.08 KB
/
Copy pathmain_test.go
File metadata and controls
306 lines (280 loc) · 9.08 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
package main
import (
"crypto/tls"
"crypto/x509"
"net/url"
"os"
"path/filepath"
"slices"
"strings"
"testing"
"unicode/utf8"
"github.com/ryanfowler/fetch/internal/aws"
"github.com/ryanfowler/fetch/internal/cli"
"github.com/ryanfowler/fetch/internal/config"
"github.com/ryanfowler/fetch/internal/core"
"github.com/ryanfowler/fetch/internal/format"
)
func TestHelpVerboseRequested(t *testing.T) {
tests := []struct {
name string
args []string
want bool
}{
{name: "ordinary help", args: []string{"--help"}},
{name: "long flags", args: []string{"--verbose", "--help"}, want: true},
{name: "short flags", args: []string{"-vh"}, want: true},
{name: "clustered verbose flags", args: []string{"-vv", "-h"}, want: true},
{name: "after positional", args: []string{"example.com", "--help", "-v"}, want: true},
{name: "after separator", args: []string{"--", "--help", "-v"}},
}
app, err := cli.Parse(nil)
if err != nil {
t.Fatalf("Parse() error = %v", err)
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
if got := helpVerboseRequested(test.args, app); got != test.want {
t.Fatalf("helpVerboseRequested(%q) = %t, want %t", test.args, got, test.want)
}
})
}
t.Run("does not treat an option value as verbose", func(t *testing.T) {
if helpVerboseRequested([]string{"--method", "-v", "--help"}, app) {
t.Fatal("method value was treated as a verbosity flag")
}
})
}
func TestConciseHelpLinesStayWithin80Columns(t *testing.T) {
app, err := cli.Parse(nil)
if err != nil {
t.Fatalf("Parse() error = %v", err)
}
p := core.TestPrinter(false)
printConciseHelp(app, p)
lines := strings.Split(string(p.Bytes()), "\n")
if len(lines) < 10 {
t.Fatalf("concise help unexpectedly short: %d lines", len(lines))
}
for i, line := range lines {
if n := utf8.RuneCountInString(line); n > 80 {
t.Fatalf("help line %d is %d columns, want <= 80: %q", i+1, n, line)
}
}
}
func TestVerboseHelpPrinterUsesTerminalMarkdownRendering(t *testing.T) {
p := newVerboseHelpPrinter(false, true)
if !p.IsTerminal() {
t.Fatal("verbose help printer is not terminal-aware")
}
if err := format.FormatMarkdown([]byte("# Help\n\n[docs](https://example.com/docs)"), p); err != nil {
t.Fatalf("FormatMarkdown() error = %v", err)
}
got := string(p.Bytes())
if strings.Contains(got, "# Help") {
t.Fatalf("terminal help retained heading marker: %q", got)
}
if !strings.Contains(got, "\x1b]8;;https://example.com/docs\x1b\\docs\x1b]8;;\x1b\\") {
t.Fatalf("terminal help omitted OSC 8 link: %q", got)
}
}
func TestParseConfigFileMergesScopesAndRecordsProvenance(t *testing.T) {
configPath := filepath.Join(t.TempDir(), "config")
configData := []byte("header = X-Global: global\nquery = scope=global\n\n[example.com]\nheader = X-Host: host\nquery = scope=host\n")
if err := os.WriteFile(configPath, configData, 0600); err != nil {
t.Fatalf("unable to write config: %v", err)
}
app, err := cli.Parse([]string{
"--config", configPath,
"--header", "X-CLI: cli",
"--query", "scope=cli",
"https://example.com",
})
if err != nil {
t.Fatalf("Parse() error = %v", err)
}
if _, err := parseConfigFile(app); err != nil {
t.Fatalf("parseConfigFile() error = %v", err)
}
if got := app.Cfg.Headers; !slices.Equal(got, []core.KeyVal[string]{
{Key: "X-Global", Val: "global"},
{Key: "X-Host", Val: "host"},
{Key: "X-CLI", Val: "cli"},
}) {
t.Fatalf("headers = %+v", got)
}
if got := app.Cfg.QueryParams; !slices.Equal(got, []core.KeyVal[string]{
{Key: "scope", Val: "global"},
{Key: "scope", Val: "host"},
{Key: "scope", Val: "cli"},
}) {
t.Fatalf("query params = %+v", got)
}
for _, name := range []string{"header", "query"} {
provenance := app.OptionProvenance(name)
for _, source := range []cli.OptionSource{cli.SourceGlobalConfig, cli.SourceHostConfig, cli.SourceCLI} {
if !provenance.Has(source) {
t.Fatalf("%s provenance = %v, missing %v", name, provenance.Sources, source)
}
}
}
}
func TestMetadataPresentationFlags(t *testing.T) {
app, err := cli.Parse([]string{"--pager", "on", "-v", "--help"})
if err != nil {
t.Fatalf("Parse() error = %v", err)
}
if app.Cfg.Pager != core.PagerOn {
t.Fatalf("pager mode = %v, want on", app.Cfg.Pager)
}
if getValue(app.Cfg.NoPager) {
t.Fatal("--pager on unexpectedly enabled no-pager")
}
}
func TestBuildInfoIncludesOptionalDependencies(t *testing.T) {
compact := string(core.GetBuildInfo())
if strings.Contains(compact, `"target_os"`) || strings.Contains(compact, `"target_arch"`) {
t.Fatalf("compact build info unexpectedly includes target settings: %s", compact)
}
if strings.Contains(compact, `"deps"`) {
t.Fatalf("compact build info unexpectedly includes dependencies: %s", compact)
}
verbose := string(core.GetBuildInfo(true))
if !strings.Contains(verbose, `"deps"`) {
t.Fatalf("verbose build info lacks dependencies: %s", verbose)
}
}
func TestIgnoredInspectionFlags(t *testing.T) {
app := inspectionFlagTestApp(t)
common := []string{
"--data/--json/--xml",
"--form",
"--multipart",
"--grpc",
"--grpc-describe",
"--grpc-list",
"--output",
"--remote-name",
"--remote-header-name",
"--copy",
"--method",
"--header",
"--query",
"--edit",
"--session",
"--retry",
"--range",
"--timing",
"--proxy",
"--discard",
"--unix",
}
if got := ignoredInspectionFlags(app, inspectionTLS); !slices.Equal(got, common) {
t.Fatalf("ignoredInspectionFlags(inspectionTLS) = %v, want %v", got, common)
}
wantDNS := append(slices.Clone(common),
"--inspect-tls",
"--bearer",
"--basic",
"--digest",
"--aws-sigv4",
"--cert",
"--key",
"--format",
"--dry-run",
)
if got := ignoredInspectionFlags(app, inspectionDNS); !slices.Equal(got, wantDNS) {
t.Fatalf("ignoredInspectionFlags(inspectionDNS) = %v, want %v", got, wantDNS)
}
}
func TestWarnIgnoredInspectionFlagsDoesNotAddBlankLine(t *testing.T) {
p := core.TestPrinter(false)
warnIgnoredInspectionFlags(p, inspectionDNS, []string{"--timing"})
got := string(p.Bytes())
want := "warning: --inspect-dns ignores: --timing\n"
if got != want {
t.Fatalf("warning output = %q, want %q", got, want)
}
}
func TestIgnoredInspectionFlagsRequireExplicitProvenance(t *testing.T) {
configOnly := &cli.App{Cfg: config.Config{
Headers: []core.KeyVal[string]{{Key: "X-Config", Val: "yes"}},
}}
configOnly.RecordConfigSource(&configOnly.Cfg, cli.SourceGlobalConfig)
if got := ignoredInspectionFlags(configOnly, inspectionDNS); len(got) != 0 {
t.Fatalf("config-only inspection flags = %v, want none", got)
}
explicit, err := cli.Parse([]string{"--header", "X-CLI: yes", "https://example.com"})
if err != nil {
t.Fatalf("Parse() error = %v", err)
}
if got := ignoredInspectionFlags(explicit, inspectionDNS); !slices.Equal(got, []string{"--header"}) {
t.Fatalf("explicit inspection flags = %v, want [--header]", got)
}
fromCurl, err := cli.Parse([]string{"--from-curl", "curl -H 'X-Curl: yes' https://example.com"})
if err != nil {
t.Fatalf("Parse(--from-curl) error = %v", err)
}
if got := ignoredInspectionFlags(fromCurl, inspectionDNS); !slices.Equal(got, []string{"--header"}) {
t.Fatalf("curl inspection flags = %v, want [--header]", got)
}
fromCurlMultipart, err := cli.Parse([]string{"--from-curl", "curl -F field=value https://example.com"})
if err != nil {
t.Fatalf("Parse(--from-curl multipart) error = %v", err)
}
if got := ignoredInspectionFlags(fromCurlMultipart, inspectionDNS); !slices.Equal(got, []string{"--multipart", "--method"}) {
t.Fatalf("curl multipart inspection flags = %v, want [--multipart --method]", got)
}
}
func inspectionFlagTestApp(t *testing.T) *cli.App {
t.Helper()
copyOutput := true
insecure := true
retry := 1
session := "session-name"
timing := true
tlsMax := uint16(tls.VersionTLS13)
tlsMin := uint16(tls.VersionTLS12)
proxyURL, err := url.Parse("http://proxy.test")
if err != nil {
t.Fatalf("url.Parse() error = %v", err)
}
return &cli.App{
AWSSigv4: &aws.Config{},
Basic: &core.KeyVal[string]{Key: "user", Val: "pass"},
Bearer: "token",
Data: strings.NewReader("body"),
Digest: &core.KeyVal[string]{Key: "user", Val: "pass"},
Discard: true,
DryRun: true,
Edit: true,
Form: []core.KeyVal[string]{{Key: "field", Val: "value"}},
GRPC: true,
GRPCDescribe: "service.Method",
GRPCList: true,
InspectTLS: true,
Method: "POST",
Multipart: []core.KeyVal[string]{{Key: "file", Val: "path"}},
Output: "out.txt",
Range: []string{"0-10"},
RemoteHeaderName: true,
RemoteName: true,
UnixSocket: "/tmp/fetch.sock",
Cfg: config.Config{
CACerts: []*x509.Certificate{{}},
CertPath: "client.pem",
Copy: ©Output,
Format: core.FormatOn,
Headers: []core.KeyVal[string]{{Key: "X-Test", Val: "1"}},
Insecure: &insecure,
KeyPath: "client-key.pem",
Proxy: proxyURL,
QueryParams: []core.KeyVal[string]{{Key: "q", Val: "1"}},
Retry: &retry,
Session: &session,
Timing: &timing,
TLSMax: &tlsMax,
TLSMin: &tlsMin,
},
}
}