2023-01-27 21:37:20 +00:00
|
|
|
// Copyright (c) Tailscale Inc & AUTHORS
|
|
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
2020-03-19 00:37:40 +00:00
|
|
|
|
|
|
|
package tsweb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"context"
|
|
|
|
"errors"
|
2022-03-20 06:16:06 +00:00
|
|
|
"fmt"
|
2020-03-19 00:37:40 +00:00
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2024-02-07 00:18:33 +00:00
|
|
|
"net/url"
|
2021-08-11 21:18:52 +00:00
|
|
|
"strings"
|
2020-03-19 00:37:40 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/google/go-cmp/cmp"
|
2020-04-29 02:56:11 +00:00
|
|
|
"tailscale.com/tstest"
|
2024-02-07 00:18:33 +00:00
|
|
|
"tailscale.com/util/must"
|
2023-02-01 00:01:02 +00:00
|
|
|
"tailscale.com/util/vizerror"
|
2020-03-19 00:37:40 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type noopHijacker struct {
|
|
|
|
*httptest.ResponseRecorder
|
|
|
|
hijacked bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *noopHijacker) Hijack() (net.Conn, *bufio.ReadWriter, error) {
|
|
|
|
// Hijack "successfully" but don't bother returning a conn.
|
|
|
|
h.hijacked = true
|
|
|
|
return nil, nil, nil
|
|
|
|
}
|
|
|
|
|
2020-04-05 16:29:24 +00:00
|
|
|
type handlerFunc func(http.ResponseWriter, *http.Request) error
|
|
|
|
|
|
|
|
func (f handlerFunc) ServeHTTPReturn(w http.ResponseWriter, r *http.Request) error {
|
|
|
|
return f(w, r)
|
|
|
|
}
|
|
|
|
|
2020-03-19 00:37:40 +00:00
|
|
|
func TestStdHandler(t *testing.T) {
|
2023-08-11 20:41:58 +00:00
|
|
|
const exampleRequestID = "example-request-id"
|
2020-03-19 00:37:40 +00:00
|
|
|
var (
|
2020-04-05 16:29:24 +00:00
|
|
|
handlerCode = func(code int) ReturnHandler {
|
|
|
|
return handlerFunc(func(w http.ResponseWriter, r *http.Request) error {
|
2020-03-19 00:37:40 +00:00
|
|
|
w.WriteHeader(code)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
2020-04-05 16:29:24 +00:00
|
|
|
handlerErr = func(code int, err error) ReturnHandler {
|
|
|
|
return handlerFunc(func(w http.ResponseWriter, r *http.Request) error {
|
2020-03-19 00:37:40 +00:00
|
|
|
if code != 0 {
|
|
|
|
w.WriteHeader(code)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
req = func(ctx context.Context, url string) *http.Request {
|
|
|
|
ret, err := http.NewRequestWithContext(ctx, "GET", url, nil)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
|
|
|
testErr = errors.New("test error")
|
|
|
|
bgCtx = context.Background()
|
|
|
|
// canceledCtx, cancel = context.WithCancel(bgCtx)
|
2023-07-06 20:09:24 +00:00
|
|
|
startTime = time.Unix(1687870000, 1234)
|
2020-03-19 00:37:40 +00:00
|
|
|
)
|
|
|
|
// cancel()
|
|
|
|
|
|
|
|
tests := []struct {
|
2023-11-28 23:37:21 +00:00
|
|
|
name string
|
|
|
|
rh ReturnHandler
|
|
|
|
r *http.Request
|
|
|
|
errHandler ErrorHandlerFunc
|
|
|
|
wantCode int
|
|
|
|
wantLog AccessLogRecord
|
|
|
|
wantBody string
|
2020-03-19 00:37:40 +00:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "handler returns 200",
|
2020-04-05 16:29:24 +00:00
|
|
|
rh: handlerCode(200),
|
2020-03-19 00:37:40 +00:00
|
|
|
r: req(bgCtx, "http://example.com/"),
|
|
|
|
wantCode: 200,
|
|
|
|
wantLog: AccessLogRecord{
|
2023-07-06 20:09:24 +00:00
|
|
|
When: startTime,
|
2020-03-19 00:37:40 +00:00
|
|
|
Seconds: 1.0,
|
|
|
|
Proto: "HTTP/1.1",
|
|
|
|
TLS: false,
|
|
|
|
Host: "example.com",
|
|
|
|
Method: "GET",
|
|
|
|
Code: 200,
|
|
|
|
RequestURI: "/",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
2023-08-11 20:41:58 +00:00
|
|
|
{
|
2023-11-28 23:37:21 +00:00
|
|
|
name: "handler returns 200 with request ID",
|
|
|
|
rh: handlerCode(200),
|
|
|
|
r: req(bgCtx, "http://example.com/"),
|
|
|
|
wantCode: 200,
|
2023-08-11 20:41:58 +00:00
|
|
|
wantLog: AccessLogRecord{
|
|
|
|
When: startTime,
|
|
|
|
Seconds: 1.0,
|
|
|
|
Proto: "HTTP/1.1",
|
|
|
|
TLS: false,
|
|
|
|
Host: "example.com",
|
|
|
|
Method: "GET",
|
|
|
|
Code: 200,
|
|
|
|
RequestURI: "/",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
2020-03-19 00:37:40 +00:00
|
|
|
{
|
|
|
|
name: "handler returns 404",
|
2020-04-05 16:29:24 +00:00
|
|
|
rh: handlerCode(404),
|
2020-03-19 00:37:40 +00:00
|
|
|
r: req(bgCtx, "http://example.com/foo"),
|
|
|
|
wantCode: 404,
|
|
|
|
wantLog: AccessLogRecord{
|
2023-07-06 20:09:24 +00:00
|
|
|
When: startTime,
|
2020-03-19 00:37:40 +00:00
|
|
|
Seconds: 1.0,
|
|
|
|
Proto: "HTTP/1.1",
|
|
|
|
Host: "example.com",
|
|
|
|
Method: "GET",
|
|
|
|
RequestURI: "/foo",
|
|
|
|
Code: 404,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
2023-08-11 20:41:58 +00:00
|
|
|
{
|
2023-11-28 23:37:21 +00:00
|
|
|
name: "handler returns 404 with request ID",
|
|
|
|
rh: handlerCode(404),
|
|
|
|
r: req(bgCtx, "http://example.com/foo"),
|
|
|
|
wantCode: 404,
|
2023-08-11 20:41:58 +00:00
|
|
|
wantLog: AccessLogRecord{
|
|
|
|
When: startTime,
|
|
|
|
Seconds: 1.0,
|
|
|
|
Proto: "HTTP/1.1",
|
|
|
|
Host: "example.com",
|
|
|
|
Method: "GET",
|
|
|
|
RequestURI: "/foo",
|
|
|
|
Code: 404,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
2020-03-19 00:37:40 +00:00
|
|
|
{
|
|
|
|
name: "handler returns 404 via HTTPError",
|
2020-04-05 16:29:24 +00:00
|
|
|
rh: handlerErr(0, Error(404, "not found", testErr)),
|
2020-03-19 00:37:40 +00:00
|
|
|
r: req(bgCtx, "http://example.com/foo"),
|
|
|
|
wantCode: 404,
|
|
|
|
wantLog: AccessLogRecord{
|
2023-07-06 20:09:24 +00:00
|
|
|
When: startTime,
|
2020-03-19 00:37:40 +00:00
|
|
|
Seconds: 1.0,
|
|
|
|
Proto: "HTTP/1.1",
|
|
|
|
Host: "example.com",
|
|
|
|
Method: "GET",
|
|
|
|
RequestURI: "/foo",
|
2020-09-17 13:56:12 +00:00
|
|
|
Err: "not found: " + testErr.Error(),
|
2020-03-19 00:37:40 +00:00
|
|
|
Code: 404,
|
|
|
|
},
|
2023-08-11 20:41:58 +00:00
|
|
|
wantBody: "not found\n",
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
2023-11-28 23:37:21 +00:00
|
|
|
name: "handler returns 404 via HTTPError with request ID",
|
|
|
|
rh: handlerErr(0, Error(404, "not found", testErr)),
|
2024-01-16 21:56:23 +00:00
|
|
|
r: req(RequestIDKey.WithValue(bgCtx, exampleRequestID), "http://example.com/foo"),
|
2023-11-28 23:37:21 +00:00
|
|
|
wantCode: 404,
|
2023-08-11 20:41:58 +00:00
|
|
|
wantLog: AccessLogRecord{
|
|
|
|
When: startTime,
|
|
|
|
Seconds: 1.0,
|
|
|
|
Proto: "HTTP/1.1",
|
|
|
|
Host: "example.com",
|
|
|
|
Method: "GET",
|
|
|
|
RequestURI: "/foo",
|
|
|
|
Err: "not found: " + testErr.Error(),
|
|
|
|
Code: 404,
|
|
|
|
RequestID: exampleRequestID,
|
|
|
|
},
|
|
|
|
wantBody: "not found\n" + exampleRequestID + "\n",
|
2020-03-19 00:37:40 +00:00
|
|
|
},
|
|
|
|
|
2020-04-03 17:37:26 +00:00
|
|
|
{
|
|
|
|
name: "handler returns 404 with nil child error",
|
2020-04-05 16:29:24 +00:00
|
|
|
rh: handlerErr(0, Error(404, "not found", nil)),
|
2020-04-03 17:37:26 +00:00
|
|
|
r: req(bgCtx, "http://example.com/foo"),
|
|
|
|
wantCode: 404,
|
|
|
|
wantLog: AccessLogRecord{
|
2023-07-06 20:09:24 +00:00
|
|
|
When: startTime,
|
2020-04-03 17:37:26 +00:00
|
|
|
Seconds: 1.0,
|
|
|
|
Proto: "HTTP/1.1",
|
|
|
|
Host: "example.com",
|
|
|
|
Method: "GET",
|
|
|
|
RequestURI: "/foo",
|
2020-09-17 13:56:12 +00:00
|
|
|
Err: "not found",
|
2020-04-03 17:37:26 +00:00
|
|
|
Code: 404,
|
|
|
|
},
|
2023-08-11 20:41:58 +00:00
|
|
|
wantBody: "not found\n",
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
2023-11-28 23:37:21 +00:00
|
|
|
name: "handler returns 404 with request ID and nil child error",
|
|
|
|
rh: handlerErr(0, Error(404, "not found", nil)),
|
2024-01-16 21:56:23 +00:00
|
|
|
r: req(RequestIDKey.WithValue(bgCtx, exampleRequestID), "http://example.com/foo"),
|
2023-11-28 23:37:21 +00:00
|
|
|
wantCode: 404,
|
2023-08-11 20:41:58 +00:00
|
|
|
wantLog: AccessLogRecord{
|
|
|
|
When: startTime,
|
|
|
|
Seconds: 1.0,
|
|
|
|
Proto: "HTTP/1.1",
|
|
|
|
Host: "example.com",
|
|
|
|
Method: "GET",
|
|
|
|
RequestURI: "/foo",
|
|
|
|
Err: "not found",
|
|
|
|
Code: 404,
|
|
|
|
RequestID: exampleRequestID,
|
|
|
|
},
|
|
|
|
wantBody: "not found\n" + exampleRequestID + "\n",
|
2020-04-03 17:37:26 +00:00
|
|
|
},
|
|
|
|
|
2023-02-01 00:01:02 +00:00
|
|
|
{
|
2023-02-01 18:38:45 +00:00
|
|
|
name: "handler returns user-visible error",
|
2023-02-01 00:01:02 +00:00
|
|
|
rh: handlerErr(0, vizerror.New("visible error")),
|
|
|
|
r: req(bgCtx, "http://example.com/foo"),
|
|
|
|
wantCode: 500,
|
|
|
|
wantLog: AccessLogRecord{
|
2023-07-06 20:09:24 +00:00
|
|
|
When: startTime,
|
2023-02-01 00:01:02 +00:00
|
|
|
Seconds: 1.0,
|
|
|
|
Proto: "HTTP/1.1",
|
|
|
|
Host: "example.com",
|
|
|
|
Method: "GET",
|
|
|
|
RequestURI: "/foo",
|
|
|
|
Err: "visible error",
|
|
|
|
Code: 500,
|
|
|
|
},
|
2023-08-11 20:41:58 +00:00
|
|
|
wantBody: "visible error\n",
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
2023-11-28 23:37:21 +00:00
|
|
|
name: "handler returns user-visible error with request ID",
|
|
|
|
rh: handlerErr(0, vizerror.New("visible error")),
|
2024-01-16 21:56:23 +00:00
|
|
|
r: req(RequestIDKey.WithValue(bgCtx, exampleRequestID), "http://example.com/foo"),
|
2023-11-28 23:37:21 +00:00
|
|
|
wantCode: 500,
|
2023-08-11 20:41:58 +00:00
|
|
|
wantLog: AccessLogRecord{
|
|
|
|
When: startTime,
|
|
|
|
Seconds: 1.0,
|
|
|
|
Proto: "HTTP/1.1",
|
|
|
|
Host: "example.com",
|
|
|
|
Method: "GET",
|
|
|
|
RequestURI: "/foo",
|
|
|
|
Err: "visible error",
|
|
|
|
Code: 500,
|
|
|
|
RequestID: exampleRequestID,
|
|
|
|
},
|
|
|
|
wantBody: "visible error\n" + exampleRequestID + "\n",
|
2023-02-01 00:01:02 +00:00
|
|
|
},
|
|
|
|
|
2023-02-01 18:38:45 +00:00
|
|
|
{
|
|
|
|
name: "handler returns user-visible error wrapped by private error",
|
|
|
|
rh: handlerErr(0, fmt.Errorf("private internal error: %w", vizerror.New("visible error"))),
|
|
|
|
r: req(bgCtx, "http://example.com/foo"),
|
|
|
|
wantCode: 500,
|
|
|
|
wantLog: AccessLogRecord{
|
2023-07-06 20:09:24 +00:00
|
|
|
When: startTime,
|
2023-02-01 18:38:45 +00:00
|
|
|
Seconds: 1.0,
|
|
|
|
Proto: "HTTP/1.1",
|
|
|
|
Host: "example.com",
|
|
|
|
Method: "GET",
|
|
|
|
RequestURI: "/foo",
|
|
|
|
Err: "visible error",
|
|
|
|
Code: 500,
|
|
|
|
},
|
2023-08-11 20:41:58 +00:00
|
|
|
wantBody: "visible error\n",
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
2023-11-28 23:37:21 +00:00
|
|
|
name: "handler returns user-visible error wrapped by private error with request ID",
|
|
|
|
rh: handlerErr(0, fmt.Errorf("private internal error: %w", vizerror.New("visible error"))),
|
2024-01-16 21:56:23 +00:00
|
|
|
r: req(RequestIDKey.WithValue(bgCtx, exampleRequestID), "http://example.com/foo"),
|
2023-11-28 23:37:21 +00:00
|
|
|
wantCode: 500,
|
2023-08-11 20:41:58 +00:00
|
|
|
wantLog: AccessLogRecord{
|
|
|
|
When: startTime,
|
|
|
|
Seconds: 1.0,
|
|
|
|
Proto: "HTTP/1.1",
|
|
|
|
Host: "example.com",
|
|
|
|
Method: "GET",
|
|
|
|
RequestURI: "/foo",
|
|
|
|
Err: "visible error",
|
|
|
|
Code: 500,
|
|
|
|
RequestID: exampleRequestID,
|
|
|
|
},
|
|
|
|
wantBody: "visible error\n" + exampleRequestID + "\n",
|
2023-02-01 18:38:45 +00:00
|
|
|
},
|
|
|
|
|
2020-03-19 00:37:40 +00:00
|
|
|
{
|
|
|
|
name: "handler returns generic error",
|
2020-04-05 16:29:24 +00:00
|
|
|
rh: handlerErr(0, testErr),
|
2020-03-19 00:37:40 +00:00
|
|
|
r: req(bgCtx, "http://example.com/foo"),
|
|
|
|
wantCode: 500,
|
|
|
|
wantLog: AccessLogRecord{
|
2023-07-06 20:09:24 +00:00
|
|
|
When: startTime,
|
2020-03-19 00:37:40 +00:00
|
|
|
Seconds: 1.0,
|
|
|
|
Proto: "HTTP/1.1",
|
|
|
|
Host: "example.com",
|
|
|
|
Method: "GET",
|
|
|
|
RequestURI: "/foo",
|
|
|
|
Err: testErr.Error(),
|
|
|
|
Code: 500,
|
|
|
|
},
|
2023-08-11 20:41:58 +00:00
|
|
|
wantBody: "internal server error\n",
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
2023-11-28 23:37:21 +00:00
|
|
|
name: "handler returns generic error with request ID",
|
|
|
|
rh: handlerErr(0, testErr),
|
2024-01-16 21:56:23 +00:00
|
|
|
r: req(RequestIDKey.WithValue(bgCtx, exampleRequestID), "http://example.com/foo"),
|
2023-11-28 23:37:21 +00:00
|
|
|
wantCode: 500,
|
2023-08-11 20:41:58 +00:00
|
|
|
wantLog: AccessLogRecord{
|
|
|
|
When: startTime,
|
|
|
|
Seconds: 1.0,
|
|
|
|
Proto: "HTTP/1.1",
|
|
|
|
Host: "example.com",
|
|
|
|
Method: "GET",
|
|
|
|
RequestURI: "/foo",
|
|
|
|
Err: testErr.Error(),
|
|
|
|
Code: 500,
|
|
|
|
RequestID: exampleRequestID,
|
|
|
|
},
|
|
|
|
wantBody: "internal server error\n" + exampleRequestID + "\n",
|
2020-03-19 00:37:40 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
name: "handler returns error after writing response",
|
2020-04-05 16:29:24 +00:00
|
|
|
rh: handlerErr(200, testErr),
|
2020-03-19 00:37:40 +00:00
|
|
|
r: req(bgCtx, "http://example.com/foo"),
|
|
|
|
wantCode: 200,
|
|
|
|
wantLog: AccessLogRecord{
|
2023-07-06 20:09:24 +00:00
|
|
|
When: startTime,
|
2020-03-19 00:37:40 +00:00
|
|
|
Seconds: 1.0,
|
|
|
|
Proto: "HTTP/1.1",
|
|
|
|
Host: "example.com",
|
|
|
|
Method: "GET",
|
|
|
|
RequestURI: "/foo",
|
|
|
|
Err: testErr.Error(),
|
|
|
|
Code: 200,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
2023-08-11 20:41:58 +00:00
|
|
|
{
|
2023-11-28 23:37:21 +00:00
|
|
|
name: "handler returns error after writing response with request ID",
|
|
|
|
rh: handlerErr(200, testErr),
|
2024-01-16 21:56:23 +00:00
|
|
|
r: req(RequestIDKey.WithValue(bgCtx, exampleRequestID), "http://example.com/foo"),
|
2023-11-28 23:37:21 +00:00
|
|
|
wantCode: 200,
|
2023-08-11 20:41:58 +00:00
|
|
|
wantLog: AccessLogRecord{
|
|
|
|
When: startTime,
|
|
|
|
Seconds: 1.0,
|
|
|
|
Proto: "HTTP/1.1",
|
|
|
|
Host: "example.com",
|
|
|
|
Method: "GET",
|
|
|
|
RequestURI: "/foo",
|
|
|
|
Err: testErr.Error(),
|
|
|
|
Code: 200,
|
|
|
|
RequestID: exampleRequestID,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
2020-03-19 00:37:40 +00:00
|
|
|
{
|
|
|
|
name: "handler returns HTTPError after writing response",
|
2020-04-05 16:29:24 +00:00
|
|
|
rh: handlerErr(200, Error(404, "not found", testErr)),
|
2020-03-19 00:37:40 +00:00
|
|
|
r: req(bgCtx, "http://example.com/foo"),
|
|
|
|
wantCode: 200,
|
|
|
|
wantLog: AccessLogRecord{
|
2023-07-06 20:09:24 +00:00
|
|
|
When: startTime,
|
2020-03-19 00:37:40 +00:00
|
|
|
Seconds: 1.0,
|
|
|
|
Proto: "HTTP/1.1",
|
|
|
|
Host: "example.com",
|
|
|
|
Method: "GET",
|
|
|
|
RequestURI: "/foo",
|
2020-09-17 13:56:12 +00:00
|
|
|
Err: "not found: " + testErr.Error(),
|
2020-03-19 00:37:40 +00:00
|
|
|
Code: 200,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
name: "handler does nothing",
|
2020-04-05 16:29:24 +00:00
|
|
|
rh: handlerFunc(func(http.ResponseWriter, *http.Request) error { return nil }),
|
2020-03-19 00:37:40 +00:00
|
|
|
r: req(bgCtx, "http://example.com/foo"),
|
2020-04-04 15:55:10 +00:00
|
|
|
wantCode: 200,
|
2020-03-19 00:37:40 +00:00
|
|
|
wantLog: AccessLogRecord{
|
2023-07-06 20:09:24 +00:00
|
|
|
When: startTime,
|
2020-03-19 00:37:40 +00:00
|
|
|
Seconds: 1.0,
|
|
|
|
Proto: "HTTP/1.1",
|
|
|
|
Host: "example.com",
|
|
|
|
Method: "GET",
|
|
|
|
RequestURI: "/foo",
|
2020-04-04 15:55:10 +00:00
|
|
|
Code: 200,
|
2020-03-19 00:37:40 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
name: "handler hijacks conn",
|
2020-04-05 16:29:24 +00:00
|
|
|
rh: handlerFunc(func(w http.ResponseWriter, r *http.Request) error {
|
2020-03-19 00:37:40 +00:00
|
|
|
_, _, err := w.(http.Hijacker).Hijack()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("couldn't hijack: %v", err)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}),
|
|
|
|
r: req(bgCtx, "http://example.com/foo"),
|
2020-04-04 15:55:10 +00:00
|
|
|
wantCode: 200,
|
2020-03-19 00:37:40 +00:00
|
|
|
wantLog: AccessLogRecord{
|
2023-07-06 20:09:24 +00:00
|
|
|
When: startTime,
|
2020-03-19 00:37:40 +00:00
|
|
|
Seconds: 1.0,
|
|
|
|
|
|
|
|
Proto: "HTTP/1.1",
|
|
|
|
Host: "example.com",
|
|
|
|
Method: "GET",
|
|
|
|
RequestURI: "/foo",
|
|
|
|
Code: 101,
|
|
|
|
},
|
|
|
|
},
|
2023-08-11 20:41:58 +00:00
|
|
|
|
2022-06-08 20:05:15 +00:00
|
|
|
{
|
|
|
|
name: "error handler gets run",
|
|
|
|
rh: handlerErr(0, Error(404, "not found", nil)), // status code changed in errHandler
|
|
|
|
r: req(bgCtx, "http://example.com/"),
|
|
|
|
wantCode: 200,
|
|
|
|
errHandler: func(w http.ResponseWriter, r *http.Request, e HTTPError) {
|
|
|
|
http.Error(w, e.Msg, 200)
|
|
|
|
},
|
|
|
|
wantLog: AccessLogRecord{
|
2023-07-06 20:09:24 +00:00
|
|
|
When: startTime,
|
2022-06-08 20:05:15 +00:00
|
|
|
Seconds: 1.0,
|
|
|
|
Proto: "HTTP/1.1",
|
|
|
|
TLS: false,
|
|
|
|
Host: "example.com",
|
|
|
|
Method: "GET",
|
|
|
|
Code: 404,
|
|
|
|
Err: "not found",
|
|
|
|
RequestURI: "/",
|
|
|
|
},
|
2023-08-11 20:41:58 +00:00
|
|
|
wantBody: "not found\n",
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
2023-11-28 23:37:21 +00:00
|
|
|
name: "error handler gets run with request ID",
|
|
|
|
rh: handlerErr(0, Error(404, "not found", nil)), // status code changed in errHandler
|
2024-01-16 21:56:23 +00:00
|
|
|
r: req(RequestIDKey.WithValue(bgCtx, exampleRequestID), "http://example.com/"),
|
2023-11-28 23:37:21 +00:00
|
|
|
wantCode: 200,
|
2023-08-11 20:41:58 +00:00
|
|
|
errHandler: func(w http.ResponseWriter, r *http.Request, e HTTPError) {
|
2023-11-28 23:37:21 +00:00
|
|
|
requestID := RequestIDFromContext(r.Context())
|
|
|
|
http.Error(w, fmt.Sprintf("%s with request ID %s", e.Msg, requestID), 200)
|
2023-08-11 20:41:58 +00:00
|
|
|
},
|
|
|
|
wantLog: AccessLogRecord{
|
|
|
|
When: startTime,
|
|
|
|
Seconds: 1.0,
|
|
|
|
Proto: "HTTP/1.1",
|
|
|
|
TLS: false,
|
|
|
|
Host: "example.com",
|
|
|
|
Method: "GET",
|
|
|
|
Code: 404,
|
|
|
|
Err: "not found",
|
|
|
|
RequestURI: "/",
|
|
|
|
RequestID: exampleRequestID,
|
|
|
|
},
|
|
|
|
wantBody: "not found with request ID " + exampleRequestID + "\n",
|
|
|
|
},
|
2020-03-19 00:37:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
t.Run(test.name, func(t *testing.T) {
|
|
|
|
var logs []AccessLogRecord
|
2022-03-16 23:27:57 +00:00
|
|
|
logf := func(fmt string, args ...any) {
|
2020-03-19 00:37:40 +00:00
|
|
|
if fmt == "%s" {
|
|
|
|
logs = append(logs, args[0].(AccessLogRecord))
|
|
|
|
}
|
|
|
|
t.Logf(fmt, args...)
|
|
|
|
}
|
|
|
|
|
2023-07-06 20:09:24 +00:00
|
|
|
clock := tstest.NewClock(tstest.ClockOpts{
|
|
|
|
Start: startTime,
|
|
|
|
Step: time.Second,
|
|
|
|
})
|
2020-03-19 00:37:40 +00:00
|
|
|
|
|
|
|
rec := noopHijacker{httptest.NewRecorder(), false}
|
2023-11-28 23:37:21 +00:00
|
|
|
h := StdHandler(test.rh, HandlerOptions{Logf: logf, Now: clock.Now, OnError: test.errHandler})
|
2020-03-19 00:37:40 +00:00
|
|
|
h.ServeHTTP(&rec, test.r)
|
2020-04-04 15:55:10 +00:00
|
|
|
res := rec.Result()
|
|
|
|
if res.StatusCode != test.wantCode {
|
|
|
|
t.Errorf("HTTP code = %v, want %v", res.StatusCode, test.wantCode)
|
2020-03-19 00:37:40 +00:00
|
|
|
}
|
|
|
|
if len(logs) != 1 {
|
|
|
|
t.Errorf("handler didn't write a request log")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
errTransform := cmp.Transformer("err", func(e error) string {
|
|
|
|
if e == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return e.Error()
|
|
|
|
})
|
|
|
|
if diff := cmp.Diff(logs[0], test.wantLog, errTransform); diff != "" {
|
|
|
|
t.Errorf("handler wrote incorrect request log (-got+want):\n%s", diff)
|
|
|
|
}
|
2023-08-11 20:41:58 +00:00
|
|
|
if diff := cmp.Diff(rec.Body.String(), test.wantBody); diff != "" {
|
|
|
|
t.Errorf("handler wrote incorrect body (-got+want):\n%s", diff)
|
|
|
|
}
|
2020-03-19 00:37:40 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2020-04-05 19:12:15 +00:00
|
|
|
|
|
|
|
func BenchmarkLogNot200(b *testing.B) {
|
|
|
|
b.ReportAllocs()
|
|
|
|
rh := handlerFunc(func(w http.ResponseWriter, r *http.Request) error {
|
|
|
|
// Implicit 200 OK.
|
|
|
|
return nil
|
|
|
|
})
|
2022-09-23 17:19:13 +00:00
|
|
|
h := StdHandler(rh, HandlerOptions{QuietLoggingIfSuccessful: true})
|
2020-04-05 19:12:15 +00:00
|
|
|
req := httptest.NewRequest("GET", "/", nil)
|
|
|
|
rw := new(httptest.ResponseRecorder)
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
*rw = httptest.ResponseRecorder{}
|
|
|
|
h.ServeHTTP(rw, req)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkLog(b *testing.B) {
|
|
|
|
b.ReportAllocs()
|
|
|
|
rh := handlerFunc(func(w http.ResponseWriter, r *http.Request) error {
|
|
|
|
// Implicit 200 OK.
|
|
|
|
return nil
|
|
|
|
})
|
2021-06-16 04:48:47 +00:00
|
|
|
h := StdHandler(rh, HandlerOptions{})
|
2020-04-05 19:12:15 +00:00
|
|
|
req := httptest.NewRequest("GET", "/", nil)
|
|
|
|
rw := new(httptest.ResponseRecorder)
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
*rw = httptest.ResponseRecorder{}
|
|
|
|
h.ServeHTTP(rw, req)
|
|
|
|
}
|
|
|
|
}
|
2021-08-10 20:38:12 +00:00
|
|
|
|
2022-10-05 03:39:10 +00:00
|
|
|
func TestHTTPError_Unwrap(t *testing.T) {
|
|
|
|
wrappedErr := fmt.Errorf("wrapped")
|
|
|
|
err := Error(404, "not found", wrappedErr)
|
|
|
|
if got := errors.Unwrap(err); got != wrappedErr {
|
|
|
|
t.Errorf("HTTPError.Unwrap() = %v, want %v", got, wrappedErr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-04 04:42:24 +00:00
|
|
|
func TestAcceptsEncoding(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
in, enc string
|
|
|
|
want bool
|
|
|
|
}{
|
|
|
|
{"", "gzip", false},
|
|
|
|
{"gzip", "gzip", true},
|
|
|
|
{"foo,gzip", "gzip", true},
|
|
|
|
{"foo, gzip", "gzip", true},
|
|
|
|
{"foo, gzip ", "gzip", true},
|
|
|
|
{"gzip, foo ", "gzip", true},
|
|
|
|
{"gzip, foo ", "br", false},
|
|
|
|
{"gzip, foo ", "fo", false},
|
|
|
|
{"gzip;q=1.2, foo ", "gzip", true},
|
|
|
|
{" gzip;q=1.2, foo ", "gzip", true},
|
|
|
|
}
|
|
|
|
for i, tt := range tests {
|
|
|
|
h := make(http.Header)
|
|
|
|
if tt.in != "" {
|
|
|
|
h.Set("Accept-Encoding", tt.in)
|
|
|
|
}
|
|
|
|
got := AcceptsEncoding(&http.Request{Header: h}, tt.enc)
|
|
|
|
if got != tt.want {
|
|
|
|
t.Errorf("%d. got %v; want %v", i, got, tt.want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-06-05 18:23:55 +00:00
|
|
|
|
|
|
|
func TestPort80Handler(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
h *Port80Handler
|
|
|
|
req string
|
|
|
|
wantLoc string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "no_fqdn",
|
|
|
|
h: &Port80Handler{},
|
|
|
|
req: "GET / HTTP/1.1\r\nHost: foo.com\r\n\r\n",
|
|
|
|
wantLoc: "https://foo.com/",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "fqdn_and_path",
|
|
|
|
h: &Port80Handler{FQDN: "bar.com"},
|
|
|
|
req: "GET /path HTTP/1.1\r\nHost: foo.com\r\n\r\n",
|
|
|
|
wantLoc: "https://bar.com/path",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "path_and_query_string",
|
|
|
|
h: &Port80Handler{FQDN: "baz.com"},
|
|
|
|
req: "GET /path?a=b HTTP/1.1\r\nHost: foo.com\r\n\r\n",
|
|
|
|
wantLoc: "https://baz.com/path?a=b",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
r, _ := http.ReadRequest(bufio.NewReader(strings.NewReader(tt.req)))
|
|
|
|
rec := httptest.NewRecorder()
|
|
|
|
tt.h.ServeHTTP(rec, r)
|
|
|
|
got := rec.Result()
|
|
|
|
if got, want := got.StatusCode, 302; got != want {
|
|
|
|
t.Errorf("got status code %v; want %v", got, want)
|
|
|
|
}
|
|
|
|
if got, want := got.Header.Get("Location"), "https://foo.com/"; got != tt.wantLoc {
|
|
|
|
t.Errorf("Location = %q; want %q", got, want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2024-01-19 22:15:27 +00:00
|
|
|
|
|
|
|
func TestCleanRedirectURL(t *testing.T) {
|
|
|
|
tailscaleHost := []string{"tailscale.com"}
|
|
|
|
tailscaleAndOtherHost := []string{"microsoft.com", "tailscale.com"}
|
|
|
|
localHost := []string{"127.0.0.1", "localhost"}
|
|
|
|
myServer := []string{"myserver"}
|
|
|
|
cases := []struct {
|
|
|
|
url string
|
|
|
|
hosts []string
|
|
|
|
want string
|
|
|
|
}{
|
|
|
|
{"http://tailscale.com/foo", tailscaleHost, "http://tailscale.com/foo"},
|
|
|
|
{"http://tailscale.com/foo", tailscaleAndOtherHost, "http://tailscale.com/foo"},
|
|
|
|
{"http://microsoft.com/foo", tailscaleAndOtherHost, "http://microsoft.com/foo"},
|
|
|
|
{"https://tailscale.com/foo", tailscaleHost, "https://tailscale.com/foo"},
|
|
|
|
{"/foo", tailscaleHost, "/foo"},
|
|
|
|
{"//tailscale.com/foo", tailscaleHost, "//tailscale.com/foo"},
|
|
|
|
|
|
|
|
{"/a/foobar", tailscaleHost, "/a/foobar"},
|
|
|
|
{"http://127.0.0.1/a/foobar", localHost, "http://127.0.0.1/a/foobar"},
|
|
|
|
{"http://127.0.0.1:123/a/foobar", localHost, "http://127.0.0.1:123/a/foobar"},
|
|
|
|
{"http://127.0.0.1:31544/a/foobar", localHost, "http://127.0.0.1:31544/a/foobar"},
|
|
|
|
{"http://localhost/a/foobar", localHost, "http://localhost/a/foobar"},
|
|
|
|
{"http://localhost:123/a/foobar", localHost, "http://localhost:123/a/foobar"},
|
|
|
|
{"http://localhost:31544/a/foobar", localHost, "http://localhost:31544/a/foobar"},
|
|
|
|
{"http://myserver/a/foobar", myServer, "http://myserver/a/foobar"},
|
|
|
|
{"http://myserver:123/a/foobar", myServer, "http://myserver:123/a/foobar"},
|
|
|
|
{"http://myserver:31544/a/foobar", myServer, "http://myserver:31544/a/foobar"},
|
|
|
|
{"http://evil.com/foo", tailscaleHost, ""},
|
|
|
|
{"//evil.com", tailscaleHost, ""},
|
|
|
|
{"HttP://tailscale.com", tailscaleHost, "http://tailscale.com"},
|
|
|
|
{"http://TaIlScAlE.CoM/spongebob", tailscaleHost, "http://TaIlScAlE.CoM/spongebob"},
|
|
|
|
{"ftp://tailscale.com", tailscaleHost, ""},
|
|
|
|
{"https:/evil.com", tailscaleHost, ""}, // regression test for tailscale/corp#892
|
|
|
|
{"%2Fa%2F44869c061701", tailscaleHost, "/a/44869c061701"}, // regression test for tailscale/corp#13288
|
|
|
|
{"https%3A%2Ftailscale.com", tailscaleHost, ""}, // escaped colon-single-slash malformed URL
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range cases {
|
|
|
|
gotURL, err := CleanRedirectURL(tc.url, tc.hosts)
|
|
|
|
if err != nil {
|
|
|
|
if tc.want != "" {
|
|
|
|
t.Errorf("CleanRedirectURL(%q, %v) got error: %v", tc.url, tc.hosts, err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if got := gotURL.String(); got != tc.want {
|
|
|
|
t.Errorf("CleanRedirectURL(%q, %v) = %q, want %q", tc.url, tc.hosts, got, tc.want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-02-07 00:18:33 +00:00
|
|
|
|
|
|
|
func TestBucket(t *testing.T) {
|
|
|
|
tcs := []struct {
|
|
|
|
path string
|
|
|
|
want string
|
|
|
|
}{
|
|
|
|
{"/map", "/map"},
|
|
|
|
{"/key?v=63", "/key"},
|
|
|
|
{"/map/a87e865a9d1c7", "/map/…"},
|
|
|
|
{"/machine/37fc1acb57f256b69b0d76749d814d91c68b241057c6b127fee3df37e4af111e", "/machine/…"},
|
|
|
|
{"/machine/37fc1acb57f256b69b0d76749d814d91c68b241057c6b127fee3df37e4af111e/map", "/machine/…/map"},
|
2024-02-07 21:15:43 +00:00
|
|
|
{"/api/v2/tailnet/jeremiah@squish.com/devices", "/api/v2/tailnet/…/devices"},
|
|
|
|
{"/machine/ssh/wait/5227109621243650/to/7111899293970143/a/a9e4e04cc01b", "/machine/ssh/wait/…/to/…/a/…"},
|
|
|
|
{"/a/831a4bf39856?refreshed=true", "/a/…"},
|
2024-02-07 00:18:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tcs {
|
|
|
|
t.Run(tc.path, func(t *testing.T) {
|
|
|
|
o := BucketedStatsOptions{}
|
|
|
|
bucket := (&o).bucketForRequest(&http.Request{
|
|
|
|
URL: must.Get(url.Parse(tc.path)),
|
|
|
|
})
|
|
|
|
|
|
|
|
if bucket != tc.want {
|
|
|
|
t.Errorf("bucket for %q was %q, want %q", tc.path, bucket, tc.want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|