2020-06-05 05:10:50 +01:00
|
|
|
// Copyright (c) 2020 Tailscale Inc & AUTHORS All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package tsweb
|
|
|
|
|
|
|
|
import (
|
2022-02-07 18:01:23 +00:00
|
|
|
"bytes"
|
|
|
|
"compress/gzip"
|
2020-06-05 05:10:50 +01:00
|
|
|
"encoding/json"
|
2020-08-18 22:37:01 +01:00
|
|
|
"fmt"
|
2022-02-07 18:01:23 +00:00
|
|
|
"io"
|
2020-06-05 05:10:50 +01:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
2020-11-10 14:52:26 +00:00
|
|
|
|
|
|
|
"github.com/google/go-cmp/cmp"
|
2020-06-05 05:10:50 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type Data struct {
|
|
|
|
Name string
|
|
|
|
Price int
|
|
|
|
}
|
|
|
|
|
|
|
|
type Response struct {
|
|
|
|
Status string
|
|
|
|
Error string
|
|
|
|
Data *Data
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewJSONHandler(t *testing.T) {
|
2022-02-07 18:01:23 +00:00
|
|
|
checkStatus := func(t *testing.T, w *httptest.ResponseRecorder, status string, code int) *Response {
|
2020-06-05 05:10:50 +01:00
|
|
|
d := &Response{
|
|
|
|
Data: &Data{},
|
|
|
|
}
|
|
|
|
|
2022-02-07 18:01:23 +00:00
|
|
|
bodyBytes := w.Body.Bytes()
|
|
|
|
if w.Result().Header.Get("Content-Encoding") == "gzip" {
|
|
|
|
zr, err := gzip.NewReader(bytes.NewReader(bodyBytes))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("gzip read error at start: %v", err)
|
|
|
|
}
|
|
|
|
bodyBytes, err = io.ReadAll(zr)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("gzip read error: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Logf("%s", bodyBytes)
|
|
|
|
err := json.Unmarshal(bodyBytes, d)
|
2020-06-05 05:10:50 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Logf(err.Error())
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if d.Status == status {
|
|
|
|
t.Logf("ok: %s", d.Status)
|
|
|
|
} else {
|
2020-11-10 14:52:26 +00:00
|
|
|
t.Fatalf("wrong status: got: %s, want: %s", d.Status, status)
|
2020-06-05 05:10:50 +01:00
|
|
|
}
|
|
|
|
|
2020-08-18 22:37:01 +01:00
|
|
|
if w.Code != code {
|
2020-11-10 14:52:26 +00:00
|
|
|
t.Fatalf("wrong status code: got: %d, want: %d", w.Code, code)
|
2020-08-18 22:37:01 +01:00
|
|
|
}
|
|
|
|
|
2020-07-14 20:27:26 +01:00
|
|
|
if w.Header().Get("Content-Type") != "application/json" {
|
|
|
|
t.Fatalf("wrong content type: %s", w.Header().Get("Content-Type"))
|
|
|
|
}
|
|
|
|
|
2020-06-05 05:10:50 +01:00
|
|
|
return d
|
|
|
|
}
|
|
|
|
|
2022-03-16 23:27:57 +00:00
|
|
|
h21 := JSONHandlerFunc(func(r *http.Request) (int, any, error) {
|
2020-08-18 22:37:01 +01:00
|
|
|
return http.StatusOK, nil, nil
|
2020-06-05 05:10:50 +01:00
|
|
|
})
|
|
|
|
|
2020-08-18 22:37:01 +01:00
|
|
|
t.Run("200 simple", func(t *testing.T) {
|
2020-06-05 05:10:50 +01:00
|
|
|
w := httptest.NewRecorder()
|
|
|
|
r := httptest.NewRequest("GET", "/", nil)
|
2020-09-17 13:56:14 +01:00
|
|
|
h21.ServeHTTPReturn(w, r)
|
2022-02-07 18:01:23 +00:00
|
|
|
checkStatus(t, w, "success", http.StatusOK)
|
2020-06-05 05:10:50 +01:00
|
|
|
})
|
|
|
|
|
2020-08-18 22:37:01 +01:00
|
|
|
t.Run("403 HTTPError", func(t *testing.T) {
|
2022-03-16 23:27:57 +00:00
|
|
|
h := JSONHandlerFunc(func(r *http.Request) (int, any, error) {
|
2020-11-10 14:52:26 +00:00
|
|
|
return 0, nil, Error(http.StatusForbidden, "forbidden", nil)
|
2020-06-09 22:40:45 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
r := httptest.NewRequest("GET", "/", nil)
|
2020-09-17 13:56:14 +01:00
|
|
|
h.ServeHTTPReturn(w, r)
|
2022-02-07 18:01:23 +00:00
|
|
|
checkStatus(t, w, "error", http.StatusForbidden)
|
2020-06-09 22:40:45 +01:00
|
|
|
})
|
|
|
|
|
2022-03-16 23:27:57 +00:00
|
|
|
h22 := JSONHandlerFunc(func(r *http.Request) (int, any, error) {
|
2020-08-18 22:37:01 +01:00
|
|
|
return http.StatusOK, &Data{Name: "tailscale"}, nil
|
2020-06-05 05:10:50 +01:00
|
|
|
})
|
2020-08-18 22:37:01 +01:00
|
|
|
|
|
|
|
t.Run("200 get data", func(t *testing.T) {
|
2020-06-05 05:10:50 +01:00
|
|
|
w := httptest.NewRecorder()
|
|
|
|
r := httptest.NewRequest("GET", "/", nil)
|
2020-09-17 13:56:14 +01:00
|
|
|
h22.ServeHTTPReturn(w, r)
|
2022-02-07 18:01:23 +00:00
|
|
|
checkStatus(t, w, "success", http.StatusOK)
|
2020-06-05 05:10:50 +01:00
|
|
|
})
|
|
|
|
|
2022-03-16 23:27:57 +00:00
|
|
|
h31 := JSONHandlerFunc(func(r *http.Request) (int, any, error) {
|
2020-08-18 22:37:01 +01:00
|
|
|
body := new(Data)
|
|
|
|
if err := json.NewDecoder(r.Body).Decode(body); err != nil {
|
2020-11-10 14:52:26 +00:00
|
|
|
return 0, nil, Error(http.StatusBadRequest, err.Error(), err)
|
2020-06-05 05:10:50 +01:00
|
|
|
}
|
|
|
|
|
2020-08-18 22:37:01 +01:00
|
|
|
if body.Name == "" {
|
2020-11-10 14:52:26 +00:00
|
|
|
return 0, nil, Error(http.StatusBadRequest, "name is empty", nil)
|
2020-08-18 22:37:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return http.StatusOK, nil, nil
|
2020-06-05 05:10:50 +01:00
|
|
|
})
|
2020-08-18 22:37:01 +01:00
|
|
|
t.Run("200 post data", func(t *testing.T) {
|
2020-06-05 05:10:50 +01:00
|
|
|
w := httptest.NewRecorder()
|
|
|
|
r := httptest.NewRequest("POST", "/", strings.NewReader(`{"Name": "tailscale"}`))
|
2020-09-17 13:56:14 +01:00
|
|
|
h31.ServeHTTPReturn(w, r)
|
2022-02-07 18:01:23 +00:00
|
|
|
checkStatus(t, w, "success", http.StatusOK)
|
2020-06-05 05:10:50 +01:00
|
|
|
})
|
|
|
|
|
2020-08-18 22:37:01 +01:00
|
|
|
t.Run("400 bad json", func(t *testing.T) {
|
2020-06-05 05:10:50 +01:00
|
|
|
w := httptest.NewRecorder()
|
|
|
|
r := httptest.NewRequest("POST", "/", strings.NewReader(`{`))
|
2020-09-17 13:56:14 +01:00
|
|
|
h31.ServeHTTPReturn(w, r)
|
2022-02-07 18:01:23 +00:00
|
|
|
checkStatus(t, w, "error", http.StatusBadRequest)
|
2020-06-05 05:10:50 +01:00
|
|
|
})
|
|
|
|
|
2020-08-18 22:37:01 +01:00
|
|
|
t.Run("400 post data error", func(t *testing.T) {
|
2020-06-05 05:10:50 +01:00
|
|
|
w := httptest.NewRecorder()
|
|
|
|
r := httptest.NewRequest("POST", "/", strings.NewReader(`{}`))
|
2020-09-17 13:56:14 +01:00
|
|
|
h31.ServeHTTPReturn(w, r)
|
2022-02-07 18:01:23 +00:00
|
|
|
resp := checkStatus(t, w, "error", http.StatusBadRequest)
|
2020-06-05 05:10:50 +01:00
|
|
|
if resp.Error != "name is empty" {
|
|
|
|
t.Fatalf("wrong error")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2022-03-16 23:27:57 +00:00
|
|
|
h32 := JSONHandlerFunc(func(r *http.Request) (int, any, error) {
|
2020-08-18 22:37:01 +01:00
|
|
|
body := new(Data)
|
|
|
|
if err := json.NewDecoder(r.Body).Decode(body); err != nil {
|
2020-11-10 14:52:26 +00:00
|
|
|
return 0, nil, Error(http.StatusBadRequest, err.Error(), err)
|
2020-08-18 22:37:01 +01:00
|
|
|
}
|
|
|
|
if body.Name == "root" {
|
2020-11-10 14:52:26 +00:00
|
|
|
return 0, nil, fmt.Errorf("invalid name")
|
2020-08-18 22:37:01 +01:00
|
|
|
}
|
|
|
|
if body.Price == 0 {
|
2020-11-10 14:52:26 +00:00
|
|
|
return 0, nil, Error(http.StatusBadRequest, "price is empty", nil)
|
2020-06-05 05:10:50 +01:00
|
|
|
}
|
|
|
|
|
2020-08-18 22:37:01 +01:00
|
|
|
return http.StatusOK, &Data{Price: body.Price * 2}, nil
|
2020-06-05 05:10:50 +01:00
|
|
|
})
|
2020-08-18 22:37:01 +01:00
|
|
|
|
|
|
|
t.Run("200 post data", func(t *testing.T) {
|
2020-06-05 05:10:50 +01:00
|
|
|
w := httptest.NewRecorder()
|
|
|
|
r := httptest.NewRequest("POST", "/", strings.NewReader(`{"Price": 10}`))
|
2020-09-17 13:56:14 +01:00
|
|
|
h32.ServeHTTPReturn(w, r)
|
2022-02-07 18:01:23 +00:00
|
|
|
resp := checkStatus(t, w, "success", http.StatusOK)
|
|
|
|
t.Log(resp.Data)
|
|
|
|
if resp.Data.Price != 20 {
|
|
|
|
t.Fatalf("wrong price: %d %d", resp.Data.Price, 10)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("gzipped", func(t *testing.T) {
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
r := httptest.NewRequest("POST", "/", strings.NewReader(`{"Price": 10}`))
|
|
|
|
r.Header.Set("Accept-Encoding", "gzip")
|
|
|
|
h32.ServeHTTPReturn(w, r)
|
|
|
|
res := w.Result()
|
|
|
|
if ct := res.Header.Get("Content-Encoding"); ct != "gzip" {
|
|
|
|
t.Fatalf("encoding = %q; want gzip", ct)
|
|
|
|
}
|
|
|
|
resp := checkStatus(t, w, "success", http.StatusOK)
|
2020-06-05 05:10:50 +01:00
|
|
|
t.Log(resp.Data)
|
|
|
|
if resp.Data.Price != 20 {
|
|
|
|
t.Fatalf("wrong price: %d %d", resp.Data.Price, 10)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2022-02-09 20:06:27 +00:00
|
|
|
t.Run("gzipped_400", func(t *testing.T) {
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
r := httptest.NewRequest("POST", "/", strings.NewReader(`{"Price": 10}`))
|
|
|
|
r.Header.Set("Accept-Encoding", "gzip")
|
|
|
|
value := []string{"foo", "foo", "foo"}
|
2022-03-16 23:27:57 +00:00
|
|
|
JSONHandlerFunc(func(r *http.Request) (int, any, error) {
|
2022-02-09 20:06:27 +00:00
|
|
|
return 400, value, nil
|
|
|
|
}).ServeHTTPReturn(w, r)
|
|
|
|
res := w.Result()
|
|
|
|
if ct := res.Header.Get("Content-Encoding"); ct != "gzip" {
|
|
|
|
t.Fatalf("encoding = %q; want gzip", ct)
|
|
|
|
}
|
|
|
|
if res.StatusCode != 400 {
|
|
|
|
t.Errorf("Status = %v; want 400", res.StatusCode)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2020-08-18 22:37:01 +01:00
|
|
|
t.Run("400 post data error", func(t *testing.T) {
|
2020-06-05 05:10:50 +01:00
|
|
|
w := httptest.NewRecorder()
|
|
|
|
r := httptest.NewRequest("POST", "/", strings.NewReader(`{}`))
|
2020-09-17 13:56:14 +01:00
|
|
|
h32.ServeHTTPReturn(w, r)
|
2022-02-07 18:01:23 +00:00
|
|
|
resp := checkStatus(t, w, "error", http.StatusBadRequest)
|
2020-06-05 05:10:50 +01:00
|
|
|
if resp.Error != "price is empty" {
|
|
|
|
t.Fatalf("wrong error")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2020-11-10 14:52:26 +00:00
|
|
|
t.Run("500 internal server error (unspecified error, not of type HTTPError)", func(t *testing.T) {
|
2020-08-18 22:37:01 +01:00
|
|
|
w := httptest.NewRecorder()
|
|
|
|
r := httptest.NewRequest("POST", "/", strings.NewReader(`{"Name": "root"}`))
|
2020-09-17 13:56:14 +01:00
|
|
|
h32.ServeHTTPReturn(w, r)
|
2022-02-07 18:01:23 +00:00
|
|
|
resp := checkStatus(t, w, "error", http.StatusInternalServerError)
|
2020-08-18 22:37:01 +01:00
|
|
|
if resp.Error != "internal server error" {
|
|
|
|
t.Fatalf("wrong error")
|
2020-06-05 05:10:50 +01:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2020-08-18 22:37:01 +01:00
|
|
|
t.Run("500 misuse", func(t *testing.T) {
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
r := httptest.NewRequest("POST", "/", nil)
|
2022-03-16 23:27:57 +00:00
|
|
|
JSONHandlerFunc(func(r *http.Request) (int, any, error) {
|
2020-08-18 22:37:01 +01:00
|
|
|
return http.StatusOK, make(chan int), nil
|
2020-09-17 13:56:14 +01:00
|
|
|
}).ServeHTTPReturn(w, r)
|
2022-02-07 18:01:23 +00:00
|
|
|
resp := checkStatus(t, w, "error", http.StatusInternalServerError)
|
2020-08-18 22:37:01 +01:00
|
|
|
if resp.Error != "json marshal error" {
|
|
|
|
t.Fatalf("wrong error")
|
|
|
|
}
|
2020-06-05 05:10:50 +01:00
|
|
|
})
|
2020-06-08 20:48:38 +01:00
|
|
|
|
2020-08-18 22:37:01 +01:00
|
|
|
t.Run("500 empty status code", func(t *testing.T) {
|
2020-06-08 20:48:38 +01:00
|
|
|
w := httptest.NewRecorder()
|
2020-08-18 22:37:01 +01:00
|
|
|
r := httptest.NewRequest("POST", "/", nil)
|
2022-03-16 23:27:57 +00:00
|
|
|
JSONHandlerFunc(func(r *http.Request) (status int, data any, err error) {
|
2020-08-18 22:37:01 +01:00
|
|
|
return
|
2020-09-17 13:56:14 +01:00
|
|
|
}).ServeHTTPReturn(w, r)
|
2022-02-07 18:01:23 +00:00
|
|
|
checkStatus(t, w, "error", http.StatusInternalServerError)
|
2020-06-08 20:48:38 +01:00
|
|
|
})
|
2020-11-10 14:52:26 +00:00
|
|
|
|
|
|
|
t.Run("403 forbidden, status returned by JSONHandlerFunc and HTTPError agree", func(t *testing.T) {
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
r := httptest.NewRequest("POST", "/", nil)
|
2022-03-16 23:27:57 +00:00
|
|
|
JSONHandlerFunc(func(r *http.Request) (int, any, error) {
|
2020-11-10 14:52:26 +00:00
|
|
|
return http.StatusForbidden, nil, Error(http.StatusForbidden, "403 forbidden", nil)
|
|
|
|
}).ServeHTTPReturn(w, r)
|
|
|
|
want := &Response{
|
|
|
|
Status: "error",
|
|
|
|
Data: &Data{},
|
|
|
|
Error: "403 forbidden",
|
|
|
|
}
|
2022-02-07 18:01:23 +00:00
|
|
|
got := checkStatus(t, w, "error", http.StatusForbidden)
|
2020-11-10 14:52:26 +00:00
|
|
|
if diff := cmp.Diff(want, got); diff != "" {
|
|
|
|
t.Fatalf(diff)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("403 forbidden, status returned by JSONHandlerFunc and HTTPError do not agree", func(t *testing.T) {
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
r := httptest.NewRequest("POST", "/", nil)
|
2022-03-16 23:27:57 +00:00
|
|
|
err := JSONHandlerFunc(func(r *http.Request) (int, any, error) {
|
2020-11-10 14:52:26 +00:00
|
|
|
return http.StatusInternalServerError, nil, Error(http.StatusForbidden, "403 forbidden", nil)
|
|
|
|
}).ServeHTTPReturn(w, r)
|
|
|
|
if !strings.HasPrefix(err.Error(), "[unexpected]") {
|
|
|
|
t.Fatalf("returned error should have `[unexpected]` to note the disagreeing status codes: %v", err)
|
|
|
|
}
|
|
|
|
want := &Response{
|
|
|
|
Status: "error",
|
|
|
|
Data: &Data{},
|
|
|
|
Error: "403 forbidden",
|
|
|
|
}
|
2022-02-07 18:01:23 +00:00
|
|
|
got := checkStatus(t, w, "error", http.StatusForbidden)
|
2020-11-10 14:52:26 +00:00
|
|
|
if diff := cmp.Diff(want, got); diff != "" {
|
|
|
|
t.Fatalf("(-want,+got):\n%s", diff)
|
|
|
|
}
|
|
|
|
})
|
2020-06-05 05:10:50 +01:00
|
|
|
}
|
2022-02-07 18:01:23 +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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|