-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathformat_cov_test.go
More file actions
105 lines (93 loc) · 3.13 KB
/
Copy pathformat_cov_test.go
File metadata and controls
105 lines (93 loc) · 3.13 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
package har
import (
"bytes"
"testing"
"github.com/stretchr/testify/assert"
)
// Cover Har.ToYAML ToJSON-error branch (lines 25-27): inject an
// un-marshalable value (func) into Response.Error (any, json:"_error")
// so that ToJSON fails and ToYAML propagates the error.
func TestCovToYAML_ToJSONError(t *testing.T) {
h := NewHar()
e := h.AddEntry("GET", "https://example.com", "HTTP/1.1", "")
e.SetResponseStatus(200, "OK")
e.Response.Error = func() {} // unsupported by json.Marshal
out, err := h.ToYAML()
if err == nil {
t.Fatal("expected error from ToJSON failure in ToYAML, got nil")
}
if out != "" {
t.Fatalf("expected empty string on error, got %q", out)
}
assertHarErrorCode(t, err, ErrCodeJSONParse)
}
// Cover Har.ToYAML nil-receiver branch (line 19-21) and happy path.
func TestCovToYAML_NilAndHappy(t *testing.T) {
// nil receiver
var h *Har
out, err := h.ToYAML()
if err == nil {
t.Fatal("expected error for nil HAR, got nil")
}
assertHarErrorCode(t, err, ErrCodeInvalidFormat)
assert.Equal(t, "", out)
// happy path
hh := NewHar()
ee := hh.AddEntry("GET", "https://example.com", "HTTP/1.1", "")
ee.SetResponseStatus(200, "OK")
out, err = hh.ToYAML()
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if out == "" {
t.Fatal("expected non-empty YAML output")
}
}
// Cover ConvertTo error branch (lines 195-197): when the chosen format's
// underlying call returns an error. We trigger it by using FormatYAML with
// a HAR whose ToJSON fails (func in Response.Error), so ToYAML returns
// error and ConvertTo propagates it (line 195-197).
func TestCovConvertTo_ErrorBranch(t *testing.T) {
h := NewHar()
e := h.AddEntry("GET", "https://example.com", "HTTP/1.1", "")
e.SetResponseStatus(200, "OK")
e.Response.Error = func() {}
var buf bytes.Buffer
err := h.ConvertTo(FormatYAML, &buf, DefaultConvertOptions())
if err == nil {
t.Fatal("expected error from ConvertTo (YAML/ToJSON failure), got nil")
}
assertHarErrorCode(t, err, ErrCodeJSONParse)
}
// Cover ConvertTo nil-har branch (line 171-173) and nil-writer branch
// (lines 174-176).
func TestCovConvertTo_NilBranches(t *testing.T) {
var h *Har
err := h.ConvertTo(FormatYAML, &bytes.Buffer{}, DefaultConvertOptions())
if err == nil {
t.Fatal("expected error for nil HAR, got nil")
}
assertHarErrorCode(t, err, ErrCodeInvalidFormat)
hh := NewHar()
err = hh.ConvertTo(FormatYAML, nil, DefaultConvertOptions())
if err == nil {
t.Fatal("expected error for nil writer, got nil")
}
assertHarErrorCode(t, err, ErrCodeInvalidFormat)
}
// Cover ConvertTo default (JSON) branch (lines 186-192) and the
// writeAllToWriter success path (line 199).
func TestCovConvertTo_DefaultJSONBranch(t *testing.T) {
h := NewHar()
e := h.AddEntry("GET", "https://example.com", "HTTP/1.1", "")
e.SetResponseStatus(200, "OK")
// Unknown format falls into default -> ToJSON + writeAllToWriter (line 191).
var buf bytes.Buffer
err := h.ConvertTo(ConvertFormat("unknown-format"), &buf, DefaultConvertOptions())
if err != nil {
t.Fatalf("unexpected error on default JSON branch: %v", err)
}
if buf.Len() == 0 {
t.Fatal("expected non-empty output on default JSON branch")
}
}