forked from jlelse/GoBlog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
indexnow_test.go
58 lines (48 loc) · 1.27 KB
/
indexnow_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
package main
import (
"context"
"io"
"net/http"
"testing"
"time"
"github.com/stretchr/testify/require"
)
func Test_indexNow(t *testing.T) {
fc := newFakeHttpClient()
fc.setFakeResponse(http.StatusOK, "OK")
app := &goBlog{
cfg: createDefaultTestConfig(t),
httpClient: fc.Client,
}
app.cfg.IndexNow = &configIndexNow{Enabled: true}
_ = app.initConfig(false)
_ = app.initCache()
app.initIndexNow()
// Create http router
app.d = app.buildRouter()
// Check key
require.NotEmpty(t, app.inKey)
req, _ := http.NewRequestWithContext(context.Background(), "GET", "http://localhost:8080/"+string(app.inKey)+".txt", nil)
res, err := doHandlerRequest(req, app.d)
require.NoError(t, err)
require.Equal(t, 200, res.StatusCode)
body, _ := io.ReadAll(res.Body)
_ = res.Body.Close()
require.Equal(t, app.inKey, body)
// Test publish post
_ = app.createPost(&post{
Section: "posts",
Path: "/testpost",
Published: "2022-01-01",
})
// Wait for hooks to run
fc.mu.Lock()
for fc.req == nil {
fc.mu.Unlock()
time.Sleep(10 * time.Millisecond)
fc.mu.Lock()
}
fc.mu.Unlock()
// Check fake http client
require.Equal(t, "https://api.indexnow.org/indexnow?key="+string(app.inKey)+"&url=http%3A%2F%2Flocalhost%3A8080%2Ftestpost", fc.req.URL.String())
}