This repository was archived by the owner on Oct 22, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_test.go
63 lines (56 loc) · 1.56 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
package main_test
import (
"fmt"
"io"
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
"strings"
"testing"
. "code.cloudfoundry.org/quarks-gora"
)
func Test(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(Gora))
defer ts.Close()
newreq := func(method, url string, body io.Reader) *http.Request {
r, err := http.NewRequest(method, url, body)
if err != nil {
t.Fatal(err)
}
return r
}
os.Setenv("FOO", "BAR")
tests := []struct {
name string
r *http.Request
contains string
status int
}{
{name: "1: testing get", r: newreq("GET", ts.URL+"/", nil), contains: "FOO=BAR", status: 200},
{name: "2: testing post", r: newreq("POST", ts.URL+"/", strings.NewReader("exit 0")), contains: "OK", status: 200},
{name: "3: testing post (error)", r: newreq("POST", ts.URL+"/", strings.NewReader("exit 1")), contains: "failed executing", status: 500},
{name: "4: testing post (error) that shows error message", r: newreq("POST", ts.URL+"/", strings.NewReader("echo 'foofail' && exit 1")), contains: "foofail", status: 500},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
resp, err := http.DefaultClient.Do(tt.r)
if err != nil {
t.Fatal(err)
}
defer resp.Body.Close()
if resp.StatusCode != tt.status {
t.Fatal("Wrong status code returned")
}
body := resp.Body
data, err := ioutil.ReadAll(body)
if err != nil {
t.Fatal(err)
}
result := string(data)
if !strings.Contains(result, tt.contains) {
t.Fatal(fmt.Sprintf("Response didn't contained: %s", tt.contains))
}
})
}
}