Skip to content

Commit 62977bf

Browse files
authored
Merge pull request #17 from jan-carreras/#16-in-range-returns-incorrect-registered-port
#16 in range returns incorrect registered port
2 parents 45373e1 + 0154f7d commit 62977bf

File tree

4 files changed

+82
-61
lines changed

4 files changed

+82
-61
lines changed

httpref.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,9 @@ func (r References) ByName(code string) References {
6464
// InRange attempts to find a numeric in a range in the Name field
6565
func (r References) InRange(code string) References {
6666
for _, v := range r {
67-
if v.Name == code {
68-
return References{v}
69-
}
70-
7167
if strings.Contains(v.Name, "-") {
7268
parts := strings.Split(v.Name, "-")
73-
if code >= parts[0] || code <= parts[len(parts)-1] {
69+
if code >= parts[0] && code <= parts[len(parts)-1] {
7470
return References{v}
7571
}
7672
}

httpref_test.go

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package httpref
22

33
import (
4+
"regexp"
45
"strings"
56
"testing"
67

@@ -29,20 +30,34 @@ func TestReferences_ByName(t *testing.T) {
2930
}
3031

3132
func TestReferences_InRange(t *testing.T) {
32-
tests := []string{
33-
"19150",
34-
"16406",
35-
"5988",
36-
"5989",
33+
tests := []struct {
34+
having string
35+
expect string
36+
}{
37+
{having: "19150", expect: "10000-20000"},
38+
{having: "16406", expect: "10000-20000"},
39+
{having: "5988", expect: "5988-5989"},
40+
{having: "5989", expect: "5988-5989"},
3741
}
3842

3943
for _, tt := range tests {
40-
t.Run(tt, func(t *testing.T) {
41-
if got := RegisteredPorts.InRange(tt); len(got) != 1 {
44+
t.Run(tt.having, func(t *testing.T) {
45+
got := RegisteredPorts.InRange(tt.having)
46+
if len(got) != 1 {
4247
t.Errorf("References.InRange() = %v, want %v", len(got), 1)
4348
}
49+
if got[0].Name != tt.expect {
50+
t.Errorf("References.InRange()[0] = %v, want %v", got[0].Name, tt.expect)
51+
}
4452
})
4553
}
54+
55+
t.Run("Invalid port should not return anything", func(t *testing.T) {
56+
got := RegisteredPorts.InRange("70000") // Invalid port, should not return anything
57+
if len(got) != 0 {
58+
t.Errorf("References.InRange() = %v, want %v", len(got), 0)
59+
}
60+
})
4661
}
4762

4863
func TestReference_SummarizeContainsCorrectParts(t *testing.T) {
@@ -86,3 +101,13 @@ func TestReferences_Titles(t *testing.T) {
86101
n := Statuses.Titles()
87102
assert.Equal(t, 5, len(n))
88103
}
104+
105+
func TestPortsConsistencyValidation(t *testing.T) {
106+
ports := append(WellKnownPorts[1:], RegisteredPorts[1:]...)
107+
var validRange = regexp.MustCompile(`^\d+(-\d+)?$`)
108+
for _, port := range ports {
109+
if !validRange.MatchString(port.Name) {
110+
t.Errorf("Invalid port format: %v", port.Name)
111+
}
112+
}
113+
}

0 commit comments

Comments
 (0)