forked from mailgun/mailgun-go
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathacceptance_test.go
More file actions
99 lines (91 loc) · 2.73 KB
/
acceptance_test.go
File metadata and controls
99 lines (91 loc) · 2.73 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
package mailgun
import (
"bytes"
"crypto/rand"
"fmt"
"io"
"io/ioutil"
"mime"
"mime/multipart"
"net/http"
"net/url"
"os"
"strings"
"testing"
"github.com/facebookgo/ensure"
"github.com/onsi/ginkgo"
)
// Many tests require configuration settings unique to the user, passed in via
// environment variables. If these variables aren't set, we need to fail the test early.
func reqEnv(t ginkgo.GinkgoTInterface, variableName string) string {
value := os.Getenv(variableName)
ensure.True(t, value != "")
return value
}
func randomDomainURL(n int) string {
const alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
var bytes = make([]byte, n)
rand.Read(bytes)
for i, b := range bytes {
bytes[i] = alpha[b%byte(len(alpha))]
}
return "http://" + string(bytes) + ".com"
}
// randomString generates a string of given length, but random content.
// All content will be within the ASCII graphic character set.
// (Implementation from Even Shaw's contribution on
// http://stackoverflow.com/questions/12771930/what-is-the-fastest-way-to-generate-a-long-random-string-in-go).
func randomString(n int, prefix string) string {
const alphanum = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
var bytes = make([]byte, n)
rand.Read(bytes)
for i, b := range bytes {
bytes[i] = alphanum[b%byte(len(alphanum))]
}
return prefix + string(bytes)
}
func randomEmail(prefix, domain string) string {
return strings.ToLower(fmt.Sprintf("%s@%s", randomString(20, prefix), domain))
}
func spendMoney(t *testing.T, tFunc func()) {
ok := os.Getenv("MG_SPEND_MONEY")
if ok != "" {
tFunc()
} else {
t.Log("Money spending not allowed, not running function.")
}
}
func parseContentType(req *http.Request) (url.Values, error) {
contentType, attrs, _ := mime.ParseMediaType(req.Header.Get("Content-Type"))
if contentType != "multipart/form-data" {
return nil, fmt.Errorf("unexpected content type: %v", contentType)
}
raw, err := ioutil.ReadAll(req.Body)
if err != nil {
return nil, fmt.Errorf("err reading body of multipart request snapshot: %v", err)
}
boundary := attrs["boundary"]
reader := multipart.NewReader(bytes.NewReader(raw), boundary)
values := url.Values{}
for {
part, err := reader.NextPart()
if err != nil {
if err == io.EOF {
break
}
return nil, fmt.Errorf("err getting nextpart: %v", err)
}
data, err := ioutil.ReadAll(part)
if err != nil {
return nil, fmt.Errorf("err getting data from part: %v", err)
}
if bytes.Contains(data, []byte(boundary)) {
return nil, fmt.Errorf("part contains the boundary, which is not expected")
}
values.Set(part.FormName(), string(data))
if err = part.Close(); err != nil {
return nil, fmt.Errorf("err closing part: %v", err)
}
}
return values, nil
}