feat: Add unit tests for the API.

This commit is contained in:
Nicolas Carlier 2015-04-07 14:43:28 +00:00
parent be4483d3dd
commit 31c14f17dc
3 changed files with 135 additions and 22 deletions

View File

@ -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
}

101
src/api/api_test.go Normal file
View File

@ -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)
}

27
src/main.go Normal file
View File

@ -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()))
}