|
| 1 | +package server |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "io/ioutil" |
| 7 | + "net/http" |
| 8 | + "net/http/httptest" |
| 9 | + "testing" |
| 10 | + |
| 11 | + sample "github.com/friendsofgo/gopher-api/cmd/sample-data" |
| 12 | + gopher "github.com/friendsofgo/gopher-api/pkg" |
| 13 | + "github.com/friendsofgo/gopher-api/pkg/storage/inmem" |
| 14 | +) |
| 15 | + |
| 16 | +func TestFetchGophers(t *testing.T) { |
| 17 | + req, err := http.NewRequest("GET", "/gophers", nil) |
| 18 | + if err != nil { |
| 19 | + t.Fatalf("could not created request: %v", err) |
| 20 | + } |
| 21 | + |
| 22 | + repo := inmem.NewGopherRepository(sample.Gophers) |
| 23 | + s := New(repo) |
| 24 | + |
| 25 | + rec := httptest.NewRecorder() |
| 26 | + |
| 27 | + s.FetchGophers(rec, req) |
| 28 | + |
| 29 | + res := rec.Result() |
| 30 | + defer res.Body.Close() |
| 31 | + if res.StatusCode != http.StatusOK { |
| 32 | + t.Errorf("expected %d, got: %d", http.StatusOK, res.StatusCode) |
| 33 | + } |
| 34 | + b, err := ioutil.ReadAll(res.Body) |
| 35 | + if err != nil { |
| 36 | + t.Fatalf("could not read response: %v", err) |
| 37 | + } |
| 38 | + |
| 39 | + var got []*gopher.Gopher |
| 40 | + err = json.Unmarshal(b, &got) |
| 41 | + if err != nil { |
| 42 | + t.Fatalf("could not unmarshall response %v", err) |
| 43 | + } |
| 44 | + |
| 45 | + expected := len(sample.Gophers) |
| 46 | + |
| 47 | + if len(got) != expected { |
| 48 | + t.Errorf("expected %d gophers, got: %d gopher", sample.Gophers, got) |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +func TestFetchGopher(t *testing.T) { |
| 53 | + |
| 54 | + testData := []struct { |
| 55 | + name string |
| 56 | + g *gopher.Gopher |
| 57 | + status int |
| 58 | + err string |
| 59 | + }{ |
| 60 | + {name: "gopher found", g: gopherSample(), status: http.StatusOK}, |
| 61 | + {name: "gopher not found", g: &gopher.Gopher{ID: "123"}, status: http.StatusNotFound, err: "Gopher Not found"}, |
| 62 | + } |
| 63 | + |
| 64 | + for _, tt := range testData { |
| 65 | + t.Run(tt.name, func(t *testing.T) { |
| 66 | + uri := fmt.Sprintf("/gophers/%s", tt.g.ID) |
| 67 | + req, err := http.NewRequest("GET", uri, nil) |
| 68 | + if err != nil { |
| 69 | + t.Fatalf("could not created request: %v", err) |
| 70 | + } |
| 71 | + |
| 72 | + repo := inmem.NewGopherRepository(sample.Gophers) |
| 73 | + s := New(repo) |
| 74 | + |
| 75 | + rec := httptest.NewRecorder() |
| 76 | + s.Router().ServeHTTP(rec, req) |
| 77 | + |
| 78 | + res := rec.Result() |
| 79 | + |
| 80 | + defer res.Body.Close() |
| 81 | + if tt.status != res.StatusCode { |
| 82 | + t.Errorf("expected %d, got: %d", tt.status, res.StatusCode) |
| 83 | + } |
| 84 | + b, err := ioutil.ReadAll(res.Body) |
| 85 | + if err != nil { |
| 86 | + t.Fatalf("could not read response: %v", err) |
| 87 | + } |
| 88 | + |
| 89 | + if tt.err == "" { |
| 90 | + var got *gopher.Gopher |
| 91 | + err = json.Unmarshal(b, &got) |
| 92 | + if err != nil { |
| 93 | + t.Fatalf("could not unmarshall response %v", err) |
| 94 | + } |
| 95 | + |
| 96 | + if *got != *tt.g { |
| 97 | + t.Fatalf("expected %v, got: %v", tt.g, got) |
| 98 | + } |
| 99 | + } |
| 100 | + }) |
| 101 | + } |
| 102 | + |
| 103 | +} |
| 104 | + |
| 105 | +func gopherSample() *gopher.Gopher { |
| 106 | + return &gopher.Gopher{ |
| 107 | + ID: "01D3XZ3ZHCP3KG9VT4FGAD8KDR", |
| 108 | + Name: "Jenny", |
| 109 | + Age: 18, |
| 110 | + Image: "https://storage.googleapis.com/gopherizeme.appspot.com/gophers/0ceb2c10fc0c30575c18ff1defa1ffd41501bc62.png", |
| 111 | + } |
| 112 | +} |
0 commit comments