forked from pion/ice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
url_test.go
71 lines (65 loc) · 2.84 KB
/
url_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
package ice
import (
"errors"
"testing"
"github.com/stretchr/testify/assert"
)
func TestParseURL(t *testing.T) {
t.Run("Success", func(t *testing.T) {
testCases := []struct {
rawURL string
expectedURLString string
expectedScheme SchemeType
expectedSecure bool
expectedHost string
expectedPort int
expectedProto ProtoType
}{
{"stun:google.de", "stun:google.de:3478", SchemeTypeSTUN, false, "google.de", 3478, ProtoTypeUDP},
{"stun:google.de:1234", "stun:google.de:1234", SchemeTypeSTUN, false, "google.de", 1234, ProtoTypeUDP},
{"stuns:google.de", "stuns:google.de:5349", SchemeTypeSTUNS, true, "google.de", 5349, ProtoTypeTCP},
{"stun:[::1]:123", "stun:[::1]:123", SchemeTypeSTUN, false, "::1", 123, ProtoTypeUDP},
{"turn:google.de", "turn:google.de:3478?transport=udp", SchemeTypeTURN, false, "google.de", 3478, ProtoTypeUDP},
{"turns:google.de", "turns:google.de:5349?transport=tcp", SchemeTypeTURNS, true, "google.de", 5349, ProtoTypeTCP},
{"turn:google.de?transport=udp", "turn:google.de:3478?transport=udp", SchemeTypeTURN, false, "google.de", 3478, ProtoTypeUDP},
{"turns:google.de?transport=tcp", "turns:google.de:5349?transport=tcp", SchemeTypeTURNS, true, "google.de", 5349, ProtoTypeTCP},
}
for i, testCase := range testCases {
url, err := ParseURL(testCase.rawURL)
assert.Nil(t, err, "testCase: %d %v", i, testCase)
if err != nil {
return
}
assert.Equal(t, testCase.expectedScheme, url.Scheme, "testCase: %d %v", i, testCase)
assert.Equal(t, testCase.expectedURLString, url.String(), "testCase: %d %v", i, testCase)
assert.Equal(t, testCase.expectedSecure, url.IsSecure(), "testCase: %d %v", i, testCase)
assert.Equal(t, testCase.expectedHost, url.Host, "testCase: %d %v", i, testCase)
assert.Equal(t, testCase.expectedPort, url.Port, "testCase: %d %v", i, testCase)
assert.Equal(t, testCase.expectedProto, url.Proto, "testCase: %d %v", i, testCase)
}
})
t.Run("Failure", func(t *testing.T) {
testCases := []struct {
rawURL string
expectedErr error
}{
{"", ErrSchemeType},
{":::", errors.New("parse :::: missing protocol scheme")},
{"stun:[::1]:123:", errors.New("address [::1]:123:: too many colons in address")},
{"stun:[::1]:123a", ErrPort},
{"google.de", ErrSchemeType},
{"stun:", ErrHost},
{"stun:google.de:abc", ErrPort},
{"stun:google.de?transport=udp", ErrSTUNQuery},
{"stuns:google.de?transport=udp", ErrSTUNQuery},
{"turn:google.de?trans=udp", ErrInvalidQuery},
{"turns:google.de?trans=udp", ErrInvalidQuery},
{"turns:google.de?transport=udp&another=1", ErrInvalidQuery},
{"turn:google.de?transport=ip", ErrProtoType},
}
for i, testCase := range testCases {
_, err := ParseURL(testCase.rawURL)
assert.EqualError(t, err, testCase.expectedErr.Error(), "testCase: %d %v", i, testCase)
}
})
}