This repository has been archived by the owner on Feb 7, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtest_helper.go
78 lines (59 loc) · 1.45 KB
/
test_helper.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
// Copyright © 2016-present Thomas Rabaix <[email protected]>.
//
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.
package gitlab_ci_helper
import (
"bytes"
"fmt"
"io"
"net/http"
"net/http/httptest"
"os"
"testing"
)
type FakeRequest struct {
Host string
Path string
Method string
Called int
Response *http.Response
}
func WrapperTestCommand(reqs []*FakeRequest, envs map[string]string, t *testing.T, fn func(ts *httptest.Server)) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Printf("Handling a request: %s\n", r.URL.Path)
// dummy matcher
for _, req := range reqs {
if req.Path == r.URL.Path && req.Method == r.Method {
fmt.Printf("Found request: %s\n", req.Path)
req.Called++
for name, values := range req.Response.Header {
for _, v := range values {
w.Header().Add(name, v)
}
}
buf := bytes.NewBuffer([]byte(""))
io.Copy(buf, req.Response.Body)
//bytes.NewBuffer(buf.Bytes()).WriteTo(os.Stdout)
bytes.NewBuffer(buf.Bytes()).WriteTo(w)
req.Response.Body.Close()
return
}
}
t.Error("Unable to find a response to handle the request")
}))
envs["GITLAB_HOST"] = ts.URL
defer func() {
ts.Close()
for name := range envs {
os.Unsetenv(name)
}
}()
for name, value := range envs {
err := os.Setenv(name, value)
if err != nil {
panic(err)
}
}
fn(ts)
}