diff --git a/test/mock/gitlab/server.go b/test/mock/gitlab/server.go new file mode 100644 index 0000000..1102e95 --- /dev/null +++ b/test/mock/gitlab/server.go @@ -0,0 +1,44 @@ +package gitlab_mock + +import ( + "encoding/json" + "fmt" + "net/http" + "path" + "strings" +) + +type FileHandler struct { + Root http.FileSystem +} + +func (f *FileHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { + upath := r.URL.Path + if !strings.HasPrefix(upath, "/") { + upath = "/" + upath + r.URL.Path = upath + } + serveFile(w, r, f.Root, path.Clean(upath)) +} + +func serveFile(w http.ResponseWriter, r *http.Request, fs http.FileSystem, name string) { + f, err := fs.Open(name + ".json") + if err != nil { + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusNotFound) + fmt.Fprintln(w, fmt.Sprintf(`{"message": "%s"}`, err)) + return + } + defer f.Close() + + var data interface{} + if err := json.NewDecoder(f).Decode(&data); err != nil { + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusInternalServerError) + fmt.Fprintln(w, fmt.Sprintf(`{"message": "%s"}`, err)) + return + } + + w.Header().Set("Content-Type", "application/json") + json.NewEncoder(w).Encode(data) +}