Skip to content

Commit

Permalink
Add cacleall tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
googollee committed Apr 13, 2024
1 parent b62541f commit 255654d
Show file tree
Hide file tree
Showing 5 changed files with 151 additions and 12 deletions.
33 changes: 24 additions & 9 deletions cacheall.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,32 +13,47 @@ func cacheAllError(ctx Context) error {
}
code := http.StatusInternalServerError
defer func() {
perr := recover()
err := checkError(ctx, recover())

if wr.hasWritten || (ctx.Error() == nil && perr == nil) {
if wr.hasWritten || err == nil {
return
}

if httpCoder, ok := ctx.Error().(HTTPError); ok {
if httpCoder, ok := err.(HTTPError); ok {
code = httpCoder.HTTPCode()
}
wr.WriteHeader(code)

if perr == nil {
perr = ctx.Error()
}

codec := codec.Module.Value(ctx)
if codec == nil {
fmt.Fprintf(wr, "%v", perr)
fmt.Fprintf(wr, "%v", err)
return
}

_ = codec.EncodeResponse(ctx, perr)
_ = codec.EncodeResponse(ctx, err)
}()

ctx = ctx.WithResponseWriter(wr)
ctx.Next()

return nil
}

func checkError(ctx Context, perr any) error {
if perr != nil {
return Error(http.StatusInternalServerError, fmt.Errorf("%v", perr))
}

for _, err := range []error{ctx.Err(), ctx.Error()} {
if err == nil {
continue
}

if _, ok := err.(HTTPError); ok {
return err
}
return Error(http.StatusInternalServerError, err)
}

return nil
}
125 changes: 125 additions & 0 deletions cacheall_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
package espresso_test

import (
"errors"
"fmt"
"io"
"net/http"
"net/http/httptest"
"sync/atomic"
"testing"

"github.com/googollee/go-espresso"
"github.com/googollee/go-espresso/codec"
"github.com/googollee/go-espresso/module"
)

func TestCacheAllMiddleware(t *testing.T) {
tests := []struct {
name string
providers []module.Provider
middlewares []espresso.HandleFunc
wantCode int
wantBody string
}{
{
name: "MiddlewareError",
middlewares: []espresso.HandleFunc{func(ctx espresso.Context) error {
return errors.New("error")
}},
wantCode: http.StatusInternalServerError,
wantBody: "error",
},
{
name: "MiddlewareHTTPError",
middlewares: []espresso.HandleFunc{func(ctx espresso.Context) error {
return espresso.Error(http.StatusGatewayTimeout, errors.New("gateway timeout"))
}},
wantCode: http.StatusGatewayTimeout,
wantBody: "gateway timeout",
},
{
name: "MiddlewarePanic",
middlewares: []espresso.HandleFunc{func(ctx espresso.Context) error {
panic("panic")
}},
wantCode: http.StatusInternalServerError,
wantBody: "panic",
},
{
name: "MiddlewareErrorWithCodec",
providers: []module.Provider{codec.Provider},
middlewares: []espresso.HandleFunc{func(ctx espresso.Context) error {
return errors.New("error")
}},
wantCode: http.StatusInternalServerError,
wantBody: "{\"message\":\"error\"}\n",
},
{
name: "MiddlewareHTTPErrorWithCodec",
providers: []module.Provider{codec.Provider},
middlewares: []espresso.HandleFunc{func(ctx espresso.Context) error {
return espresso.Error(http.StatusGatewayTimeout, errors.New("gateway timeout"))
}},
wantCode: http.StatusGatewayTimeout,
wantBody: "{\"message\":\"gateway timeout\"}\n",
},
{
name: "MiddlewarePanicWithCodec",
providers: []module.Provider{codec.Provider},
middlewares: []espresso.HandleFunc{func(ctx espresso.Context) error {
panic("panic")
}},
wantCode: http.StatusInternalServerError,
wantBody: "{\"message\":\"panic\"}\n",
},
}

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
espo := espresso.New()
espo.Use(tc.middlewares...)
espo.AddModule(tc.providers...)

var called int32
espo.HandleFunc(func(ctx espresso.Context) error {
atomic.AddInt32(&called, 1)

if err := ctx.Endpoint(http.MethodGet, "/").End(); err != nil {
return err
}

fmt.Fprint(ctx.ResponseWriter(), "ok")

return nil
})

called = 0
svr := httptest.NewServer(espo)
defer svr.Close()

resp, err := http.Get(svr.URL)
if err != nil {
t.Fatal(err)
}
defer resp.Body.Close()

if got := atomic.LoadInt32(&called); got != 0 {
t.Fatalf("handle func is called")
}

if got, want := resp.StatusCode, tc.wantCode; got != want {
t.Fatalf("resp.Status = %d, want: %d", got, want)
}

respBody, err := io.ReadAll(resp.Body)
if err != nil {
t.Fatal(err)
}

if got, want := string(respBody), tc.wantBody; got != want {
t.Errorf("resp.Body = %q, want: %q", got, want)
}
})
}
}
1 change: 1 addition & 0 deletions examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ func ExampleEspresso() {
}

espo := espresso.New()
espo.AddModule(codec.Provider)

espo.HandleFunc(func(ctx espresso.Context) error {
var id int
Expand Down
2 changes: 0 additions & 2 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package espresso
import (
"net/http"

"github.com/googollee/go-espresso/codec"
"github.com/googollee/go-espresso/module"
)

Expand All @@ -22,7 +21,6 @@ func New() *Espresso {
mux: ret.mux,
}

ret.AddModule(codec.Module.ProvideWithFunc(codec.Default))
ret.Use(cacheAllError)

return ret
Expand Down
2 changes: 1 addition & 1 deletion test.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#!/bin/sh

GODEBUG=httpmuxgo121=0 go test -v -race ./...
GODEBUG=httpmuxgo121=0 go test -v -race -cover ./...

0 comments on commit 255654d

Please sign in to comment.