MCPcopy
hub / github.com/nektos/act / TestHandler

Function TestHandler

pkg/artifactcache/handler_test.go:21–529  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

19)
20
21func TestHandler(t *testing.T) {
22 dir := filepath.Join(t.TempDir(), "artifactcache")
23 handler, err := StartHandler(dir, "", "", 0, nil)
24 require.NoError(t, err)
25
26 base := fmt.Sprintf("%s%s", handler.ExternalURL(), apiPath)
27
28 defer func() {
29 t.Run("inpect db", func(t *testing.T) {
30 db, err := handler.openDB()
31 require.NoError(t, err)
32 defer db.Close()
33 require.NoError(t, db.Bolt().View(func(tx *bbolt.Tx) error {
34 return tx.Bucket([]byte("Cache")).ForEach(func(k, v []byte) error {
35 t.Logf("%s: %s", k, v)
36 return nil
37 })
38 }))
39 })
40 t.Run("close", func(t *testing.T) {
41 require.NoError(t, handler.Close())
42 assert.Nil(t, handler.server)
43 assert.Nil(t, handler.listener)
44 _, err := http.Post(fmt.Sprintf("%s/caches/%d", base, 1), "", nil)
45 assert.Error(t, err)
46 })
47 }()
48
49 t.Run("get not exist", func(t *testing.T) {
50 key := strings.ToLower(t.Name())
51 version := "c19da02a2bd7e77277f1ac29ab45c09b7d46a4ee758284e26bb3045ad11d9d20"
52 resp, err := http.Get(fmt.Sprintf("%s/cache?keys=%s&version=%s", base, key, version))
53 require.NoError(t, err)
54 require.Equal(t, 204, resp.StatusCode)
55 })
56
57 t.Run("reserve and upload", func(t *testing.T) {
58 key := strings.ToLower(t.Name())
59 version := "c19da02a2bd7e77277f1ac29ab45c09b7d46a4ee758284e26bb3045ad11d9d20"
60 content := make([]byte, 100)
61 _, err := rand.Read(content)
62 require.NoError(t, err)
63 uploadCacheNormally(t, base, key, version, content)
64 })
65
66 t.Run("clean", func(t *testing.T) {
67 resp, err := http.Post(fmt.Sprintf("%s/clean", base), "", nil)
68 require.NoError(t, err)
69 assert.Equal(t, 200, resp.StatusCode)
70 })
71
72 t.Run("reserve with bad request", func(t *testing.T) {
73 body := []byte(`invalid json`)
74 require.NoError(t, err)
75 resp, err := http.Post(fmt.Sprintf("%s/caches", base), "application/json", bytes.NewReader(body))
76 require.NoError(t, err)
77 assert.Equal(t, 400, resp.StatusCode)
78 })

Callers

nothing calls this directly

Calls 9

StartHandlerFunction · 0.85
makeFunction · 0.85
uploadCacheNormallyFunction · 0.85
ExternalURLMethod · 0.80
openDBMethod · 0.80
NameMethod · 0.80
ReadMethod · 0.80
CloseMethod · 0.65
ErrorMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…