-
Notifications
You must be signed in to change notification settings - Fork 0
/
mock_http_git_server_test.go
188 lines (157 loc) · 5.3 KB
/
mock_http_git_server_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package vignet_test
import (
"fmt"
"net/http"
"github.com/apex/log"
"github.com/go-git/go-billy/v5"
"github.com/go-git/go-git/v5/plumbing/format/pktline"
"github.com/go-git/go-git/v5/plumbing/protocol/packp"
"github.com/go-git/go-git/v5/plumbing/transport"
gitHttp "github.com/go-git/go-git/v5/plumbing/transport/http"
"github.com/go-git/go-git/v5/plumbing/transport/server"
)
type mockHttpGitServer struct {
srv transport.Transport
mux http.Handler
}
func (m *mockHttpGitServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
m.mux.ServeHTTP(w, r)
}
var _ http.Handler = &mockHttpGitServer{}
type mockHttpGitServerOpts struct {
basicAuth *gitHttp.BasicAuth
}
func newMockHttpGitServer(fs billy.Filesystem, opts mockHttpGitServerOpts) *mockHttpGitServer {
ld := server.NewFilesystemLoader(fs)
srv := server.NewServer(ld)
s := &mockHttpGitServer{
srv: srv,
}
mux := http.NewServeMux()
mux.HandleFunc("/info/refs", s.httpInfoRefs)
mux.HandleFunc("/git-upload-pack", s.httpGitUploadPack)
mux.HandleFunc("/git-receive-pack", s.httpGitReceivePack)
s.mux = mux
return s
}
func (m *mockHttpGitServer) httpInfoRefs(rw http.ResponseWriter, r *http.Request) {
log.Debugf("Request httpInfoRefs %s %s", r.Method, r.URL)
service := r.URL.Query().Get("service")
if service != "git-upload-pack" && service != "git-receive-pack" {
http.Error(rw, "Smart Git is required", http.StatusForbidden)
return
}
rw.Header().Set("Content-Type", fmt.Sprintf("application/x-%s-advertisement", service))
ep, err := transport.NewEndpoint("/")
if err != nil {
http.Error(rw, "Internal server error", http.StatusInternalServerError)
log.WithError(err).Error("Failed to create endpoint")
return
}
var sess transport.Session
if service == "git-upload-pack" {
sess, err = m.srv.NewUploadPackSession(ep, nil)
if err != nil {
http.Error(rw, "Internal server error", http.StatusInternalServerError)
log.WithError(err).Error("Failed to create upload pack session")
return
}
} else {
sess, err = m.srv.NewReceivePackSession(ep, nil)
if err != nil {
http.Error(rw, "Internal server error", http.StatusInternalServerError)
log.WithError(err).Error("Failed to create receive pack session")
return
}
}
defer sess.Close()
ar, err := sess.AdvertisedReferencesContext(r.Context())
if err != nil {
http.Error(rw, "Internal server error", http.StatusInternalServerError)
log.WithError(err).Error("Failed to get advertised references")
return
}
ar.Prefix = [][]byte{
[]byte(fmt.Sprintf("# service=%s", service)),
pktline.Flush,
}
err = ar.Encode(rw)
if err != nil {
http.Error(rw, "Internal server error", http.StatusInternalServerError)
log.WithError(err).Error("Failed to encode advertised references")
return
}
}
func (m *mockHttpGitServer) httpGitUploadPack(rw http.ResponseWriter, r *http.Request) {
log.Debugf("Request httpGitUploadPack %s %s", r.Method, r.URL)
rw.Header().Set("Content-Type", "application/x-git-upload-pack-result")
upr := packp.NewUploadPackRequest()
err := upr.Decode(r.Body)
if err != nil {
http.Error(rw, "Internal server error", http.StatusInternalServerError)
log.WithError(err).Error("Failed to decode upload pack request")
return
}
ep, err := transport.NewEndpoint("/")
if err != nil {
http.Error(rw, "Internal server error", http.StatusInternalServerError)
log.WithError(err).Error("Failed to create endpoint")
return
}
sess, err := m.srv.NewUploadPackSession(ep, nil)
if err != nil {
http.Error(rw, "Internal server error", http.StatusInternalServerError)
log.WithError(err).Error("Failed to create upload pack session")
return
}
defer sess.Close()
res, err := sess.UploadPack(r.Context(), upr)
if err != nil {
http.Error(rw, "Internal server error", http.StatusInternalServerError)
log.WithError(err).Error("Failed to upload pack")
return
}
defer res.Close()
err = res.Encode(rw)
if err != nil {
http.Error(rw, "Internal server error", http.StatusInternalServerError)
log.WithError(err).Error("Failed to encode upload pack response")
return
}
}
func (m *mockHttpGitServer) httpGitReceivePack(rw http.ResponseWriter, r *http.Request) {
log.Debugf("Request httpGitReceivePack %s %s", r.Method, r.URL)
rw.Header().Set("Content-Type", "application/x-git-receive-pack-result")
upr := packp.NewReferenceUpdateRequest()
err := upr.Decode(r.Body)
if err != nil {
http.Error(rw, "Internal server error", http.StatusInternalServerError)
log.WithError(err).Error("Failed to decode reference update request")
return
}
ep, err := transport.NewEndpoint("/")
if err != nil {
http.Error(rw, "Internal server error", http.StatusInternalServerError)
log.WithError(err).Error("Failed to create endpoint")
return
}
sess, err := m.srv.NewReceivePackSession(ep, nil)
if err != nil {
http.Error(rw, "Internal server error", http.StatusInternalServerError)
log.WithError(err).Error("Failed to create receive pack session")
return
}
defer sess.Close()
res, err := sess.ReceivePack(r.Context(), upr)
if err != nil {
http.Error(rw, "Internal server error", http.StatusInternalServerError)
log.WithError(err).Error("Failed to receive pack")
return
}
err = res.Encode(rw)
if err != nil {
http.Error(rw, "Internal server error", http.StatusInternalServerError)
log.WithError(err).Error("Failed to encode receive pack response")
return
}
}