-
Notifications
You must be signed in to change notification settings - Fork 0
/
warcraft_test.go
96 lines (79 loc) · 1.93 KB
/
warcraft_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
92
93
94
95
96
// Copyright 2021 Wayback Archiver. All rights reserved.
// Use of this source code is governed by the MIT
// license that can be found in the LICENSE file.
package warcraft // import "github.com/wabarc/warcraft"
import (
"context"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"os"
"testing"
"github.com/wabarc/helper"
)
func TestDownload(t *testing.T) {
if _, err := findWgetExecPath(); err != nil {
t.Skip(err.Error(), ", skipped")
}
_, mux, server := helper.MockServer()
defer server.Close()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello, Golang.")
})
uri := server.URL
in, err := url.Parse(uri)
if err != nil {
t.Fatal(err)
}
dir, err := ioutil.TempDir("", "warcraft")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(dir)
warc := New()
path, err := warc.Download(context.TODO(), in)
if err != nil {
t.Fatal(err)
}
defer os.Remove(path)
if !helper.Exists(path) {
t.Log(path)
t.Errorf(`download warc file failed`)
}
}
func TestUserAgent(t *testing.T) {
if _, err := findWgetExecPath(); err != nil {
t.Skip(err.Error(), ", skipped")
}
_, mux, server := helper.MockServer()
defer server.Close()
userAgent := "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:47.0) Gecko/20100101 Firefox/47.0"
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
ua := r.Header.Get("User-Agent")
if ua != userAgent {
t.Fatalf("Unexpected create warc with User-Agent, got %s instead of %s", ua, userAgent)
}
fmt.Fprintf(w, "Hello, Golang.")
})
uri := server.URL
in, err := url.Parse(uri)
if err != nil {
t.Fatal(err)
}
dir, err := ioutil.TempDir("", "warcraft")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(dir)
warc := &Warcraft{UserAgent: userAgent}
path, err := warc.Download(context.TODO(), in)
if err != nil {
t.Fatal(err)
}
defer os.Remove(path)
if !helper.Exists(path) {
t.Log(path)
t.Errorf(`download warc file failed`)
}
}