TestAPICache tests the cache API
(t *testing.T)
| 21 | |
| 22 | // TestAPICache tests the cache API |
| 23 | func TestAPICache(t *testing.T) { |
| 24 | s, err := miniredis.Run() |
| 25 | if err != nil { |
| 26 | t.Fatalf("miniredis connection: %s", err) |
| 27 | } |
| 28 | defer s.Close() |
| 29 | |
| 30 | c := redis.NewClient(&redis.Options{Addr: s.Addr()}) |
| 31 | |
| 32 | testMsg := "This is a test!" |
| 33 | testUpdateRedis(t, c, testMsg) |
| 34 | |
| 35 | req := httptest.NewRequest(http.MethodGet, "/api/cache", nil) |
| 36 | w := httptest.NewRecorder() |
| 37 | handler := handleAPICache(c) |
| 38 | handler(w, req) |
| 39 | res := w.Result() |
| 40 | defer res.Body.Close() |
| 41 | |
| 42 | data, err := ioutil.ReadAll(res.Body) |
| 43 | if err != nil { |
| 44 | t.Errorf("expected error to be nil got %v", err) |
| 45 | } |
| 46 | if !strings.Contains(string(data), testMsg) { |
| 47 | t.Errorf("test message not found: %q", testMsg) |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | // testUpdateRedis inserts a marshalled message into mock redis |
| 52 | func testUpdateRedis(t *testing.T, c *redis.Client, message string) { |
nothing calls this directly
no test coverage detected