2015-02-05 22:56:50 +01:00
|
|
|
package context
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
2015-03-25 00:46:08 +01:00
|
|
|
"net/http/httptest"
|
|
|
|
"net/http/httputil"
|
|
|
|
"net/url"
|
2015-02-05 22:56:50 +01:00
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestWithRequest(t *testing.T) {
|
|
|
|
var req http.Request
|
|
|
|
|
|
|
|
start := time.Now()
|
2022-11-02 23:31:23 +01:00
|
|
|
req.Method = http.MethodGet
|
2015-02-05 22:56:50 +01:00
|
|
|
req.Host = "example.com"
|
|
|
|
req.RequestURI = "/test-test"
|
|
|
|
req.Header = make(http.Header)
|
|
|
|
req.Header.Set("Referer", "foo.com/referer")
|
|
|
|
req.Header.Set("User-Agent", "test/0.1")
|
|
|
|
|
2015-04-01 18:46:35 +02:00
|
|
|
ctx := WithRequest(Background(), &req)
|
2023-04-30 14:43:13 +02:00
|
|
|
for _, tc := range []struct {
|
2015-02-05 22:56:50 +01:00
|
|
|
key string
|
|
|
|
expected interface{}
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
key: "http.request",
|
|
|
|
expected: &req,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: "http.request.id",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: "http.request.method",
|
|
|
|
expected: req.Method,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: "http.request.host",
|
|
|
|
expected: req.Host,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: "http.request.uri",
|
|
|
|
expected: req.RequestURI,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: "http.request.referer",
|
|
|
|
expected: req.Referer(),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: "http.request.useragent",
|
|
|
|
expected: req.UserAgent(),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: "http.request.remoteaddr",
|
|
|
|
expected: req.RemoteAddr,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: "http.request.startedat",
|
|
|
|
},
|
|
|
|
} {
|
2023-04-30 14:43:13 +02:00
|
|
|
v := ctx.Value(tc.key)
|
2015-02-05 22:56:50 +01:00
|
|
|
|
|
|
|
if v == nil {
|
2023-04-30 14:43:13 +02:00
|
|
|
t.Fatalf("value not found for %q", tc.key)
|
2015-02-05 22:56:50 +01:00
|
|
|
}
|
|
|
|
|
2023-04-30 14:43:13 +02:00
|
|
|
if tc.expected != nil && v != tc.expected {
|
|
|
|
t.Fatalf("%s: %v != %v", tc.key, v, tc.expected)
|
2015-02-05 22:56:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Key specific checks!
|
2023-04-30 14:43:13 +02:00
|
|
|
switch tc.key {
|
2015-02-05 22:56:50 +01:00
|
|
|
case "http.request.id":
|
|
|
|
if _, ok := v.(string); !ok {
|
|
|
|
t.Fatalf("request id not a string: %v", v)
|
|
|
|
}
|
|
|
|
case "http.request.startedat":
|
|
|
|
vt, ok := v.(time.Time)
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("value not a time: %v", v)
|
|
|
|
}
|
|
|
|
|
|
|
|
now := time.Now()
|
|
|
|
if vt.After(now) {
|
|
|
|
t.Fatalf("time generated too late: %v > %v", vt, now)
|
|
|
|
}
|
|
|
|
|
|
|
|
if vt.Before(start) {
|
|
|
|
t.Fatalf("time generated too early: %v < %v", vt, start)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type testResponseWriter struct {
|
|
|
|
flushed bool
|
|
|
|
status int
|
|
|
|
written int64
|
|
|
|
header http.Header
|
|
|
|
}
|
|
|
|
|
|
|
|
func (trw *testResponseWriter) Header() http.Header {
|
|
|
|
if trw.header == nil {
|
|
|
|
trw.header = make(http.Header)
|
|
|
|
}
|
|
|
|
|
|
|
|
return trw.header
|
|
|
|
}
|
|
|
|
|
|
|
|
func (trw *testResponseWriter) Write(p []byte) (n int, err error) {
|
|
|
|
if trw.status == 0 {
|
|
|
|
trw.status = http.StatusOK
|
|
|
|
}
|
|
|
|
|
|
|
|
n = len(p)
|
|
|
|
trw.written += int64(n)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (trw *testResponseWriter) WriteHeader(status int) {
|
|
|
|
trw.status = status
|
|
|
|
}
|
|
|
|
|
|
|
|
func (trw *testResponseWriter) Flush() {
|
|
|
|
trw.flushed = true
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWithResponseWriter(t *testing.T) {
|
|
|
|
trw := testResponseWriter{}
|
2015-04-01 18:46:35 +02:00
|
|
|
ctx, rw := WithResponseWriter(Background(), &trw)
|
2015-02-05 22:56:50 +01:00
|
|
|
|
2015-04-20 20:15:01 +02:00
|
|
|
if ctx.Value("http.response") != rw {
|
|
|
|
t.Fatalf("response not available in context: %v != %v", ctx.Value("http.response"), rw)
|
|
|
|
}
|
|
|
|
|
|
|
|
grw, err := GetResponseWriter(ctx)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error getting response writer: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if grw != rw {
|
|
|
|
t.Fatalf("unexpected response writer returned: %#v != %#v", grw, rw)
|
2015-02-05 22:56:50 +01:00
|
|
|
}
|
|
|
|
|
2015-05-05 23:21:33 +02:00
|
|
|
if ctx.Value("http.response.status") != 0 {
|
|
|
|
t.Fatalf("response status should always be a number and should be zero here: %v != 0", ctx.Value("http.response.status"))
|
|
|
|
}
|
|
|
|
|
2015-02-05 22:56:50 +01:00
|
|
|
if n, err := rw.Write(make([]byte, 1024)); err != nil {
|
|
|
|
t.Fatalf("unexpected error writing: %v", err)
|
|
|
|
} else if n != 1024 {
|
|
|
|
t.Fatalf("unexpected number of bytes written: %v != %v", n, 1024)
|
|
|
|
}
|
|
|
|
|
|
|
|
if ctx.Value("http.response.status") != http.StatusOK {
|
|
|
|
t.Fatalf("unexpected response status in context: %v != %v", ctx.Value("http.response.status"), http.StatusOK)
|
|
|
|
}
|
|
|
|
|
|
|
|
if ctx.Value("http.response.written") != int64(1024) {
|
|
|
|
t.Fatalf("unexpected number reported bytes written: %v != %v", ctx.Value("http.response.written"), 1024)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure flush propagates
|
|
|
|
rw.(http.Flusher).Flush()
|
|
|
|
|
|
|
|
if !trw.flushed {
|
|
|
|
t.Fatalf("response writer not flushed")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write another status and make sure context is correct. This normally
|
|
|
|
// wouldn't work except for in this contrived testcase.
|
|
|
|
rw.WriteHeader(http.StatusBadRequest)
|
|
|
|
|
|
|
|
if ctx.Value("http.response.status") != http.StatusBadRequest {
|
|
|
|
t.Fatalf("unexpected response status in context: %v != %v", ctx.Value("http.response.status"), http.StatusBadRequest)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWithVars(t *testing.T) {
|
|
|
|
var req http.Request
|
|
|
|
vars := map[string]string{
|
|
|
|
"foo": "asdf",
|
|
|
|
"bar": "qwer",
|
|
|
|
}
|
|
|
|
|
|
|
|
getVarsFromRequest = func(r *http.Request) map[string]string {
|
|
|
|
if r != &req {
|
|
|
|
t.Fatalf("unexpected request: %v != %v", r, req)
|
|
|
|
}
|
|
|
|
|
|
|
|
return vars
|
|
|
|
}
|
|
|
|
|
2015-04-01 18:46:35 +02:00
|
|
|
ctx := WithVars(Background(), &req)
|
2023-04-30 14:43:13 +02:00
|
|
|
for _, tc := range []struct {
|
2015-02-05 22:56:50 +01:00
|
|
|
key string
|
|
|
|
expected interface{}
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
key: "vars",
|
|
|
|
expected: vars,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: "vars.foo",
|
|
|
|
expected: "asdf",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: "vars.bar",
|
|
|
|
expected: "qwer",
|
|
|
|
},
|
|
|
|
} {
|
2023-04-30 14:43:13 +02:00
|
|
|
v := ctx.Value(tc.key)
|
2015-02-05 22:56:50 +01:00
|
|
|
|
2023-04-30 14:43:13 +02:00
|
|
|
if !reflect.DeepEqual(v, tc.expected) {
|
|
|
|
t.Fatalf("%q: %v != %v", tc.key, v, tc.expected)
|
2015-02-05 22:56:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-03-25 00:46:08 +01:00
|
|
|
|
|
|
|
// SingleHostReverseProxy will insert an X-Forwarded-For header, and can be used to test
|
|
|
|
// RemoteAddr(). A fake RemoteAddr cannot be set on the HTTP request - it is overwritten
|
|
|
|
// at the transport layer to 127.0.0.1:<port> . However, as the X-Forwarded-For header
|
|
|
|
// just contains the IP address, it is different enough for testing.
|
|
|
|
func TestRemoteAddr(t *testing.T) {
|
2015-03-25 19:11:46 +01:00
|
|
|
var expectedRemote string
|
2015-03-25 00:46:08 +01:00
|
|
|
backend := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
defer r.Body.Close()
|
|
|
|
|
|
|
|
if r.RemoteAddr == expectedRemote {
|
|
|
|
t.Errorf("Unexpected matching remote addresses")
|
|
|
|
}
|
|
|
|
|
2015-03-25 19:11:46 +01:00
|
|
|
actualRemote := RemoteAddr(r)
|
2015-03-25 00:46:08 +01:00
|
|
|
if expectedRemote != actualRemote {
|
|
|
|
t.Errorf("Mismatching remote hosts: %v != %v", expectedRemote, actualRemote)
|
|
|
|
}
|
|
|
|
|
|
|
|
w.WriteHeader(200)
|
|
|
|
}))
|
|
|
|
|
|
|
|
defer backend.Close()
|
|
|
|
backendURL, err := url.Parse(backend.URL)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
proxy := httputil.NewSingleHostReverseProxy(backendURL)
|
|
|
|
frontend := httptest.NewServer(proxy)
|
|
|
|
defer frontend.Close()
|
|
|
|
|
2015-03-25 19:11:46 +01:00
|
|
|
// X-Forwarded-For set by proxy
|
|
|
|
expectedRemote = "127.0.0.1"
|
2022-11-02 23:31:23 +01:00
|
|
|
proxyReq, err := http.NewRequest(http.MethodGet, frontend.URL, nil)
|
2015-03-25 00:46:08 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-03-25 19:11:46 +01:00
|
|
|
_, err = http.DefaultClient.Do(proxyReq)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// RemoteAddr in X-Real-Ip
|
2022-11-02 23:31:23 +01:00
|
|
|
getReq, err := http.NewRequest(http.MethodGet, backend.URL, nil)
|
2015-03-25 19:11:46 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
expectedRemote = "1.2.3.4"
|
|
|
|
getReq.Header["X-Real-ip"] = []string{expectedRemote}
|
2015-03-25 00:46:08 +01:00
|
|
|
_, err = http.DefaultClient.Do(getReq)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-03-25 19:11:46 +01:00
|
|
|
// Valid X-Real-Ip and invalid X-Forwarded-For
|
|
|
|
getReq.Header["X-forwarded-for"] = []string{"1.2.3"}
|
|
|
|
_, err = http.DefaultClient.Do(getReq)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-03-25 00:46:08 +01:00
|
|
|
}
|