@@ -7,10 +7,10 @@ import (
7
7
"net/http"
8
8
"net/http/httptest"
9
9
"testing"
10
-
10
+
11
11
"github.com/stretchr/testify/assert"
12
12
"github.com/stretchr/testify/require"
13
-
13
+
14
14
"github.com/jsteenb2/mess/allsrv"
15
15
)
16
16
@@ -21,16 +21,16 @@ func TestServer(t *testing.T) {
21
21
svr := allsrv .NewServer (db , "dodgers@stink.com" , "PaSsWoRd" , allsrv .WithIDFn (func () string {
22
22
return "id1"
23
23
}))
24
-
24
+
25
25
req := httptest .NewRequest ("POST" , "/foo" , newJSONBody (t , allsrv.Foo {
26
26
Name : "first-foo" ,
27
27
Note : "some note" ,
28
28
}))
29
29
req .SetBasicAuth ("dodgers@stink.com" , "PaSsWoRd" )
30
30
rec := httptest .NewRecorder ()
31
-
31
+
32
32
svr .ServeHTTP (rec , req )
33
-
33
+
34
34
assert .Equal (t , http .StatusCreated , rec .Code )
35
35
expectJSONBody (t , rec .Body , func (t * testing.T , got allsrv.Foo ) {
36
36
want := allsrv.Foo {
@@ -42,24 +42,54 @@ func TestServer(t *testing.T) {
42
42
})
43
43
})
44
44
})
45
+
46
+ t .Run ("foo read" , func (t * testing.T ) {
47
+ t .Run ("when querying for existing foo id should pass" , func (t * testing.T ) {
48
+ db := new (allsrv.InmemDB )
49
+ err := db .CreateFoo (allsrv.Foo {
50
+ ID : "reader1" ,
51
+ Name : "read" ,
52
+ Note : "another note" ,
53
+ })
54
+ require .NoError (t , err )
55
+
56
+ svr := allsrv .NewServer (db , "dodgers@stink.com" , "PaSsWoRd" )
57
+
58
+ req := httptest .NewRequest ("GET" , "/foo?id=reader1" , nil )
59
+ req .SetBasicAuth ("dodgers@stink.com" , "PaSsWoRd" )
60
+ rec := httptest .NewRecorder ()
61
+
62
+ svr .ServeHTTP (rec , req )
63
+
64
+ assert .Equal (t , http .StatusOK , rec .Code )
65
+ expectJSONBody (t , rec .Body , func (t * testing.T , got allsrv.Foo ) {
66
+ want := allsrv.Foo {
67
+ ID : "reader1" ,
68
+ Name : "read" ,
69
+ Note : "another note" ,
70
+ }
71
+ assert .Equal (t , want , got )
72
+ })
73
+ })
74
+ })
45
75
}
46
76
47
77
func newJSONBody (t * testing.T , v any ) * bytes.Buffer {
48
78
t .Helper ()
49
-
79
+
50
80
var buf bytes.Buffer
51
81
err := json .NewEncoder (& buf ).Encode (v )
52
82
require .NoError (t , err )
53
-
83
+
54
84
return & buf
55
85
}
56
86
57
87
func expectJSONBody [T any ](t * testing.T , r io.Reader , assertFn func (t * testing.T , got T )) {
58
88
t .Helper ()
59
-
89
+
60
90
var out T
61
91
err := json .NewDecoder (r ).Decode (& out )
62
92
require .NoError (t , err )
63
-
93
+
64
94
assertFn (t , out )
65
95
}
0 commit comments