-
Notifications
You must be signed in to change notification settings - Fork 8
/
main_test.go
84 lines (78 loc) · 2.13 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package forest
import (
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
"strings"
"testing"
)
// go test -coverprofile=cover.out && go tool cover -html=cover.out
var tsAPI *APITesting
func TestMain(m *testing.M) {
// disable for testing
ErrorColorSyntaxCode = ""
FatalColorSyntaxCode = ""
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if strings.HasSuffix(r.URL.Path, "404") {
w.WriteHeader(404)
return
}
if strings.HasSuffix(r.URL.Path, "jsonarray") {
w.Header().Add("Content-Type", "application/json")
fmt.Fprintln(w, "[42]")
return
}
if strings.HasSuffix(r.URL.Path, "jsondoc") {
w.Header().Add("Content-Type", "application/json")
fmt.Fprintln(w, "{\"Value\":42}")
return
}
if strings.HasSuffix(r.URL.Path, "json-nested-doc") {
w.Header().Add("Content-Type", "application/json")
fmt.Fprintln(w, `{"Root": {"Child":12} }`)
return
}
if strings.HasSuffix(r.URL.Path, "json-array-of-doc") {
w.Header().Add("Content-Type", "application/json")
fmt.Fprintln(w, `[{"digit":1}, {"digit":2} ]`)
return
}
if strings.HasSuffix(r.URL.Path, "xmldoc") {
w.Header().Add("Content-Type", "application/xml")
fmt.Fprintln(w, `<?xml version="1.0"?>
<Root>
<Child name="answer"><Value>1</Value></Child>
<Child name="answer"><Value>2</Value></Child>
</Root>`)
return
}
if strings.HasSuffix(r.URL.Path, "form") {
w.Header().Add("Content-Type", "text/plain")
if err := r.ParseForm(); err != nil {
w.WriteHeader(400)
return
}
fmt.Fprintln(w, fmt.Sprintf("%v", r.PostForm))
return
}
if strings.HasSuffix(r.URL.Path, "echo") {
w.Header().Add("Content-Type", "application/octet-stream")
w.Header().Add("ECHO", r.Header.Get("ECHO"))
data, _ := ioutil.ReadAll(r.Body)
defer r.Body.Close()
w.Write(data)
return
}
if r.Method == "POST" || r.Method == "PUT" || r.Method == "DELETE" || r.Method == "PATCH" {
w.WriteHeader(204)
}
// 200 is written
}))
tsAPI = NewClient(ts.URL, new(http.Client))
exitCode := m.Run()
// on early exit close will not be called
ts.Close()
os.Exit(exitCode)
}