Skip to content

Commit 1f64f7f

Browse files
authored
Merge pull request #105 from keep94/30730
Use a random available port for testing.
2 parents 2072f2e + 2495a50 commit 1f64f7f

File tree

1 file changed

+47
-31
lines changed

1 file changed

+47
-31
lines changed

senders/client_test.go

+47-31
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ package senders_test
33
import (
44
"bytes"
55
"compress/gzip"
6-
"context"
76
"io/ioutil"
87
"log"
98
"net/http"
9+
"net/http/httptest"
1010
"os"
1111
"strings"
1212
"testing"
@@ -19,44 +19,60 @@ import (
1919
)
2020

2121
const (
22-
wfPort = "8080"
23-
proxyPort = "8081"
24-
token = "DUMMY_TOKEN"
22+
token = "DUMMY_TOKEN"
2523
)
2624

27-
var requests = map[string][]string{}
25+
var requests = []string{}
26+
var wfPort string
27+
var proxyPort string
2828

2929
func TestMain(m *testing.M) {
30-
wf := http.Server{Addr: "localhost:" + wfPort}
31-
proxy := http.Server{Addr: "localhost:" + proxyPort}
3230

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) {
3455
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
4059
}
60+
w.WriteHeader(http.StatusForbidden)
61+
})
62+
return mux
63+
}
4164

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
4772
}
48-
4973
w.WriteHeader(http.StatusForbidden)
5074
})
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
6076
}
6177

6278
func readBodyIntoString(r *http.Request) {
@@ -72,9 +88,9 @@ func readBodyIntoString(r *http.Request) {
7288
if err != nil {
7389
log.Fatalln(err)
7490
}
75-
requests[wfPort] = append(requests[wfPort], string(data))
91+
requests = append(requests, string(data))
7692
} else {
77-
requests[wfPort] = append(requests[wfPort], string(b))
93+
requests = append(requests, string(b))
7894
}
7995
}
8096

@@ -136,7 +152,7 @@ func doTest(t *testing.T, wf senders.Sender) {
136152
hgFlag := false
137153
spansFlag := false
138154

139-
for _, request := range requests["8080"] {
155+
for _, request := range requests {
140156
if strings.Contains(request, "new-york.power.usage") {
141157
metricsFlag = true
142158
}

0 commit comments

Comments
 (0)