2021-06-11 15:27:42 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
2022-02-12 22:29:45 +01:00
|
|
|
"net/http/httptest"
|
2021-06-11 15:27:42 +02:00
|
|
|
"testing"
|
2021-06-15 17:36:41 +02:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2021-06-11 15:27:42 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func Test_robotsTXT(t *testing.T) {
|
|
|
|
|
2023-08-09 14:21:39 +02:00
|
|
|
t.Run("Default", func(t *testing.T) {
|
|
|
|
app := &goBlog{
|
|
|
|
cfg: &config{
|
|
|
|
Server: &configServer{
|
|
|
|
PublicAddress: "https://example.com",
|
|
|
|
},
|
2021-06-11 15:27:42 +02:00
|
|
|
},
|
2023-08-09 14:21:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
rec := httptest.NewRecorder()
|
|
|
|
req := httptest.NewRequest("GET", "/robots.txt", nil)
|
|
|
|
app.serveRobotsTXT(rec, req)
|
|
|
|
assert.Equal(t, http.StatusOK, rec.Code)
|
|
|
|
assert.Equal(t, "User-agent: *\nAllow: /\n\nSitemap: https://example.com/sitemap.xml\n", rec.Body.String())
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Private mode", func(t *testing.T) {
|
|
|
|
app := &goBlog{
|
|
|
|
cfg: &config{
|
|
|
|
Server: &configServer{
|
|
|
|
PublicAddress: "https://example.com",
|
|
|
|
},
|
|
|
|
PrivateMode: &configPrivateMode{
|
|
|
|
Enabled: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.True(t, app.isPrivate())
|
|
|
|
|
|
|
|
rec := httptest.NewRecorder()
|
|
|
|
req := httptest.NewRequest("GET", "/robots.txt", nil)
|
|
|
|
app.serveRobotsTXT(rec, req)
|
|
|
|
assert.Equal(t, http.StatusOK, rec.Code)
|
|
|
|
assert.Equal(t, "User-agent: *\nDisallow: /\n", rec.Body.String())
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Blocked bot", func(t *testing.T) {
|
|
|
|
app := &goBlog{
|
|
|
|
cfg: &config{
|
|
|
|
Server: &configServer{
|
|
|
|
PublicAddress: "https://example.com",
|
|
|
|
},
|
|
|
|
RobotsTxt: &configRobotsTxt{
|
|
|
|
BlockedBots: []string{
|
|
|
|
"GPTBot",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
rec := httptest.NewRecorder()
|
|
|
|
req := httptest.NewRequest("GET", "/robots.txt", nil)
|
|
|
|
app.serveRobotsTXT(rec, req)
|
|
|
|
assert.Equal(t, http.StatusOK, rec.Code)
|
|
|
|
assert.Equal(t, "User-agent: *\nAllow: /\n\nUser-agent: GPTBot\nDisallow: /\n\nSitemap: https://example.com/sitemap.xml\n", rec.Body.String())
|
|
|
|
})
|
2021-06-11 15:27:42 +02:00
|
|
|
|
|
|
|
}
|