forked from segmentio/segment-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
65 lines (56 loc) · 1.28 KB
/
main_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
package main
import (
"fmt"
"log"
"net/http"
"net/http/httptest"
"net/url"
"testing"
)
type SegmentServer int
const (
CDN SegmentServer = iota
TrackingAPI
)
func TestSegmentReverseProxy(t *testing.T) {
cases := []struct {
url string
expectedServer SegmentServer
}{
{"/v1/projects", CDN},
{"/morse.js/v1", CDN},
{"/analytics.js/v1", CDN},
{"/v1/import", TrackingAPI},
{"/v1/pixel", TrackingAPI},
}
for _, c := range cases {
cdn := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if c.expectedServer == CDN {
fmt.Fprintln(w, "Hello, client")
} else {
t.Errorf("CDN unexpected request: %v\n", r.URL)
}
}))
trackingAPI := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if c.expectedServer == TrackingAPI {
fmt.Fprintln(w, "Hello, client")
} else {
t.Errorf("Tracking API unexpected request: %v\n", r.URL)
}
}))
proxy := httptest.NewServer(NewSegmentReverseProxy(mustParseUrl(cdn.URL), mustParseUrl(trackingAPI.URL)))
_, err := http.Get(proxy.URL + c.url)
if err != nil {
t.Fatal(err)
}
cdn.Close()
trackingAPI.Close()
}
}
func mustParseUrl(raw string) *url.URL {
u, err := url.Parse(raw)
if err != nil {
log.Fatal(err)
}
return u
}