mirror of
https://github.com/tailscale/tailscale.git
synced 2025-01-07 08:07:42 +00:00
tsweb: fix JSONHandler nil response
Signed-off-by: Zijie Lu <zijie@tailscale.com>
This commit is contained in:
parent
c3f7733f53
commit
5e1ee4be53
@ -113,7 +113,9 @@ func JSONHandler(fn interface{}) http.Handler {
|
|||||||
}
|
}
|
||||||
case 2:
|
case 2:
|
||||||
if vs[1].IsNil() {
|
if vs[1].IsNil() {
|
||||||
writeResponse(w, http.StatusOK, responseSuccess(vs[0].Interface()))
|
if !vs[0].IsNil() {
|
||||||
|
writeResponse(w, http.StatusOK, responseSuccess(vs[0].Interface()))
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
err := vs[1].Interface().(error)
|
err := vs[1].Interface().(error)
|
||||||
writeResponse(w, http.StatusBadRequest, responseError(err.Error()))
|
writeResponse(w, http.StatusBadRequest, responseError(err.Error()))
|
||||||
|
@ -5,6 +5,7 @@
|
|||||||
package tsweb
|
package tsweb
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"net/http"
|
"net/http"
|
||||||
@ -172,4 +173,24 @@ func TestNewJSONHandler(t *testing.T) {
|
|||||||
return nil, "panic"
|
return nil, "panic"
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
t.Run("2 2 forbidden", func(t *testing.T) {
|
||||||
|
code := http.StatusForbidden
|
||||||
|
body := []byte("forbidden")
|
||||||
|
h := JSONHandler(func(w http.ResponseWriter, r *http.Request) (*Data, error) {
|
||||||
|
w.WriteHeader(code)
|
||||||
|
w.Write(body)
|
||||||
|
return nil, nil
|
||||||
|
})
|
||||||
|
|
||||||
|
w := httptest.NewRecorder()
|
||||||
|
r := httptest.NewRequest("GET", "/", nil)
|
||||||
|
h.ServeHTTP(w, r)
|
||||||
|
if w.Code != http.StatusForbidden {
|
||||||
|
t.Fatalf("wrong code: %d %d", w.Code, code)
|
||||||
|
}
|
||||||
|
if !bytes.Equal(w.Body.Bytes(), []byte("forbidden")) {
|
||||||
|
t.Fatalf("wrong body: %s %s", w.Body.Bytes(), body)
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user