fix check for empty array result (II)

This commit is contained in:
Sergio Moura 2023-09-11 16:37:55 -04:00
parent 2b3912c750
commit eaa837d17d

View File

@ -5,6 +5,7 @@ import (
"context" "context"
"encoding/json" "encoding/json"
"github.com/ddworken/hishtory/internal/database" "github.com/ddworken/hishtory/internal/database"
"github.com/stretchr/testify/require"
"io" "io"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
@ -187,9 +188,8 @@ func TestDumpRequestAndResponse(t *testing.T) {
defer res.Body.Close() defer res.Body.Close()
respBody, err = io.ReadAll(res.Body) respBody, err = io.ReadAll(res.Body)
testutils.Check(t, err) testutils.Check(t, err)
if string(respBody) != "[]" { resp := strings.TrimSpace(string(respBody))
t.Fatalf("got unexpected respBody: %#v", string(respBody)) require.Equalf(t, "[]", resp, "got unexpected respBody: %#v", string(resp))
}
// And none for a missing user ID // And none for a missing user ID
w = httptest.NewRecorder() w = httptest.NewRecorder()
@ -198,9 +198,8 @@ func TestDumpRequestAndResponse(t *testing.T) {
defer res.Body.Close() defer res.Body.Close()
respBody, err = io.ReadAll(res.Body) respBody, err = io.ReadAll(res.Body)
testutils.Check(t, err) testutils.Check(t, err)
if string(respBody) != "[]" { resp = strings.TrimSpace(string(respBody))
t.Fatalf("got unexpected respBody: %#v", string(respBody)) require.Equalf(t, "[]", resp, "got unexpected respBody: %#v", string(resp))
}
// Now submit a dump for userId // Now submit a dump for userId
entry1Dec := testutils.MakeFakeHistoryEntry("ls ~/") entry1Dec := testutils.MakeFakeHistoryEntry("ls ~/")
@ -221,19 +220,19 @@ func TestDumpRequestAndResponse(t *testing.T) {
defer res.Body.Close() defer res.Body.Close()
respBody, err = io.ReadAll(res.Body) respBody, err = io.ReadAll(res.Body)
testutils.Check(t, err) testutils.Check(t, err)
if string(respBody) != "[]" { resp = strings.TrimSpace(string(respBody))
t.Fatalf("got unexpected respBody: %#v", string(respBody)) require.Equalf(t, "[]", resp, "got unexpected respBody: %#v", string(respBody))
}
w = httptest.NewRecorder() w = httptest.NewRecorder()
// The other user // The other user
apiGetPendingDumpRequestsHandler(w, httptest.NewRequest(http.MethodGet, "/?user_id="+userId+"&device_id="+devId2, nil)) apiGetPendingDumpRequestsHandler(w, httptest.NewRequest(http.MethodGet, "/?user_id="+userId+"&device_id="+devId2, nil))
res = w.Result() res = w.Result()
defer res.Body.Close() defer res.Body.Close()
respBody, err = io.ReadAll(res.Body) respBody, err = io.ReadAll(res.Body)
testutils.Check(t, err) testutils.Check(t, err)
if string(respBody) != "[]" { resp = strings.TrimSpace(string(respBody))
t.Fatalf("got unexpected respBody: %#v", string(respBody)) require.Equalf(t, "[]", resp, "got unexpected respBody: %#v", string(respBody))
}
// But it is there for the other user // But it is there for the other user
w = httptest.NewRecorder() w = httptest.NewRecorder()