-
Notifications
You must be signed in to change notification settings - Fork 0
/
walker_test.go
83 lines (78 loc) · 1.96 KB
/
walker_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
package walker
import (
"fmt"
"net/http/httptest"
"path/filepath"
"runtime"
"testing"
"time"
"github.com/davecgh/go-spew/spew"
"github.com/foomo/walker/config"
"github.com/foomo/walker/htmlschema/example"
"github.com/stretchr/testify/assert"
)
func getExampleDir(path ...string) string {
_, filename, _, _ := runtime.Caller(0)
return filepath.Join(append([]string{filepath.Dir(filename)}, path...)...)
}
func TestSortPathsByLength(t *testing.T) {
paths := []string{"/", "/deeper", "/deeper/and/deeper"}
sortedPaths := sortPathsByLength(paths)
for i, p := range paths {
assert.Equal(t, sortedPaths[len(sortedPaths)-1-i], p)
}
}
func TestWalker(t *testing.T) {
s := example.NewServer(getExampleDir("htmlschema", "example", "htdocs"))
testServer := httptest.NewServer(s)
defer testServer.Close()
w := NewWalker()
conf := &config.Config{
Target: config.Target{
BaseURL: testServer.URL,
Paths: []string{
"/",
},
},
GroupHeader: example.GroupHeader,
IgnoreRobots: true,
Concurrency: 1,
SchemaRoot: getExampleDir("htmlschema", "example", "schema", "groups"),
}
chanStatus, errWalk := w.Walk(conf, nil, nil, nil, nil)
assert.NoError(t, errWalk)
StatusLoop:
for {
select {
case status := <-chanStatus:
type score struct {
score int
penalty int
pages int
}
groupScores := map[string]*score{}
w.Stop()
for _, r := range status.Results {
fmt.Println(r.Code, r.TargetURL)
if r.ValidationReport != nil {
groupScore, okGroupScore := groupScores[r.Group]
if !okGroupScore {
groupScore = &score{}
groupScores[r.Group] = groupScore
}
groupScore.score += r.ValidationReport.Score
groupScore.pages++
for _, v := range r.ValidationReport.Validations {
groupScore.penalty += v.Penalty
}
} else {
fmt.Println("validation error", r.ValidionError)
}
}
spew.Dump(groupScores)
break StatusLoop
case <-time.After(time.Second * 10):
t.Log("10s")
}
}
}