File tree Expand file tree Collapse file tree 1 file changed +49
-0
lines changed
Expand file tree Collapse file tree 1 file changed +49
-0
lines changed Original file line number Diff line number Diff line change 1+ package server
2+
3+ import (
4+ "encoding/json"
5+ "io/ioutil"
6+ "net/http"
7+ "net/http/httptest"
8+ "testing"
9+
10+ sample "github.com/friendsofgo/gopher-api/cmd/sample-data"
11+ gopher "github.com/friendsofgo/gopher-api/pkg"
12+ "github.com/friendsofgo/gopher-api/pkg/storage/inmem"
13+ )
14+
15+ func TestFetchGophers (t * testing.T ) {
16+ req , err := http .NewRequest ("GET" , "/gophers" , nil )
17+ if err != nil {
18+ t .Fatalf ("could not created request: %v" , err )
19+ }
20+
21+ repo := inmem .NewGopherRepository (sample .Gophers )
22+ s := New (repo )
23+
24+ rec := httptest .NewRecorder ()
25+
26+ s .FetchGophers (rec , req )
27+
28+ res := rec .Result ()
29+ defer res .Body .Close ()
30+ if res .StatusCode != http .StatusOK {
31+ t .Errorf ("expected %d, got: %d" , http .StatusOK , res .StatusCode )
32+ }
33+ b , err := ioutil .ReadAll (res .Body )
34+ if err != nil {
35+ t .Fatalf ("could not read response: %v" , err )
36+ }
37+
38+ var got []* gopher.Gopher
39+ err = json .Unmarshal (b , & got )
40+ if err != nil {
41+ t .Fatalf ("could not unmarshall response %v" , err )
42+ }
43+
44+ expected := len (sample .Gophers )
45+
46+ if len (got ) != expected {
47+ t .Errorf ("expected %d gophers, got: %d gopher" , sample .Gophers , got )
48+ }
49+ }
You can’t perform that action at this time.
0 commit comments