Skip to content

Commit 39e6074

Browse files
committed
Test should disallow on FromStatus(500) like TestAgent correctly was
fixes #40 co-authored-by: https://github.com/masonlouchart
1 parent 1fe1f4f commit 39e6074

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

robotstxt.go

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,21 @@ import (
1818
)
1919

2020
type RobotsData struct {
21+
Host string
22+
Sitemaps []string
23+
2124
// private
22-
groups map[string]*Group
2325
allowAll bool
2426
disallowAll bool
25-
Host string
26-
Sitemaps []string
27+
groups map[string]*Group
2728
}
2829

2930
type Group struct {
3031
rules []*rule
3132
Agent string
3233
CrawlDelay time.Duration
34+
35+
disallowAll bool
3336
}
3437

3538
type rule struct {
@@ -59,6 +62,7 @@ func (e ParseError) Error() string {
5962
var allowAll = &RobotsData{allowAll: true}
6063
var disallowAll = &RobotsData{disallowAll: true}
6164
var emptyGroup = &Group{}
65+
var emptyDisallowGroup = &Group{disallowAll: true}
6266

6367
func FromStatusAndBytes(statusCode int, body []byte) (*RobotsData, error) {
6468
switch {
@@ -173,12 +177,19 @@ func (r *RobotsData) FindGroup(agent string) (ret *Group) {
173177
}
174178

175179
if ret == nil {
176-
return emptyGroup
180+
if r.disallowAll {
181+
return emptyDisallowGroup
182+
} else {
183+
return emptyGroup
184+
}
177185
}
178186
return
179187
}
180188

181189
func (g *Group) Test(path string) bool {
190+
if g.disallowAll {
191+
return false
192+
}
182193
if r := g.findRule(path); r != nil {
183194
return r.allow
184195
}

robotstxt_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,16 @@ disallow: /c`
230230
expectAccess(t, r, false, "/c", "c")
231231
}
232232

233+
func TestDifferentApiDisallowAll(t *testing.T) {
234+
r, err := FromStatusAndBytes(500, nil)
235+
require.NoError(t, err)
236+
237+
a := r.TestAgent("/", "*")
238+
b := r.FindGroup("*").Test("/")
239+
assert.Equal(t, false, a, "Expected false (disallow) from TestAgent(/, *)")
240+
assert.Equal(t, false, b, "Expected false (disallow) from FindGroup(*).Test(/)")
241+
}
242+
233243
func BenchmarkParseFromString001(b *testing.B) {
234244
input := robotsText001
235245
b.ReportAllocs()

0 commit comments

Comments
 (0)