Skip to content

Commit

Permalink
Merge pull request #105 from keep94/30730
Browse files Browse the repository at this point in the history
Use a random available port for testing.
  • Loading branch information
keep94 authored Sep 2, 2022
2 parents 2072f2e + 2495a50 commit 1f64f7f
Showing 1 changed file with 47 additions and 31 deletions.
78 changes: 47 additions & 31 deletions senders/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package senders_test
import (
"bytes"
"compress/gzip"
"context"
"io/ioutil"
"log"
"net/http"
"net/http/httptest"
"os"
"strings"
"testing"
Expand All @@ -19,44 +19,60 @@ import (
)

const (
wfPort = "8080"
proxyPort = "8081"
token = "DUMMY_TOKEN"
token = "DUMMY_TOKEN"
)

var requests = map[string][]string{}
var requests = []string{}
var wfPort string
var proxyPort string

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

http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
directServer := httptest.NewServer(directHandler())
proxyServer := httptest.NewServer(proxyHandler())
wfPort = extractPort(directServer.URL)
proxyPort = extractPort(proxyServer.URL)

exitVal := m.Run()

directServer.Close()
proxyServer.Close()

os.Exit(exitVal)
}

func extractPort(url string) string {
idx := strings.LastIndex(url, ":")
if idx == -1 {
log.Fatal("No port found.")
}
return url[idx+1:]
}

func directHandler() http.Handler {
mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
readBodyIntoString(r)
if strings.HasSuffix(r.Host, wfPort) {
if strings.HasSuffix(r.Header.Get("Authorization"), token) {
w.WriteHeader(http.StatusOK)
return
}
if strings.HasSuffix(r.Header.Get("Authorization"), token) {
w.WriteHeader(http.StatusOK)
return
}
w.WriteHeader(http.StatusForbidden)
})
return mux
}

if strings.HasSuffix(r.Host, proxyPort) {
if len(r.Header.Get("Authorization")) == 0 {
w.WriteHeader(http.StatusOK)
return
}
func proxyHandler() http.Handler {
mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
readBodyIntoString(r)
if len(r.Header.Get("Authorization")) == 0 {
w.WriteHeader(http.StatusOK)
return
}

w.WriteHeader(http.StatusForbidden)
})
go func() { wf.ListenAndServe() }()
go func() { proxy.ListenAndServe() }()

exitVal := m.Run()

wf.Shutdown(context.Background())
proxy.Shutdown(context.Background())

os.Exit(exitVal)
return mux
}

func readBodyIntoString(r *http.Request) {
Expand All @@ -72,9 +88,9 @@ func readBodyIntoString(r *http.Request) {
if err != nil {
log.Fatalln(err)
}
requests[wfPort] = append(requests[wfPort], string(data))
requests = append(requests, string(data))
} else {
requests[wfPort] = append(requests[wfPort], string(b))
requests = append(requests, string(b))
}
}

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

for _, request := range requests["8080"] {
for _, request := range requests {
if strings.Contains(request, "new-york.power.usage") {
metricsFlag = true
}
Expand Down

0 comments on commit 1f64f7f

Please sign in to comment.