forked from Team254/cheesy-arena
-
Notifications
You must be signed in to change notification settings - Fork 0
/
web_test.go
76 lines (66 loc) · 2.16 KB
/
web_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
// Copyright 2014 Team 254. All Rights Reserved.
// Author: [email protected] (Patrick Fairbank)
package main
import (
"github.com/stretchr/testify/assert"
"net/http"
"net/http/httptest"
"strings"
"testing"
)
func TestIndex(t *testing.T) {
clearDb()
defer clearDb()
var err error
db, err = OpenDatabase(testDbPath)
assert.Nil(t, err)
defer db.Close()
eventSettings, _ = db.GetEventSettings()
recorder := getHttpResponse("/")
assert.Equal(t, 200, recorder.Code)
assert.Contains(t, recorder.Body.String(), "Home - Untitled Event - Cheesy Arena")
}
func getHttpResponse(path string) *httptest.ResponseRecorder {
recorder := httptest.NewRecorder()
req, _ := http.NewRequest("GET", path, nil)
newHandler().ServeHTTP(recorder, req)
return recorder
}
func postHttpResponse(path string, body string) *httptest.ResponseRecorder {
recorder := httptest.NewRecorder()
req, _ := http.NewRequest("POST", path, strings.NewReader(body))
req.Header.Set("Content-Type", "application/x-www-form-urlencoded; param=value")
newHandler().ServeHTTP(recorder, req)
return recorder
}
// Starts a real local HTTP server that can be used by more sophisticated tests.
func startTestServer() (*httptest.Server, string) {
server := httptest.NewServer(newHandler())
return server, "ws" + server.URL[len("http"):]
}
// Receives the next websocket message and asserts that it is an error.
func readWebsocketError(t *testing.T, ws *Websocket) string {
messageType, data, err := ws.Read()
if assert.Nil(t, err) && assert.Equal(t, "error", messageType) {
return data.(string)
}
return "error"
}
// Receives the next websocket message and asserts that it is of the given type.
func readWebsocketType(t *testing.T, ws *Websocket, expectedMessageType string) interface{} {
messageType, message, err := ws.Read()
if assert.Nil(t, err) {
assert.Equal(t, expectedMessageType, messageType)
}
return message
}
func readWebsocketMultiple(t *testing.T, ws *Websocket, count int) map[string]interface{} {
messages := make(map[string]interface{})
for i := 0; i < count; i++ {
messageType, message, err := ws.Read()
if assert.Nil(t, err) {
messages[messageType] = message
}
}
return messages
}