begin noodling on a little module of common idioms I use in personal projects
This commit is contained in:
42
http/http.go
Normal file
42
http/http.go
Normal file
@@ -0,0 +1,42 @@
|
||||
package grabhttp
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"io/fs"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type errorStruct struct {
|
||||
Status string `json:"status"`
|
||||
Error string `json:"error"`
|
||||
}
|
||||
|
||||
func StaticHandler(f fs.FS) http.Handler {
|
||||
return http.FileServer(http.FS(f))
|
||||
}
|
||||
|
||||
func WriteJSON(i interface{}, status int, w http.ResponseWriter, r *http.Request) {
|
||||
b, err := json.Marshal(i)
|
||||
if err != nil {
|
||||
ErrorJSON(err, http.StatusInternalServerError, w, r)
|
||||
return
|
||||
}
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(status)
|
||||
w.Write(b)
|
||||
w.Write([]byte("\n"))
|
||||
}
|
||||
|
||||
func WriteHTML(b []byte, status int, w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "text/html")
|
||||
w.WriteHeader(status)
|
||||
w.Write(b)
|
||||
w.Write([]byte("\n"))
|
||||
}
|
||||
|
||||
func ErrorJSON(err error, status int, w http.ResponseWriter, r *http.Request) {
|
||||
WriteJSON(errorStruct{
|
||||
Status: "error",
|
||||
Error: err.Error(),
|
||||
}, status, w, r)
|
||||
}
|
70
http/http_test.go
Normal file
70
http/http_test.go
Normal file
@@ -0,0 +1,70 @@
|
||||
package grabhttp
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestWriteJSON(t *testing.T) {
|
||||
testcases := []struct {
|
||||
input interface{}
|
||||
code int
|
||||
outcode int
|
||||
output []byte
|
||||
}{
|
||||
{
|
||||
input: struct{}{},
|
||||
code: http.StatusOK,
|
||||
output: []byte("{}\n"),
|
||||
},
|
||||
{
|
||||
input: struct{}{},
|
||||
code: http.StatusNotFound,
|
||||
output: []byte("{}\n"),
|
||||
},
|
||||
{
|
||||
input: struct {
|
||||
Foo string
|
||||
}{
|
||||
Foo: "foo",
|
||||
},
|
||||
code: http.StatusOK,
|
||||
output: []byte(`{"Foo":"foo"}` + "\n"),
|
||||
},
|
||||
{
|
||||
input: struct {
|
||||
Foo struct {
|
||||
Bar string `json:"quuz"`
|
||||
}
|
||||
}{
|
||||
Foo: struct {
|
||||
Bar string `json:"quuz"`
|
||||
}{Bar: "foo"},
|
||||
},
|
||||
code: http.StatusOK,
|
||||
output: []byte(`{"Foo":{"quuz":"foo"}}` + "\n"),
|
||||
},
|
||||
{
|
||||
input: struct{ C func() }{C: func() {}},
|
||||
code: http.StatusOK,
|
||||
outcode: http.StatusInternalServerError,
|
||||
output: []byte(`{"status":"error","error":"json: unsupported type: func()"}` + "\n"),
|
||||
},
|
||||
}
|
||||
for i, c := range testcases {
|
||||
w := httptest.NewRecorder()
|
||||
WriteJSON(c.input, c.code, w, httptest.NewRequest("", "/", nil))
|
||||
if w.Code != c.code && w.Code != c.outcode {
|
||||
t.Logf("code mismatch on case %d", i)
|
||||
t.Fail()
|
||||
}
|
||||
b, err := io.ReadAll(w.Body)
|
||||
if err != nil || !bytes.Equal(b, c.output) {
|
||||
t.Logf("failed: %s != %s", b, c.output)
|
||||
t.Fail()
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user