-
Notifications
You must be signed in to change notification settings - Fork 195
/
main_test.go
60 lines (49 loc) · 1.27 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
package main
import (
"bytes"
"io/ioutil"
"testing"
"net/http"
"net/http/httptest"
)
func TestHandle(t *testing.T) {
pl, err := ioutil.ReadFile("testdata/payload.json")
if err != nil {
t.Fatalf("could not read payload.json: %v", err)
}
req, err := http.NewRequest(http.MethodPost, "http://localhost:8080", bytes.NewReader(pl))
if err != nil {
t.Fatalf("could not create test request: %v", err)
}
rec := httptest.NewRecorder()
handle(rec, req)
res := rec.Result()
if res.StatusCode != http.StatusOK {
t.Errorf("unexpected status code %s", res.Status)
}
defer res.Body.Close()
msg, err := ioutil.ReadAll(res.Body)
if err != nil {
t.Fatalf("could not read result payload: %v", err)
}
if exp := "pull request id: 191568743"; string(msg) != exp {
t.Fatalf("expected message %q; got %q", exp, msg)
}
}
func BenchmarkHandle(b *testing.B) {
b.StopTimer()
pl, err := ioutil.ReadFile("testdata/payload.json")
if err != nil {
b.Fatalf("could not read payload.json: %v", err)
}
for i := 0; i < b.N; i++ {
req, err := http.NewRequest(http.MethodPost, "http://localhost:8080", bytes.NewReader(pl))
if err != nil {
b.Fatalf("could not create test request: %v", err)
}
rec := httptest.NewRecorder()
b.StartTimer()
handle(rec, req)
b.StopTimer()
}
}