From 5c610227aeb10ce18bce08fd640156d65cfe8462 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alejandro=20Rodr=C3=ADguez?= Date: Fri, 6 Jan 2017 00:00:31 -0300 Subject: [PATCH] Add handler for post-receive notification --- handler/post_receive.go | 10 ++++++++++ handler/post_receive_test.go | 30 ++++++++++++++++++++++++++++++ router/router.go | 1 + 3 files changed, 41 insertions(+) create mode 100644 handler/post_receive.go create mode 100644 handler/post_receive_test.go diff --git a/handler/post_receive.go b/handler/post_receive.go new file mode 100644 index 00000000000..e656d19627a --- /dev/null +++ b/handler/post_receive.go @@ -0,0 +1,10 @@ +package handler + +import ( + "net/http" +) + +func PostReceive(w http.ResponseWriter, r *http.Request) { + // TODO: Invalidate info-refs cache. For now, just return 200 + w.WriteHeader(http.StatusOK) +} diff --git a/handler/post_receive_test.go b/handler/post_receive_test.go new file mode 100644 index 00000000000..b7b5578b46d --- /dev/null +++ b/handler/post_receive_test.go @@ -0,0 +1,30 @@ +package handler + +import ( + "bytes" + "net/http" + "net/http/httptest" + "net/url" + "strconv" + "testing" +) + +func TestGetPostReceive(t *testing.T) { + recorder := httptest.NewRecorder() + data := url.Values{} + data.Set("project", "foo/bar.git") + data.Set("changes", "0000000000000000000000000000000000000000 92d0970eefd7acb6d548878925ce2208cfe2d2ec refs/heads/branch4") + + req, err := http.NewRequest("POST", "/post-receive", bytes.NewBufferString(data.Encode())) + if err != nil { + t.Fatal("Creating 'POST /post-receive' request failed!") + } + req.Header.Add("Content-Type", "application/x-www-form-urlencoded") + req.Header.Add("Content-Length", strconv.Itoa(len(data.Encode()))) + + http.HandlerFunc(PostReceive).ServeHTTP(recorder, req) + + if recorder.Code != http.StatusOK { + t.Fatal("Server error: Returned ", recorder.Code, " instead of ", http.StatusOK) + } +} diff --git a/router/router.go b/router/router.go index 0c929cb2152..996fdaac950 100644 --- a/router/router.go +++ b/router/router.go @@ -14,6 +14,7 @@ func NewRouter() http.Handler { r := mux.NewRouter() r.HandleFunc("/", handler.Home) + r.HandleFunc("/post-receive", handler.PostReceive) return handlers.LoggingHandler(os.Stdout, r) } -- GitLab