-
Notifications
You must be signed in to change notification settings - Fork 1
/
suite_test.go
140 lines (120 loc) · 2.74 KB
/
suite_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
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package aws_test
import (
"flag"
"fmt"
"github.com/usiegj00/goamz-aws"
// "launchpad.net/goamz/aws"
. "launchpad.net/gocheck"
"net/http"
"net/url"
"os"
"testing"
"time"
)
func Test(t *testing.T) {
TestingT(t)
}
var integration = flag.Bool("i", false, "Enable integration tests")
type SuiteI struct {
auth aws.Auth
}
func (s *SuiteI) SetUpSuite(c *C) {
if !*integration {
c.Skip("Integration tests not enabled (-int flag)")
}
auth, err := aws.EnvAuth()
if err != nil {
c.Fatal(err)
}
s.auth = auth
}
type HTTPSuite struct{}
var testServer = NewTestHTTPServer("http://localhost:4444", 5 * time.Second)
func (s *HTTPSuite) SetUpSuite(c *C) {
testServer.Start()
}
func (s *HTTPSuite) TearDownTest(c *C) {
testServer.FlushRequests()
}
type TestHTTPServer struct {
URL string
Timeout time.Duration
started bool
request chan *http.Request
response chan *testResponse
pending chan bool
}
type testResponse struct {
Status int
Headers map[string]string
Body string
}
func NewTestHTTPServer(url_ string, timeout time.Duration) *TestHTTPServer {
return &TestHTTPServer{URL: url_, Timeout: timeout}
}
func (s *TestHTTPServer) Start() {
if s.started {
return
}
s.started = true
s.request = make(chan *http.Request, 64)
s.response = make(chan *testResponse, 64)
s.pending = make(chan bool, 64)
url, _ := url.Parse(s.URL)
go http.ListenAndServe(url.Host, s)
s.PrepareResponse(123, nil, "")
for {
// Wait for it to be up.
resp, err := http.Get(s.URL)
if err == nil && resp.StatusCode == 123 {
break
}
fmt.Fprintf(os.Stderr, "\nWaiting for fake server to be up... ")
time.Sleep(1e8)
}
fmt.Fprintf(os.Stderr, "done\n\n")
s.WaitRequest() // Consume dummy request.
}
// FlushRequests discards requests which were not yet consumed by WaitRequest.
func (s *TestHTTPServer) FlushRequests() {
for {
select {
case <-s.request:
default:
return
}
}
}
func (s *TestHTTPServer) ServeHTTP(w http.ResponseWriter, req *http.Request) {
s.request <- req
var resp *testResponse
select {
case resp = <-s.response:
case <-time.After(s.Timeout):
fmt.Fprintf(os.Stderr, "ERROR: Timeout waiting for test to provide response\n")
resp = &testResponse{500, nil, ""}
}
h := w.Header()
if resp.Headers != nil {
for k, v := range resp.Headers {
h.Set(k, v)
}
}
if resp.Status != 0 {
w.WriteHeader(resp.Status)
}
w.Write([]byte(resp.Body))
}
func (s *TestHTTPServer) WaitRequest() *http.Request {
select {
case req := <-s.request:
req.ParseForm()
return req
case <-time.After(s.Timeout):
panic("Timeout waiting for goamz request")
}
panic("unreached")
}
func (s *TestHTTPServer) PrepareResponse(status int, headers map[string]string, body string) {
s.response <- &testResponse{status, headers, body}
}