@@ -0,0 +1,36 @@
1+package main
2+
3+import (
4+ "io/ioutil"
5+ "net"
6+ "net/http"
7+ "net/http/httptest"
8+ "net/url"
9+ "testing"
10+)
11+
12+func TestNewReverseProxy(t *testing.T) {
13+ backend := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
14+ w.WriteHeader(200)
15+ hostname, _, _ := net.SplitHostPort(r.Host)
16+ w.Write([]byte(hostname))
17+ }))
18+ defer backend.Close()
19+
20+ backendURL, _ := url.Parse(backend.URL)
21+ backendHostname := "upstream.127.0.0.1.xip.io"
22+ _, backendPort, _ := net.SplitHostPort(backendURL.Host)
23+ backendHost := net.JoinHostPort(backendHostname, backendPort)
24+ proxyURL, _ := url.Parse(backendURL.Scheme + "://" + backendHost + "/")
25+
26+ proxyHandler := NewReverseProxy(proxyURL)
27+ frontend := httptest.NewServer(proxyHandler)
28+ defer frontend.Close()
29+
30+ getReq, _ := http.NewRequest("GET", frontend.URL, nil)
31+ res, _ := http.DefaultClient.Do(getReq)
32+ bodyBytes, _ := ioutil.ReadAll(res.Body)
33+ if g, e := string(bodyBytes), backendHostname; g != e {
34+ t.Errorf("got body %q; expected %q", g, e)
35+ }
36+}