MCPcopy Create free account
hub / github.com/ebosas/microservices / TestAPICache

Function TestAPICache

cmd/server/api_test.go:23–49  ·  view source on GitHub ↗

TestAPICache tests the cache API

(t *testing.T)

Source from the content-addressed store, hash-verified

21
22// TestAPICache tests the cache API
23func 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
52func testUpdateRedis(t *testing.T, c *redis.Client, message string) {

Callers

nothing calls this directly

Calls 3

testUpdateRedisFunction · 0.85
handleAPICacheFunction · 0.85
CloseMethod · 0.80

Tested by

no test coverage detected