-
Notifications
You must be signed in to change notification settings - Fork 0
/
server_test.go
92 lines (71 loc) · 2.45 KB
/
server_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
80
81
82
83
84
85
86
87
88
89
90
91
92
// +build unit
package main
import (
"bytes"
"net/http"
"net/http/httptest"
"net/url"
"strings"
"testing"
)
const errorErrorDetailsRequiredInBodyResponse = "Error response on subscription should return the errors in the response body"
func TestSubscribeSuccess(t *testing.T) {
handler := mainHandler()
data := url.Values{}
data.Set("hub.mode", "subscribe")
data.Set("hub.callback", "http://localhost:7000")
data.Set("hub.topic", "http://localhost:8000")
data.Set("hub.lease_seconds", "3600") // in seconds, 1-hour
data.Set("hub.secret", "secret-string-1")
req, _ := http.NewRequest("POST", "", bytes.NewBufferString(data.Encode()))
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
w := httptest.NewRecorder()
handler.ServeHTTP(w, req)
if w.Code != http.StatusAccepted {
t.Errorf("Subscribe request did not return %v", http.StatusAccepted)
}
}
func TestSubscribeFailure(t *testing.T) {
handler := mainHandler()
data := url.Values{}
data.Set("hub.mode", "subscribe")
// ommitting required fields here to trigger a failure
req, _ := http.NewRequest("POST", "", bytes.NewBufferString(data.Encode()))
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
w := httptest.NewRecorder()
handler.ServeHTTP(w, req)
if w.Code != http.StatusBadRequest {
t.Errorf("Incomplete subscribe request did not return %v", http.StatusBadRequest)
}
// testing for the existence of just one error string would do
if !strings.Contains(w.Body.String(), errorRequiredFieldMissingHubCallback) {
t.Errorf(errorErrorDetailsRequiredInBodyResponse)
}
}
func TestUnsubscribe(t *testing.T) {
handler := mainHandler()
data := url.Values{}
data.Set("hub.mode", "unsubscribe")
req, _ := http.NewRequest("POST", "", bytes.NewBufferString(data.Encode()))
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
w := httptest.NewRecorder()
handler.ServeHTTP(w, req)
if w.Code != http.StatusOK {
t.Errorf("Unsubscribe request did not return %v", http.StatusOK)
}
}
func TestPublish(t *testing.T) {
handler := mainHandler()
data := url.Values{}
data.Set("hub.mode", "publish")
req, _ := http.NewRequest("POST", "", bytes.NewBufferString(data.Encode()))
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
w := httptest.NewRecorder()
handler.ServeHTTP(w, req)
if w.Code != http.StatusOK {
t.Errorf("Publish request did not return %v", http.StatusOK)
}
}
func logi(s string) {
log.Info(">> Test Suite:", s)
}