-
Notifications
You must be signed in to change notification settings - Fork 2
/
response_test.go
91 lines (84 loc) · 3.47 KB
/
response_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
package crawler
import (
"net/url"
"os"
"testing"
"github.com/stretchr/testify/require"
)
func TestReadResponse(t *testing.T) {
var u = func(uri string) *url.URL {
u, err := url.Parse(uri)
if err != nil {
t.Fatal(err)
}
return u
}
tests := []struct {
base *url.URL
file string
expects Response
}{
{
base: u("https://base.test/path/to/request"),
file: "testdata/index.html",
expects: Response{
URL: "https://base.test/path/to/request",
Links: []Link{
{URL: "http://example.localhost/absolute/url"},
{URL: "https://base.test/absolute/path"},
{URL: "https://base.test/path/relative/path"},
{URL: "https://base.test/path/to/request"},
{URL: "https://base.test/path/to/link-with-anchor/test"},
},
Assets: []Asset{
{Tag: "script", URL: "https://base.test/path/to/example/javascript.js", Type: "text/javascript"},
{Tag: "link", URL: "http://ok.test/style.css", Rel: "stylesheet", Type: "text/css"},
{Tag: "img", URL: "https://base.test/path/to/logo.svg"},
{Tag: "script", URL: "https://base.test/path/to/in-body.js", Type: "text/javascript"},
},
},
},
{
base: u("https://example"),
file: "testdata/assets.html",
expects: Response{
URL: "https://example",
Assets: []Asset{
{Tag: "script", URL: "https://example/example/javascript.js", Type: "text/javascript"},
{Tag: "link", URL: "http://ok.test/style.css", Rel: "stylesheet", Type: "text/css"},
{Tag: "img", URL: "https://example/logo.svg"},
{Tag: "script", URL: "https://example/in-body.js", Type: "text/javascript"},
{Tag: "picture>source", URL: "https://example/images/kitten-stretching.png"},
{Tag: "picture>source", URL: "https://example/images/[email protected]"},
{Tag: "picture>source", URL: "https://example/images/[email protected]"},
{Tag: "picture>source", URL: "https://example/images/kitten-sitting.png"},
{Tag: "picture>source", URL: "https://example/images/[email protected]"},
{Tag: "picture>img", URL: "https://example/images/[email protected]"},
{Tag: "picture>img", URL: "https://example/images/[email protected]"},
{Tag: "picture>source", URL: "https://example/images/kitten-stretching.png"},
{Tag: "picture>source", URL: "https://example/images/kitten-sitting.png"},
{Tag: "picture>img", URL: "https://example/images/kitten-curled.png"},
{Tag: "picture>source", URL: "https://example/images/butterfly.webp", Type: "image/webp"},
{Tag: "picture>img", URL: "https://example/images/butterfly.jpg"},
{Tag: "video", URL: "https://example/video.webm"},
{Tag: "video>source", URL: "https://example/devstories.webm", Type: `video/webm;codecs="vp8, vorbis"`},
{Tag: "video>source", URL: "https://example/devstories.mp4", Type: `video/mp4;codecs="avc1.42E01E, mp4a.40.2"`},
{Tag: "video>source", URL: "https://example/devstories.webm#t=10,20", Type: `video/webm;codecs="vp8, vorbis"`},
{Tag: "video>source", URL: "https://example/devstories.webm", Type: `video/webm;codecs="vp8, vorbis"`},
{Tag: "video>source", URL: "https://example/devstories.mp4", Type: `video/mp4;codecs="avc1.42E01E, mp4a.40.2"`},
},
},
},
}
for _, test := range tests {
t.Run(test.file, func(t *testing.T) {
f, err := os.Open(test.file)
require.NoError(t, err)
defer f.Close()
var actual Response
err = ReadResponse(test.base, f, &actual)
require.NoError(t, err)
require.Equal(t, test.expects, actual)
})
}
}