forked from PuerkitoBio/gocrawl
-
Notifications
You must be signed in to change notification settings - Fork 1
/
fileext_test.go
55 lines (46 loc) · 1.21 KB
/
fileext_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
package gocrawl
import (
"net/http"
"os"
"path"
"strings"
)
const (
FileFetcherBasePath = "./testdata/"
)
// The file fetcher, that loads URLs from files in the testdata/ directory.
type fileFetcherExtender struct {
*DefaultExtender
}
// FileFetcher constructor, creates the internal default implementation
func newFileFetcher() *fileFetcherExtender {
return &fileFetcherExtender{new(DefaultExtender)}
}
// FileFetcher's Fetch() implementation
func (x *fileFetcherExtender) Fetch(ctx *URLContext, userAgent string, headRequest bool) (*http.Response, error) {
var res = new(http.Response)
var req *http.Request
var e error
if req, e = http.NewRequest("GET", ctx.url.String(), nil); e != nil {
panic(e)
}
// Prepare the pseudo-request
req.Header.Add("User-Agent", userAgent)
// Open the file specified as path in u, relative to testdata/[host]/
host := ctx.url.Host
if strings.HasPrefix(host, "www.") {
host = host[4:]
}
f, e := os.Open(path.Join(FileFetcherBasePath, host, ctx.url.Path))
if e != nil {
// Treat errors as 404s - file not found
res.Status = "404 Not Found"
res.StatusCode = 404
} else {
res.Status = "200 OK"
res.StatusCode = 200
res.Body = f
}
res.Request = req
return res, e
}