Skip to content

Commit 294b529

Browse files
committed
Add gitlab server mock to serve json testdata
1 parent 72b378e commit 294b529

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

test/mock/gitlab/server.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package gitlab_mock
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"net/http"
7+
"path"
8+
"strings"
9+
)
10+
11+
type FileHandler struct {
12+
Root http.FileSystem
13+
}
14+
15+
func (f *FileHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
16+
upath := r.URL.Path
17+
if !strings.HasPrefix(upath, "/") {
18+
upath = "/" + upath
19+
r.URL.Path = upath
20+
}
21+
serveFile(w, r, f.Root, path.Clean(upath))
22+
}
23+
24+
func serveFile(w http.ResponseWriter, r *http.Request, fs http.FileSystem, name string) {
25+
f, err := fs.Open(name + ".json")
26+
if err != nil {
27+
w.Header().Set("Content-Type", "application/json")
28+
w.WriteHeader(http.StatusNotFound)
29+
fmt.Fprintln(w, fmt.Sprintf(`{"message": "%s"}`, err))
30+
return
31+
}
32+
defer f.Close()
33+
34+
var data interface{}
35+
if err := json.NewDecoder(f).Decode(&data); err != nil {
36+
w.Header().Set("Content-Type", "application/json")
37+
w.WriteHeader(http.StatusInternalServerError)
38+
fmt.Fprintln(w, fmt.Sprintf(`{"message": "%s"}`, err))
39+
return
40+
}
41+
42+
w.Header().Set("Content-Type", "application/json")
43+
json.NewEncoder(w).Encode(data)
44+
}

0 commit comments

Comments
 (0)