-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
151 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ./... |