-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfeedReader_test.go
More file actions
134 lines (127 loc) · 3.15 KB
/
feedReader_test.go
File metadata and controls
134 lines (127 loc) · 3.15 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package main
import (
"bytes"
"html/template"
"io"
"net/http"
"net/http/httptest"
"os"
"strconv"
"testing"
"time"
)
const (
directoryTemplate = `<!DOCTYPE HTML><html><body><h1>Index of {{.Path}}</h1><table><tr><th>Name</th><th>Last modified</th><th>Size</th><th>Description</th></tr><tr><th colspan="4"><hr></th></tr><tr><td><a href="{{.Path}}">Parent Directory</a></td><td> </td><td align="right"> - </td><td> </td></tr>{{range .Files}}<tr><td><a href="{{.Name}}">{{.Name}}</a></td><td align="right">{{.Date}} </td><td align="right">{{.Size}}</td><td> </td></tr><tr>{{end}}</table></body></html>`
entryTimeFormat = `02-Jan-2006 15:04`
testZipFilePath = "./test.zip"
)
type File struct {
Name, Date, Size string
}
func buildFileList(fileCount int) (fl []File) {
for i := 1; i < fileCount; i++ {
var file File
file.Name = "File" + strconv.Itoa(i) + ".zip"
file.Date = time.Now().Format(entryTimeFormat)
file.Size = "1.0M"
fl = append(fl, file)
}
var file = File{
"file.doc",
"",
"",
}
fl = append(fl, file)
return
}
//download directory
func TestGetDirectory(t *testing.T) {
fl := buildFileList(5)
template, err := template.New("listing").Parse(directoryTemplate)
if err != nil {
t.Fatal(err)
}
templateData := struct {
Path string
Files []File
}{
"",
fl,
}
//serve directory listing with .zip files
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
template.Execute(w, templateData)
}))
defer server.Close()
server.URL += "/"
templateData.Path = server.URL
//call getZipListings on directory
downloadList, err := getZipListings(server.URL)
//downloadList should return a slice of strings for the .zip files
for i, v := range downloadList {
if v != server.URL+fl[i].Name {
t.Errorf("incorrect file name: %s", v)
}
}
}
func TestZipFunctions(t *testing.T) {
//create a zip file to serve
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, testZipFilePath)
}))
defer server.Close()
zipFile, err := getZip(server.URL)
if err != nil {
t.Fatal(err)
}
defer zipFile.Close()
defer os.Remove(zipFile.Name())
if zipFile == nil {
t.Fatal("did not open zip file")
}
zipReader, err := openZip(zipFile)
if err != nil {
t.Fatal(err)
}
defer zipReader.Close()
if zipReader == nil {
t.Fatal("did not open zip reader")
}
rclient, err := setupRedis()
if err != nil {
t.Fatal(err)
}
for _, f := range zipReader.File {
index, err := pushXML(f, rclient)
if err != nil {
t.Fatal(err)
}
if index == 0 {
t.Fatal("clear database to run tests correctly")
}
rc, err := f.Open()
if err != nil {
t.Fatal(err)
}
defer rc.Close()
buff := bytes.NewBuffer(nil)
_, err = io.Copy(buff, rc)
if err != nil {
t.Fatal(err)
}
rresult, err := rclient.LRange(listName, index-1, index-1).Result()
if rresult[0] != buff.String() {
t.Fatal("stored data not equal")
}
}
t.Run("Test duplicate record", func(t *testing.T) {
f := zipReader.File[0]
index, err := pushXML(f, rclient)
if err != nil {
t.Fatal(err)
}
if index != 0 {
t.Fatal("record was unique")
}
})
}