-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(allsvr): panic trying to add a test for the read API
What's going on here? Take a moment to reflect on this. What is the simplest possible fix here?
- Loading branch information
Showing
2 changed files
with
41 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,10 +7,10 @@ import ( | |
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/jsteenb2/mess/allsrv" | ||
) | ||
|
||
|
@@ -21,16 +21,16 @@ func TestServer(t *testing.T) { | |
svr := allsrv.NewServer(db, "[email protected]", "PaSsWoRd", allsrv.WithIDFn(func() string { | ||
return "id1" | ||
})) | ||
|
||
req := httptest.NewRequest("POST", "/foo", newJSONBody(t, allsrv.Foo{ | ||
Name: "first-foo", | ||
Note: "some note", | ||
})) | ||
req.SetBasicAuth("[email protected]", "PaSsWoRd") | ||
rec := httptest.NewRecorder() | ||
|
||
svr.ServeHTTP(rec, req) | ||
|
||
assert.Equal(t, http.StatusCreated, rec.Code) | ||
expectJSONBody(t, rec.Body, func(t *testing.T, got allsrv.Foo) { | ||
want := allsrv.Foo{ | ||
|
@@ -42,24 +42,54 @@ func TestServer(t *testing.T) { | |
}) | ||
}) | ||
}) | ||
|
||
t.Run("foo read", func(t *testing.T) { | ||
t.Run("when querying for existing foo id should pass", func(t *testing.T) { | ||
db := new(allsrv.InmemDB) | ||
err := db.CreateFoo(allsrv.Foo{ | ||
ID: "reader1", | ||
Name: "read", | ||
Note: "another note", | ||
}) | ||
require.NoError(t, err) | ||
|
||
svr := allsrv.NewServer(db, "[email protected]", "PaSsWoRd") | ||
|
||
req := httptest.NewRequest("GET", "/foo?id=reader1", nil) | ||
req.SetBasicAuth("[email protected]", "PaSsWoRd") | ||
rec := httptest.NewRecorder() | ||
|
||
svr.ServeHTTP(rec, req) | ||
|
||
assert.Equal(t, http.StatusOK, rec.Code) | ||
expectJSONBody(t, rec.Body, func(t *testing.T, got allsrv.Foo) { | ||
want := allsrv.Foo{ | ||
ID: "reader1", | ||
Name: "read", | ||
Note: "another note", | ||
} | ||
assert.Equal(t, want, got) | ||
}) | ||
}) | ||
}) | ||
} | ||
|
||
func newJSONBody(t *testing.T, v any) *bytes.Buffer { | ||
t.Helper() | ||
|
||
var buf bytes.Buffer | ||
err := json.NewEncoder(&buf).Encode(v) | ||
require.NoError(t, err) | ||
|
||
return &buf | ||
} | ||
|
||
func expectJSONBody[T any](t *testing.T, r io.Reader, assertFn func(t *testing.T, got T)) { | ||
t.Helper() | ||
|
||
var out T | ||
err := json.NewDecoder(r).Decode(&out) | ||
require.NoError(t, err) | ||
|
||
assertFn(t, out) | ||
} |