mirror of https://github.com/jlelse/GoBlog
Simple blogging system written in Go
https://goblog.app
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
58 lines
1.3 KiB
58 lines
1.3 KiB
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() |
|
_ = app.initDatabase(false) |
|
defer app.db.close() |
|
app.initComponents(false) |
|
|
|
// 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()) |
|
}
|
|
|