GoBlog/indexnow_test.go

59 lines
1.3 KiB
Go
Raw Normal View History

2022-01-24 08:43:06 +00:00
package main
import (
"context"
2022-01-24 08:43:06 +00:00
"io"
"net/http"
"testing"
"time"
"github.com/stretchr/testify/require"
)
func Test_indexNow(t *testing.T) {
fc := newFakeHttpClient()
fc.setFakeResponse(http.StatusOK, "OK")
2022-01-24 08:43:06 +00:00
app := &goBlog{
cfg: createDefaultTestConfig(t),
httpClient: fc.Client,
}
app.cfg.IndexNow = &configIndexNow{Enabled: true}
_ = app.initConfig(false)
_ = app.initCache()
app.initIndexNow()
2022-01-24 08:43:06 +00:00
// Create http router
app.d = app.buildRouter()
2022-01-24 08:43:06 +00:00
// Check key
require.NotEmpty(t, app.inKey)
2022-02-25 15:29:42 +00:00
req, _ := http.NewRequestWithContext(context.Background(), "GET", "http://localhost:8080/"+string(app.inKey)+".txt", nil)
2022-01-24 08:43:06 +00:00
res, err := doHandlerRequest(req, app.d)
require.NoError(t, err)
require.Equal(t, 200, res.StatusCode)
body, _ := io.ReadAll(res.Body)
_ = res.Body.Close()
2022-02-25 15:29:42 +00:00
require.Equal(t, app.inKey, body)
2022-01-24 08:43:06 +00:00
// Test publish post
_ = app.createPost(&post{
Section: "posts",
Path: "/testpost",
Published: "2022-01-01",
})
// Wait for hooks to run
2022-02-12 11:37:13 +00:00
fc.mu.Lock()
for fc.req == nil {
fc.mu.Unlock()
time.Sleep(10 * time.Millisecond)
fc.mu.Lock()
}
fc.mu.Unlock()
2022-01-24 08:43:06 +00:00
// Check fake http client
2022-02-25 15:29:42 +00:00
require.Equal(t, "https://api.indexnow.org/indexnow?key="+string(app.inKey)+"&url=http%3A%2F%2Flocalhost%3A8080%2Ftestpost", fc.req.URL.String())
2022-01-24 08:43:06 +00:00
}