@@ -3,10 +3,10 @@ package senders_test
3
3
import (
4
4
"bytes"
5
5
"compress/gzip"
6
- "context"
7
6
"io/ioutil"
8
7
"log"
9
8
"net/http"
9
+ "net/http/httptest"
10
10
"os"
11
11
"strings"
12
12
"testing"
@@ -19,44 +19,60 @@ import (
19
19
)
20
20
21
21
const (
22
- wfPort = "8080"
23
- proxyPort = "8081"
24
- token = "DUMMY_TOKEN"
22
+ token = "DUMMY_TOKEN"
25
23
)
26
24
27
- var requests = map [string ][]string {}
25
+ var requests = []string {}
26
+ var wfPort string
27
+ var proxyPort string
28
28
29
29
func TestMain (m * testing.M ) {
30
- wf := http.Server {Addr : "localhost:" + wfPort }
31
- proxy := http.Server {Addr : "localhost:" + proxyPort }
32
30
33
- http .HandleFunc ("/" , func (w http.ResponseWriter , r * http.Request ) {
31
+ directServer := httptest .NewServer (directHandler ())
32
+ proxyServer := httptest .NewServer (proxyHandler ())
33
+ wfPort = extractPort (directServer .URL )
34
+ proxyPort = extractPort (proxyServer .URL )
35
+
36
+ exitVal := m .Run ()
37
+
38
+ directServer .Close ()
39
+ proxyServer .Close ()
40
+
41
+ os .Exit (exitVal )
42
+ }
43
+
44
+ func extractPort (url string ) string {
45
+ idx := strings .LastIndex (url , ":" )
46
+ if idx == - 1 {
47
+ log .Fatal ("No port found." )
48
+ }
49
+ return url [idx + 1 :]
50
+ }
51
+
52
+ func directHandler () http.Handler {
53
+ mux := http .NewServeMux ()
54
+ mux .HandleFunc ("/" , func (w http.ResponseWriter , r * http.Request ) {
34
55
readBodyIntoString (r )
35
- if strings .HasSuffix (r .Host , wfPort ) {
36
- if strings .HasSuffix (r .Header .Get ("Authorization" ), token ) {
37
- w .WriteHeader (http .StatusOK )
38
- return
39
- }
56
+ if strings .HasSuffix (r .Header .Get ("Authorization" ), token ) {
57
+ w .WriteHeader (http .StatusOK )
58
+ return
40
59
}
60
+ w .WriteHeader (http .StatusForbidden )
61
+ })
62
+ return mux
63
+ }
41
64
42
- if strings .HasSuffix (r .Host , proxyPort ) {
43
- if len (r .Header .Get ("Authorization" )) == 0 {
44
- w .WriteHeader (http .StatusOK )
45
- return
46
- }
65
+ func proxyHandler () http.Handler {
66
+ mux := http .NewServeMux ()
67
+ mux .HandleFunc ("/" , func (w http.ResponseWriter , r * http.Request ) {
68
+ readBodyIntoString (r )
69
+ if len (r .Header .Get ("Authorization" )) == 0 {
70
+ w .WriteHeader (http .StatusOK )
71
+ return
47
72
}
48
-
49
73
w .WriteHeader (http .StatusForbidden )
50
74
})
51
- go func () { wf .ListenAndServe () }()
52
- go func () { proxy .ListenAndServe () }()
53
-
54
- exitVal := m .Run ()
55
-
56
- wf .Shutdown (context .Background ())
57
- proxy .Shutdown (context .Background ())
58
-
59
- os .Exit (exitVal )
75
+ return mux
60
76
}
61
77
62
78
func readBodyIntoString (r * http.Request ) {
@@ -72,9 +88,9 @@ func readBodyIntoString(r *http.Request) {
72
88
if err != nil {
73
89
log .Fatalln (err )
74
90
}
75
- requests [ wfPort ] = append (requests [ wfPort ] , string (data ))
91
+ requests = append (requests , string (data ))
76
92
} else {
77
- requests [ wfPort ] = append (requests [ wfPort ] , string (b ))
93
+ requests = append (requests , string (b ))
78
94
}
79
95
}
80
96
@@ -136,7 +152,7 @@ func doTest(t *testing.T, wf senders.Sender) {
136
152
hgFlag := false
137
153
spansFlag := false
138
154
139
- for _ , request := range requests [ "8080" ] {
155
+ for _ , request := range requests {
140
156
if strings .Contains (request , "new-york.power.usage" ) {
141
157
metricsFlag = true
142
158
}
0 commit comments