Skip to content

Commit

Permalink
Merge pull request #1909 from YaoZengzeng/error-test
Browse files Browse the repository at this point in the history
test: unit test for cri/stream/errors.go
  • Loading branch information
allencloud authored Jul 30, 2018
2 parents 12a4930 + 91a5c86 commit 85df96b
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions cri/stream/errors_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package stream

import (
"fmt"
"net/http"
"net/http/httptest"
"reflect"
"strconv"
"testing"

"google.golang.org/grpc"
"google.golang.org/grpc/codes"
)

func TestWriteError(t *testing.T) {
for _, tt := range []struct {
err error
code int
}{
{
fmt.Errorf("error that not from grpc package"),
http.StatusInternalServerError,
},
{
ErrorStreamingDisabled("methodNotExist"),
http.StatusNotFound,
},
{
ErrorTooManyInFlight(),
http.StatusTooManyRequests,
},
} {
res := httptest.NewRecorder()
WriteError(tt.err, res)
if res.Code != tt.code {
t.Fatalf("unexpected code in http response")
}
if !reflect.DeepEqual([]byte(tt.err.Error()), res.Body.Bytes()) {
t.Fatalf("unexpected content in the body of http response")
}
if grpc.Code(tt.err) == codes.ResourceExhausted {
if res.Header().Get("Retry-After") != strconv.Itoa(int(CacheTTL.Seconds())) {
t.Fatalf("the Retry-After field of http header is not expected when the error is resource exhausted")
}
}
}
}

0 comments on commit 85df96b

Please sign in to comment.