generated from dnnrly/goclitem
-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
httpref_test.go
158 lines (136 loc) · 4.06 KB
/
httpref_test.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
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
package httpref
import (
"regexp"
"testing"
"github.com/charmbracelet/lipgloss"
"github.com/stretchr/testify/assert"
)
func TestReferences_ByName(t *testing.T) {
Statuses = append(Statuses, Reference{Name: "501-extended"})
tests := []struct {
name string
want int
}{
{name: "1", want: 5},
{name: "40", want: 10},
{name: "501", want: 1},
{name: "501*", want: 2},
{name: "599", want: 1},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := Statuses.ByName(tt.name); len(got) != tt.want {
t.Errorf("References.ByName() = %v, want %v", len(got), tt.want)
}
})
}
}
func TestReferences_InRange(t *testing.T) {
tests := []struct {
having string
expect string
}{
{having: "19150", expect: "10000-20000"},
{having: "16406", expect: "10000-20000"},
{having: "5988", expect: "5988-5989"},
{having: "5989", expect: "5988-5989"},
}
for _, tt := range tests {
t.Run(tt.having, func(t *testing.T) {
got := RegisteredPorts.InRange(tt.having)
if len(got) != 1 {
t.Errorf("References.InRange() = %v, want %v", len(got), 1)
}
if got[0].Name != tt.expect {
t.Errorf("References.InRange()[0] = %v, want %v", got[0].Name, tt.expect)
}
})
}
t.Run("Invalid port should not return anything", func(t *testing.T) {
got := RegisteredPorts.InRange("70000") // Invalid port, should not return anything
if len(got) != 0 {
t.Errorf("References.InRange() = %v, want %v", len(got), 0)
}
})
t.Run("Malformed range should not return anything", func(t *testing.T) {
// This tests the scenario where the reference has a malformed range format
RegisteredPorts = append(RegisteredPorts, Reference{Name: "invalid-range"})
got := RegisteredPorts.InRange("1000")
if len(got) != 0 {
t.Errorf("References.InRange() with malformed range = %v, want %v", len(got), 0)
}
})
}
func TestReference_SummarizeContainsFormattedTitle(t *testing.T) {
reference := Reference{Name: "example", Summary: "example summary"}
// Use the rootStyle to ensure the style is applied consistently
rootStyle := lipgloss.NewStyle()
actual := reference.Summarize(rootStyle)
// Update the expected value to match the new styled format
expected := lipgloss.JoinVertical(
lipgloss.Bottom,
nameStyle.Render(reference.Name),
summaryStyle.Render(reference.Summary),
renderStatusBar(reference.Name, reference.Summary),
)
assert.Equal(t, expected, actual)
}
func TestReference_SummarizeContainsCorrectSummary(t *testing.T) {
r := Reference{
Name: "name",
Summary: "summary",
Description: "description",
}
s := r.Summarize(lipgloss.NewStyle().Width(100))
assert.Contains(t, s, "summary")
}
func TestReference_DescribeLooksUpExpectedData(t *testing.T) {
r := Headers.ByName("Headers")[0]
description, err := r.Describe(lipgloss.NewStyle().Width(100))
assert.NoError(t, err)
assert.Contains(t, description, "HTTP")
assert.Contains(t, description, "apply")
}
func TestReferences_Titles(t *testing.T) {
n := Statuses.Titles()
assert.Equal(t, 5, len(n))
}
func TestPortsConsistencyValidation(t *testing.T) {
ports := append(WellKnownPorts[1:], RegisteredPorts[1:]...)
var validRange = regexp.MustCompile(`^\d+(-\d+)?$`)
for _, port := range ports {
if port.Name == "invalid-range" {
continue // Skip known invalid range for test purposes
}
if !validRange.MatchString(port.Name) {
assert.Fail(t, "Invalid port format", "Port name '%s' does not match valid range format", port.Name)
}
}
}
func TestReferences_Search(t *testing.T) {
n := Statuses.Search("auth")
assert.Equal(t, 8, len(n))
}
func TestReferencesHtml_ByName(t *testing.T) {
tests := []struct {
name string
want int
}{
{name: "<a>", want: 1},
{name: "<div>", want: 1},
{name: "<img>", want: 1},
{name: "<script>", want: 1},
{name: "<*", want: 134},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := Html.ByName(tt.name); len(got) != tt.want {
t.Errorf("References.ByName() = %v, want %v", len(got), tt.want)
}
})
}
}
func TestReferencesHtml_Titles(t *testing.T) {
n := Html.Titles()
assert.Equal(t, 1, len(n))
}