From 8655ce36bf0f88c14e9b9b56bff8c836dd393511 Mon Sep 17 00:00:00 2001 From: Maxim Lebedev Date: Wed, 2 Feb 2022 00:32:57 +0500 Subject: [PATCH] :heavy_minus_sign: Removed assert dependency from some tests --- .../health/delivery/http/health_http_test.go | 15 +++++-- .../ticket/delivery/http/ticket_http_test.go | 5 ++- internal/ticket/usecase/ticket_ucase_test.go | 7 ++-- .../token/delivery/http/token_http_test.go | 39 +++++++++++++------ internal/token/usecase/token_ucase_test.go | 4 +- 5 files changed, 48 insertions(+), 22 deletions(-) diff --git a/internal/health/delivery/http/health_http_test.go b/internal/health/delivery/http/health_http_test.go index da929d1..82b4203 100644 --- a/internal/health/delivery/http/health_http_test.go +++ b/internal/health/delivery/http/health_http_test.go @@ -4,7 +4,6 @@ import ( "testing" "github.com/fasthttp/router" - "github.com/stretchr/testify/assert" http "github.com/valyala/fasthttp" delivery "source.toby3d.me/website/indieauth/internal/health/delivery/http" @@ -20,7 +19,9 @@ func TestRequestHandler(t *testing.T) { client, _, cleanup := httptest.New(t, r.Handler) t.Cleanup(cleanup) - req := httptest.NewRequest(http.MethodGet, "https://app.example.com/health", nil) + const requestURL = "https://app.example.com/health" + + req := httptest.NewRequest(http.MethodGet, requestURL, nil) defer http.ReleaseRequest(req) resp := http.AcquireResponse() @@ -30,6 +31,12 @@ func TestRequestHandler(t *testing.T) { t.Fatal(err) } - assert.Equal(t, http.StatusOK, resp.StatusCode()) - assert.Equal(t, `{"ok": true}`, string(resp.Body())) + if result := resp.StatusCode(); result != http.StatusOK { + t.Errorf("GET %s = %d, want %d", requestURL, result, http.StatusOK) + } + + const expBody = `{"ok": true}` + if result := string(resp.Body()); result != expBody { + t.Errorf("GET %s = %s, want %s", requestURL, result, expBody) + } } diff --git a/internal/ticket/delivery/http/ticket_http_test.go b/internal/ticket/delivery/http/ticket_http_test.go index d152470..c119178 100644 --- a/internal/ticket/delivery/http/ticket_http_test.go +++ b/internal/ticket/delivery/http/ticket_http_test.go @@ -5,7 +5,6 @@ import ( "testing" "github.com/fasthttp/router" - "github.com/stretchr/testify/assert" http "github.com/valyala/fasthttp" "golang.org/x/text/language" "golang.org/x/text/message" @@ -80,5 +79,7 @@ func TestUpdate(t *testing.T) { // TODO(toby3d): print the result as part of the debugging. Instead, you // need to send or save the token to the recipient for later use. - assert.NotNil(t, resp.Body()) + if resp.Body() == nil { + t.Errorf("POST %s = nil, want something", requestURI) + } } diff --git a/internal/ticket/usecase/ticket_ucase_test.go b/internal/ticket/usecase/ticket_ucase_test.go index bf6cceb..de7aa04 100644 --- a/internal/ticket/usecase/ticket_ucase_test.go +++ b/internal/ticket/usecase/ticket_ucase_test.go @@ -6,7 +6,6 @@ import ( "testing" "github.com/fasthttp/router" - "github.com/stretchr/testify/assert" http "github.com/valyala/fasthttp" "source.toby3d.me/website/indieauth/internal/common" @@ -44,7 +43,7 @@ func TestRedeem(t *testing.T) { t.Fatal(err) } - assert.Equal(t, token.AccessToken, result.AccessToken) - assert.Equal(t, token.Me.String(), result.Me.String()) - assert.Equal(t, token.Scope, result.Scope) + if result.String() != token.String() { + t.Errorf("Redeem(%+v) = %s, want %s", ticket, result, token) + } } diff --git a/internal/token/delivery/http/token_http_test.go b/internal/token/delivery/http/token_http_test.go index 636d232..07b5df3 100644 --- a/internal/token/delivery/http/token_http_test.go +++ b/internal/token/delivery/http/token_http_test.go @@ -1,14 +1,13 @@ package http_test import ( + "bytes" "context" - "strings" "sync" "testing" "github.com/fasthttp/router" json "github.com/goccy/go-json" - "github.com/stretchr/testify/assert" http "github.com/valyala/fasthttp" "source.toby3d.me/website/indieauth/internal/common" @@ -53,7 +52,9 @@ func TestVerification(t *testing.T) { client, _, cleanup := httptest.New(t, router.Handler) t.Cleanup(cleanup) - req := httptest.NewRequest(http.MethodGet, "https://app.example.com/token", nil) + const requestURL = "https://app.example.com/token" + + req := httptest.NewRequest(http.MethodGet, requestURL, nil) defer http.ReleaseRequest(req) req.Header.Set(http.HeaderAccept, common.MIMEApplicationJSON) token.SetAuthHeader(req) @@ -65,16 +66,22 @@ func TestVerification(t *testing.T) { t.Fatal(err) } - assert.Equal(t, http.StatusOK, resp.StatusCode()) + if result := resp.StatusCode(); result != http.StatusOK { + t.Errorf("GET %s = %d, want %d", requestURL, result, http.StatusOK) + } result := new(delivery.TokenVerificationResponse) if err := json.Unmarshal(resp.Body(), result); err != nil { t.Fatal(err) } - assert.Equal(t, token.ClientID.String(), result.ClientID.String()) - assert.Equal(t, token.Me.String(), result.Me.String()) - assert.Equal(t, token.Scope.String(), result.Scope.String()) + token.AccessToken = "" + + if result.ClientID.String() != token.ClientID.String() || + result.Me.String() != token.Me.String() || + result.Scope.String() != token.Scope.String() { + t.Errorf("GET %s = %+v, want %+v", requestURL, result, token) + } } func TestRevocation(t *testing.T) { @@ -102,7 +109,9 @@ func TestRevocation(t *testing.T) { client, _, cleanup := httptest.New(t, router.Handler) t.Cleanup(cleanup) - req := httptest.NewRequest(http.MethodPost, "https://app.example.com/token", nil) + const requestURL = "https://app.example.com/token" + + req := httptest.NewRequest(http.MethodPost, requestURL, nil) defer http.ReleaseRequest(req) req.Header.Set(http.HeaderAccept, common.MIMEApplicationJSON) req.Header.SetContentType(common.MIMEApplicationForm) @@ -116,13 +125,21 @@ func TestRevocation(t *testing.T) { t.Fatal(err) } - assert.Equal(t, http.StatusOK, resp.StatusCode()) - assert.Equal(t, `{}`, strings.TrimSpace(string(resp.Body()))) + if result := resp.StatusCode(); result != http.StatusOK { + t.Errorf("POST %s = %d, want %d", requestURL, result, http.StatusOK) + } + + expBody := []byte("{}") //nolint: ifshort + if result := bytes.TrimSpace(resp.Body()); !bytes.Equal(result, expBody) { + t.Errorf("POST %s = %s, want %s", requestURL, result, expBody) + } result, err := tokens.Get(context.TODO(), accessToken.AccessToken) if err != nil { t.Fatal(err) } - assert.Equal(t, accessToken.AccessToken, result.AccessToken) + if result.String() != accessToken.String() { + t.Errorf("Get(%+v) = %s, want %s", accessToken.AccessToken, result, accessToken) + } } diff --git a/internal/token/usecase/token_ucase_test.go b/internal/token/usecase/token_ucase_test.go index 1ba51c5..512110d 100644 --- a/internal/token/usecase/token_ucase_test.go +++ b/internal/token/usecase/token_ucase_test.go @@ -78,5 +78,7 @@ func TestRevoke(t *testing.T) { t.Error(err) } - assert.Equal(t, accessToken.AccessToken, result.AccessToken) + if result.AccessToken != accessToken.AccessToken { + t.Errorf("Get(%s) = %s, want %s", accessToken.AccessToken, result.AccessToken, accessToken.AccessToken) + } }