-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathurl_test.go
86 lines (75 loc) · 2.07 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package main
import (
"errors"
"net/url"
"testing"
"time"
"github.com/google/go-cmp/cmp"
)
func mustURLParse(s string) *url.URL {
u, err := url.Parse(s)
if err != nil {
panic(err)
}
return u
}
func TestURLParse(t *testing.T) {
u := mustURLParse("http://test/2020-06-02T14:00+08:00/Singapore,Malaysia")
got, err := ParseRequest(u)
if err != nil {
t.Errorf("got error %v", err)
return
}
want := Request{
time.Date(2020, time.June, 2, 14, 0, 0, 0, time.FixedZone("UTC +8", 8*60*60)),
[]string{"Singapore", "Malaysia"},
}
if !cmp.Equal(got, want) {
t.Errorf("%v", cmp.Diff(got, want))
}
u = mustURLParse("http://test/2019-04-30T18:00:00Z/Nowhere")
got, err = ParseRequest(u)
if err != nil {
t.Errorf("got error %v", err)
return
}
want = Request{
time.Date(2019, time.April, 30, 18, 0, 0, 0, time.FixedZone("UTC", 0)),
[]string{"Nowhere"},
}
if !cmp.Equal(got, want) {
t.Errorf("%v", cmp.Diff(got, want))
}
}
func TestURLParseFail(t *testing.T) {
u := mustURLParse("http://test/2002-08-30T14:00+06:00/")
_, err := ParseRequest(u)
if !errors.Is(err, ErrComponentsMismatch) {
t.Errorf("got error %v, want error %v", err, ErrComponentsMismatch)
}
u = mustURLParse("http://test/")
_, err = ParseRequest(u)
if !errors.Is(err, ErrComponentsMismatch) {
t.Errorf("got error %v, want error %v", err, ErrComponentsMismatch)
}
u = mustURLParse("http://test")
_, err = ParseRequest(u)
if !errors.Is(err, ErrComponentsMismatch) {
t.Errorf("got error %v, want error %v", err, ErrComponentsMismatch)
}
u = mustURLParse("http://test/hi/hi/hi")
_, err = ParseRequest(u)
if !errors.Is(err, ErrComponentsMismatch) {
t.Errorf("got error %v, want error %v", err, ErrComponentsMismatch)
}
u = mustURLParse("http://test/2000-01-13T00:00Z08:00/hi")
_, err = ParseRequest(u)
if !errors.Is(err, ErrInvalidTime) {
t.Errorf("got error %v, want error %v", err, ErrInvalidTime)
}
u = mustURLParse("http://test/2000-01-13 00:00+08:00/hi")
_, err = ParseRequest(u)
if !errors.Is(err, ErrInvalidTime) {
t.Errorf("got error %v, want error %v", err, ErrInvalidTime)
}
}