| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
package confighttp |
| 5 |
|
| 6 |
import ( |
| 7 |
"net" |
| 8 |
"net/http" |
| 9 |
"testing" |
| 10 |
|
| 11 |
"github.com/stretchr/testify/assert" |
| 12 |
) |
| 13 |
|
| 14 |
var _ http.Handler = (*clientInfoHandler)(nil) |
| 15 |
|
| 16 |
func TestParseIP(t *testing.T) { |
| 17 |
testCases := []struct { |
| 18 |
name string |
| 19 |
input string |
| 20 |
expected *net.IPAddr |
| 21 |
}{ |
| 22 |
{ |
| 23 |
name: "addr", |
| 24 |
input: "1.2.3.4", |
| 25 |
expected: &net.IPAddr{ |
| 26 |
IP: net.IPv4(1, 2, 3, 4), |
| 27 |
}, |
| 28 |
}, |
| 29 |
{ |
| 30 |
name: "addr:port", |
| 31 |
input: "1.2.3.4:33455", |
| 32 |
expected: &net.IPAddr{ |
| 33 |
IP: net.IPv4(1, 2, 3, 4), |
| 34 |
}, |
| 35 |
}, |
| 36 |
{ |
| 37 |
name: "protocol://addr:port", |
| 38 |
input: "http://1.2.3.4:33455", |
| 39 |
expected: nil, |
| 40 |
}, |
| 41 |
{ |
| 42 |
name: "addr/path", |
| 43 |
input: "1.2.3.4/orders", |
| 44 |
expected: nil, |
| 45 |
}, |
| 46 |
} |
| 47 |
for _, tt := range testCases { |
| 48 |
t.Run(tt.name, func(t *testing.T) { |
| 49 |
assert.Equal(t, tt.expected, parseIP(tt.input)) |
| 50 |
}) |
| 51 |
} |
| 52 |
} |