MCPcopy Index your code
hub / github.com/git-bug/git-bug / TestGitFileHandlers

Function TestGitFileHandlers

api/http/git_file_handlers_test.go:21–92  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

19)
20
21func TestGitFileHandlers(t *testing.T) {
22 repo := repository.CreateGoGitTestRepo(t, false)
23
24 mrc := cache.NewMultiRepoCache()
25 repoCache, events := mrc.RegisterDefaultRepository(repo)
26 for event := range events {
27 require.NoError(t, event.Err)
28 }
29
30 author, err := repoCache.Identities().New("test identity", "test@test.org")
31 require.NoError(t, err)
32
33 err = repoCache.SetUserIdentity(author)
34 require.NoError(t, err)
35
36 // UPLOAD
37
38 uploadHandler := NewGitUploadFileHandler(mrc)
39
40 img := image.NewNRGBA(image.Rect(0, 0, 50, 50))
41 data := &bytes.Buffer{}
42 err = png.Encode(data, img)
43 require.NoError(t, err)
44
45 body := &bytes.Buffer{}
46 writer := multipart.NewWriter(body)
47 part, err := writer.CreateFormFile("uploadfile", "noname")
48 assert.NoError(t, err)
49
50 _, err = part.Write(data.Bytes())
51 assert.NoError(t, err)
52
53 err = writer.Close()
54 assert.NoError(t, err)
55
56 w := httptest.NewRecorder()
57 r, _ := http.NewRequest("GET", "/", body)
58 r.Header.Add("Content-Type", writer.FormDataContentType())
59
60 // Simulate auth
61 r = r.WithContext(auth.CtxWithUser(r.Context(), author.Id()))
62
63 // Handler's params
64 r = mux.SetURLVars(r, map[string]string{
65 "repo": "",
66 })
67
68 uploadHandler.ServeHTTP(w, r)
69
70 assert.Equal(t, http.StatusOK, w.Code)
71 assert.Equal(t, `{"hash":"3426a1488292d8f3f3c59ca679681336542b986f"}`, w.Body.String())
72 // DOWNLOAD
73
74 downloadHandler := NewGitFileHandler(mrc)
75
76 w = httptest.NewRecorder()
77 r, _ = http.NewRequest("GET", "/", nil)
78

Callers

nothing calls this directly

Calls 15

CreateGoGitTestRepoFunction · 0.92
NewMultiRepoCacheFunction · 0.92
CtxWithUserFunction · 0.92
NewGitUploadFileHandlerFunction · 0.85
NewGitFileHandlerFunction · 0.85
IdentitiesMethod · 0.80
SetUserIdentityMethod · 0.80
AddMethod · 0.80
BytesMethod · 0.65
CloseMethod · 0.65
IdMethod · 0.65

Tested by

no test coverage detected