-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors_test.go
More file actions
75 lines (71 loc) · 1.77 KB
/
Copy patherrors_test.go
File metadata and controls
75 lines (71 loc) · 1.77 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
package httpparser
import (
"errors"
"testing"
)
func TestParseErrorKinds(t *testing.T) {
tests := []struct {
name string
input string
wantKind error
wantLine int
}{
{
name: "malformed request line",
input: "# comment\n\nNOTAMETHOD\n",
wantKind: ErrMalformedRequestLine,
wantLine: 3,
},
{
name: "unsupported method",
input: "TRACE https://example.com/x\n",
wantKind: ErrUnsupportedMethod,
wantLine: 1,
},
{
name: "malformed header",
input: "GET https://example.com/x\nnot-a-header-line\n",
wantKind: ErrMalformedHeader,
wantLine: 2,
},
{
name: "@var after first request falls through to request line",
input: "GET https://example.com/a\n###\n@late = value\n",
wantKind: ErrUnsupportedMethod,
wantLine: 3,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
_, err := ParseString(tt.input)
if err == nil {
t.Fatal("expected an error, got nil")
}
if !errors.Is(err, tt.wantKind) {
t.Errorf("errors.Is(err, %v) = false; err = %v", tt.wantKind, err)
}
var pe *ParseError
if !errors.As(err, &pe) {
t.Fatalf("err is not a *ParseError: %v", err)
}
if pe.Line != tt.wantLine {
t.Errorf("ParseError.Line = %d, want %d", pe.Line, tt.wantLine)
}
if pe.Detail == "" {
t.Error("ParseError.Detail is empty, want the offending text")
}
})
}
}
func TestParseFileMissing(t *testing.T) {
_, err := ParseFile("testdata/does-not-exist.http")
if err == nil {
t.Fatal("expected an error for a missing file, got nil")
}
// A missing file is an I/O error, not a syntax error, so it must NOT be a
// *ParseError.
var pe *ParseError
if errors.As(err, &pe) {
t.Errorf("missing-file error should not be a *ParseError, got %v", err)
}
}