From b0dcf1e78769006ce8154389476d45aa3fb4d654 Mon Sep 17 00:00:00 2001 From: Rasmus Dahlberg Date: Wed, 11 Nov 2020 15:57:37 +0100 Subject: added test that checks for a context deadline --- handler_test.go | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) (limited to 'handler_test.go') diff --git a/handler_test.go b/handler_test.go index 2d2a184..6a2e7d8 100644 --- a/handler_test.go +++ b/handler_test.go @@ -2,6 +2,7 @@ package stfe import ( "bytes" + "context" "crypto" "fmt" "strings" @@ -199,7 +200,7 @@ func TestGetSth(t *testing.T) { } w := httptest.NewRecorder() - th.client.EXPECT().GetLatestSignedLogRoot(gomock.Any(), gomock.Any()).Return(table.trsp, table.terr) + th.client.EXPECT().GetLatestSignedLogRoot(newDeadlineMatcher(), gomock.Any()).Return(table.trsp, table.terr) th.getHandler(t, "get-sth").ServeHTTP(w, req) if w.Code != table.wantCode { t.Errorf("GET(%s)=%d, want http status code %d", url, w.Code, table.wantCode) @@ -273,3 +274,27 @@ func mustMarshalRoot(t *testing.T, lr *types.LogRootV1) *trillian.SignedLogRoot } return &trillian.SignedLogRoot{LogRoot: rootBytes} } + +// deadlineMatcher implements gomock.Matcher, such that an error is detected if +// there is no context.Context deadline set +type deadlineMatcher struct{} + +// newDeadlineMatcher returns a new deadlineMatcher +func newDeadlineMatcher() gomock.Matcher { + return &deadlineMatcher{} +} + +// Matches returns true if the passed interface is a context with a deadline +func (dm *deadlineMatcher) Matches(i interface{}) bool { + ctx, ok := i.(context.Context) + if !ok { + return false + } + _, ok = ctx.Deadline() + return ok +} + +// String is needed to implement gomock.Matcher +func (dm *deadlineMatcher) String() string { + return fmt.Sprintf("deadlineMatcher{}") +} -- cgit v1.2.3