forked from jlelse/GoBlog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
editor_test.go
46 lines (35 loc) · 1.06 KB
/
editor_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
package main
import (
"context"
"net/http"
"testing"
"github.com/gorilla/websocket"
"github.com/posener/wstest"
"github.com/stretchr/testify/require"
)
func Test_editorPreview(t *testing.T) {
app := &goBlog{
cfg: createDefaultTestConfig(t),
}
_ = app.initConfig(false)
app.initMarkdown()
_ = app.initTemplateStrings()
h := http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
app.serveEditorPreview(rw, r.WithContext(context.WithValue(r.Context(), blogKey, "default")))
})
d := wstest.NewDialer(h)
c, resp, err := d.Dial("ws://example.com/editor/preview", nil)
require.NoError(t, err)
defer resp.Body.Close()
require.NotNil(t, c)
err = c.WriteMessage(websocket.TextMessage, []byte("---\ntitle: Title\nsection: posts\n---\nContent."))
require.NoError(t, err)
mt, msg, err := c.ReadMessage()
require.NoError(t, err)
require.Equal(t, websocket.TextMessage, mt)
msgStr := string(msg)
require.Contains(t, msgStr, "<h1")
require.Contains(t, msgStr, ">Title")
require.Contains(t, msgStr, "<p>Content")
require.Contains(t, msgStr, "Posts")
}