Removed assert dependency from some tests

This commit is contained in:
Maxim Lebedev 2022-02-02 00:32:57 +05:00
parent d5448c3766
commit 8655ce36bf
Signed by: toby3d
GPG Key ID: 1F14E25B7C119FC5
5 changed files with 48 additions and 22 deletions

View File

@ -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)
}
}

View File

@ -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)
}
}

View File

@ -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)
}
}

View File

@ -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)
}
}

View File

@ -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)
}
}