From 31c14f17dc321103f0de17077bb664006db3ed75 Mon Sep 17 00:00:00 2001 From: Nicolas Carlier Date: Tue, 7 Apr 2015 14:43:28 +0000 Subject: [PATCH] feat: Add unit tests for the API. --- src/{webhookd.go => api/api.go} | 29 +++------ src/api/api_test.go | 101 ++++++++++++++++++++++++++++++++ src/main.go | 27 +++++++++ 3 files changed, 135 insertions(+), 22 deletions(-) rename src/{webhookd.go => api/api.go} (63%) create mode 100644 src/api/api_test.go create mode 100644 src/main.go diff --git a/src/webhookd.go b/src/api/api.go similarity index 63% rename from src/webhookd.go rename to src/api/api.go index 5ab39cd..6982a4f 100644 --- a/src/webhookd.go +++ b/src/api/api.go @@ -1,7 +1,6 @@ -package main +package api import ( - "flag" "fmt" "github.com/gorilla/mux" "github.com/ncarlier/webhookd/hook" @@ -10,12 +9,7 @@ import ( "net/http" ) -var ( - LAddr = flag.String("l", ":8080", "HTTP service address (e.g.address, ':8080')") - NWorkers = flag.Int("n", 2, "The number of workers to start") -) - -func Handler(w http.ResponseWriter, r *http.Request) { +func createWebhookHandler(w http.ResponseWriter, r *http.Request) { params := mux.Vars(r) hookname := params["hookname"] action := params["action"] @@ -52,18 +46,9 @@ func Handler(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "Action %s of hook %s queued.", action, hookname) } -func main() { - flag.Parse() - - // Start the dispatcher. - fmt.Println("Starting the dispatcher") - worker.StartDispatcher(*NWorkers) - - rtr := mux.NewRouter() - rtr.HandleFunc("/{hookname:[a-z]+}/{action:[a-z]+}", Handler).Methods("POST") - - http.Handle("/", rtr) - - fmt.Println("webhookd server listening...") - log.Fatal(http.ListenAndServe(*LAddr, nil)) +func Handlers() *mux.Router{ + r := mux.NewRouter() + r.HandleFunc("/{hookname:[a-z]+}/{action:[a-z]+}", createWebhookHandler).Methods("POST") + return r } + diff --git a/src/api/api_test.go b/src/api/api_test.go new file mode 100644 index 0000000..c53b3fe --- /dev/null +++ b/src/api/api_test.go @@ -0,0 +1,101 @@ +package api_test + +import ( + "github.com/ncarlier/webhookd/api" + "fmt" + "io" + "net/http" + "net/http/httptest" + "strings" + "testing" +) + +var ( + server *httptest.Server + reader io.Reader +) + +func init() { + server = httptest.NewServer(api.Handlers()) +} + +func assertHook(t *testing.T, url string, json string, expectedStatus int) { + reader = strings.NewReader(json) + request, err := http.NewRequest("POST", url, reader) + res, err := http.DefaultClient.Do(request) + if err != nil { + t.Error(err) + } + if res.StatusCode != expectedStatus { + t.Errorf("Status expected: %d, Actual status: %d", expectedStatus, res.StatusCode) + } +} + +func TestBadHook(t *testing.T) { + url := fmt.Sprintf("%s/bad/echo", server.URL) + json := `{"foo": "bar"}` + assertHook(t, url, json, 404) +} + + +func TestGitlabHook(t *testing.T) { + url := fmt.Sprintf("%s/gitlab/echo", server.URL) + + json := `{ + "object_kind": "push", + "before": "95790bf891e76fee5e1747ab589903a6a1f80f22", + "after": "da1560886d4f094c3e6c9ef40349f7d38b5d27d7", + "ref": "refs/heads/master", + "user_email": "john@example.com", + "project_id": 15, + "repository": { + "name": "Diaspora", + "url": "git@example.com:mike/diasporadiaspora.git", + "description": "", + "git_http_url":"http://example.com/mike/diaspora.git", + "git_ssh_url":"git@example.com:mike/diaspora.git" + } + }` + + assertHook(t, url, json, 200) +} + +func TestGithubHook(t *testing.T) { + url := fmt.Sprintf("%s/github/echo", server.URL) + + json := `{ + "repository": { + "id": 20000106, + "name": "public-repo", + "full_name": "baxterthehacker/public-repo", + "html_url": "https://github.com/baxterthehacker/public-repo", + "description": "", + "url": "https://github.com/baxterthehacker/public-repo", + "git_url": "git://github.com/baxterthehacker/public-repo.git", + "ssh_url": "git@github.com:baxterthehacker/public-repo.git", + "homepage": null + } + }` + + assertHook(t, url, json, 200) +} + +func TestDockerHook(t *testing.T) { + url := fmt.Sprintf("%s/docker/echo", server.URL) + + json := `{ + "repository":{ + "status":"Active", + "description":"my docker repo that does cool things", + "full_description":"This is my full description", + "repo_url":"https://registry.hub.docker.com/u/username/reponame/", + "owner":"username", + "name":"reponame", + "namespace":"username", + "repo_name":"username/reponame" + } + }` + + assertHook(t, url, json, 200) +} + diff --git a/src/main.go b/src/main.go new file mode 100644 index 0000000..2bcae62 --- /dev/null +++ b/src/main.go @@ -0,0 +1,27 @@ +package main + +import ( + "flag" + "github.com/ncarlier/webhookd/api" + "github.com/ncarlier/webhookd/worker" + "log" + "net/http" +) + +var ( + LAddr = flag.String("l", ":8080", "HTTP service address (e.g.address, ':8080')") + NWorkers = flag.Int("n", 2, "The number of workers to start") +) + +func main() { + flag.Parse() + + log.Println("Starting webhookd server...") + + // Start the dispatcher. + log.Println("Starting the dispatcher") + worker.StartDispatcher(*NWorkers) + + log.Println("Starting the http server") + log.Fatal(http.ListenAndServe(*LAddr, api.Handlers())) +}