-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgohook_test.go
More file actions
113 lines (105 loc) · 2.94 KB
/
Copy pathgohook_test.go
File metadata and controls
113 lines (105 loc) · 2.94 KB
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package gohook
import (
"bytes"
"crypto/hmac"
"crypto/sha1"
"encoding/hex"
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
"time"
)
func SendRequest(method, url, secret, eventHeader string, body []byte) (*http.Response, error) {
req, err := http.NewRequest(method, url, bytes.NewBuffer(body))
if err != nil {
return nil, err
}
req.Header.Add("X-Github-Event", eventHeader)
if secret != "" {
mac := hmac.New(sha1.New, []byte(secret))
_, err := mac.Write(body)
if err != nil {
return nil, err
}
macVal := mac.Sum(nil)
sig := "sha1=" + hex.EncodeToString(macVal)
req.Header.Add("X-Hub-Signature", sig)
}
client := http.Client{}
return client.Do(req)
}
func TestCreateEvent(t *testing.T) {
raw, err := ioutil.ReadFile("testdata/sample_create.json")
if err != nil {
t.Errorf("Error reading sample create file: %s", err)
}
hookServer := NewServer(8888, "secret", "/path")
server := httptest.NewServer(hookServer)
defer server.Close()
resp, err := SendRequest("POST", server.URL+"/path", "secret", "create", raw)
if err != nil {
t.Errorf("SendRequest: %s", err)
}
if resp.StatusCode != 200 {
t.Errorf("Bad status code: %s", resp.StatusCode)
}
select {
case et := <-hookServer.EventAndTypes:
if et.Type != CreateEventType {
t.Errorf("Incorrect event type from server: %s", et.Type)
}
_, ok := et.Event.(*CreateEvent)
if !ok {
t.Error("Could not assert event as *CreateEvent.")
}
return
case <-time.After(time.Second):
t.Error("Timeout waiting for event from server.")
}
}
func TestInvalidRequests(t *testing.T) {
correctBody, err := ioutil.ReadFile("testdata/sample_create.json")
if err != nil {
t.Errorf("Error reading sample create file: %s", err)
}
hookServer := NewServer(8888, "secret", "/path")
server := httptest.NewServer(hookServer)
defer server.Close()
// check wrong method
resp, err := SendRequest("GET", server.URL+"/path", "secret", "create", correctBody)
if err != nil {
t.Errorf("SendRequest: %s", err)
}
defer resp.Body.Close()
if resp.StatusCode != 405 {
t.Errorf("Expected 405 Method Not Allowed, got: %s", resp.StatusCode)
}
// check wrong path
resp, err = SendRequest("POST", server.URL+"/wrongpath", "secret", "create", correctBody)
if err != nil {
t.Errorf("SendRequest: %s", err)
}
defer resp.Body.Close()
if resp.StatusCode != 404 {
t.Errorf("Expected 404 Not Found, got: %s", resp.StatusCode)
}
// check wrong secret
resp, err = SendRequest("POST", server.URL+"/path", "wrongsecret", "create", correctBody)
if err != nil {
t.Errorf("SendRequest: %s", err)
}
defer resp.Body.Close()
if resp.StatusCode != 403 {
t.Errorf("Expected 403 Unauthorized, got: %s", resp.StatusCode)
}
// check missing event type
resp, err = SendRequest("POST", server.URL+"/path", "secret", "", correctBody)
if err != nil {
t.Errorf("SendRequest: %s", err)
}
defer resp.Body.Close()
if resp.StatusCode != 400 {
t.Errorf("Expected 400 Bad Request, got: %s", resp.StatusCode)
}
}