-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
79 lines (72 loc) · 1.86 KB
/
main_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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package main
import (
"net/http"
"net/http/httptest"
"net/url"
"os"
"sync"
"testing"
"time"
"github.com/benitogf/katamari/messages"
"github.com/gorilla/websocket"
"github.com/benitogf/auth"
"github.com/benitogf/katamari"
"github.com/benitogf/tie/router"
"github.com/gorilla/mux"
"github.com/stretchr/testify/require"
)
func TestBlog(t *testing.T) {
// var c auth.Credentials
var wg sync.WaitGroup
var wsEvent messages.Message
var mutex sync.Mutex
authStore := &katamari.MemoryStorage{}
err := authStore.Start(katamari.StorageOpt{})
require.NoError(t, err)
go katamari.WatchStorageNoop(authStore)
auth := auth.New(
auth.NewJwtStore("a-secret-key", time.Second*1),
authStore,
)
server := &katamari.Server{}
server.Silence = true
server.Audit = func(r *http.Request) bool {
return router.Audit(r, auth)
}
server.Router = mux.NewRouter()
router.Routes(server)
auth.Router(server)
server.Start("localhost:9978")
defer server.Close(os.Interrupt)
req, err := http.NewRequest("GET", "/posts/*", nil)
require.NoError(t, err)
w := httptest.NewRecorder()
server.Router.ServeHTTP(w, req)
response := w.Result()
require.Equal(t, http.StatusUnauthorized, response.StatusCode)
req, err = http.NewRequest("DELETE", "/posts/*", nil)
require.NoError(t, err)
w = httptest.NewRecorder()
server.Router.ServeHTTP(w, req)
response = w.Result()
require.Equal(t, http.StatusUnauthorized, response.StatusCode)
wsURL := url.URL{Scheme: "ws", Host: "localhost:9978", Path: "/blog"}
wsClient, _, err := websocket.DefaultDialer.Dial(wsURL.String(), nil)
require.NoError(t, err)
wg.Add(1)
go func() {
for {
_, message, err := wsClient.ReadMessage()
if err != nil {
break
}
mutex.Lock()
wsEvent, err = messages.DecodeTest(message)
require.NoError(t, err)
mutex.Unlock()
wg.Done()
}
}()
wg.Wait()
require.Equal(t, "[]", wsEvent.Data)
}