From b65e549d95382ccc3fea5fd3259aa0a39d1f9afd Mon Sep 17 00:00:00 2001 From: joeybloggs Date: Mon, 2 Nov 2015 10:20:44 -0500 Subject: [PATCH] Add timeout for server start for tests * updated a few ports in the examples, just so can copy, paste and run locally without modification. --- README.md | 4 +-- examples/multiple-handlers/main.go | 2 +- examples/single-handler/main.go | 2 +- github/github_test.go | 56 ++++++++++++++++-------------- webhooks_test.go | 28 ++++++++------- 5 files changed, 49 insertions(+), 43 deletions(-) diff --git a/README.md b/README.md index 0bfdd35..1daf3f1 100644 --- a/README.md +++ b/README.md @@ -53,7 +53,7 @@ import ( const ( path = "/webhooks" - port = 80 + port = 3016 ) func main() { @@ -109,7 +109,7 @@ import ( const ( path = "/webhooks" - port = 80 + port = 3016 ) func main() { diff --git a/examples/multiple-handlers/main.go b/examples/multiple-handlers/main.go index 4d17e08..5f5214b 100644 --- a/examples/multiple-handlers/main.go +++ b/examples/multiple-handlers/main.go @@ -10,7 +10,7 @@ import ( const ( path = "/webhooks" - port = 80 + port = 3016 ) func main() { diff --git a/examples/single-handler/main.go b/examples/single-handler/main.go index 5a60199..dfc46db 100644 --- a/examples/single-handler/main.go +++ b/examples/single-handler/main.go @@ -10,7 +10,7 @@ import ( const ( path = "/webhooks" - port = 80 + port = 3016 ) func main() { diff --git a/github/github_test.go b/github/github_test.go index a220f7d..cc74298 100644 --- a/github/github_test.go +++ b/github/github_test.go @@ -6,6 +6,7 @@ import ( "os" "strconv" "testing" + "time" . "gopkg.in/go-playground/assert.v1" "gopkg.in/go-playground/webhooks.v1" @@ -40,7 +41,8 @@ func TestMain(m *testing.M) { hook = New(&Config{Secret: "IsWishesWereHorsesWedAllBeEatingSteak!"}) hook.RegisterEvents(HandlePayload, CommitCommentEvent, CreateEvent, CreateEvent, DeleteEvent, DeploymentEvent, DeploymentStatusEvent, ForkEvent, GollumEvent, IssueCommentEvent, IssuesEvent, MemberEvent, MembershipEvent, PageBuildEvent, PublicEvent, PullRequestReviewCommentEvent, PullRequestEvent, PushEvent, RepositoryEvent, ReleaseEvent, StatusEvent, TeamAddEvent, WatchEvent) - go webhooks.Run(hook, ":"+strconv.Itoa(port), path) + go webhooks.Run(hook, "127.0.0.1:"+strconv.Itoa(port), path) + time.Sleep(5000) os.Exit(m.Run()) @@ -54,7 +56,7 @@ func TestProvider(t *testing.T) { func TestBadNoEventHeader(t *testing.T) { payload := "{}" - req, err := http.NewRequest("POST", "http://localhost:3009/webhooks", bytes.NewBuffer([]byte(payload))) + req, err := http.NewRequest("POST", "http://127.0.0.1:3009/webhooks", bytes.NewBuffer([]byte(payload))) req.Header.Set("Content-Type", "application/json") Equal(t, err, nil) @@ -71,7 +73,7 @@ func TestBadNoEventHeader(t *testing.T) { func TestUnsubscribedEvent(t *testing.T) { payload := "{}" - req, err := http.NewRequest("POST", "http://localhost:3009/webhooks", bytes.NewBuffer([]byte(payload))) + req, err := http.NewRequest("POST", "http://127.0.0.1:3009/webhooks", bytes.NewBuffer([]byte(payload))) req.Header.Set("Content-Type", "application/json") req.Header.Set("X-Github-Event", "noneexistant_event") @@ -89,7 +91,7 @@ func TestUnsubscribedEvent(t *testing.T) { func TestBadBody(t *testing.T) { payload := "" - req, err := http.NewRequest("POST", "http://localhost:3009/webhooks", bytes.NewBuffer([]byte(payload))) + req, err := http.NewRequest("POST", "http://127.0.0.1:3009/webhooks", bytes.NewBuffer([]byte(payload))) req.Header.Set("Content-Type", "application/json") req.Header.Set("X-Github-Event", "commit_comment") req.Header.Set("X-Hub-Signature", "sha1=156404ad5f721c53151147f3d3d302329f95a3ab") @@ -108,7 +110,7 @@ func TestBadBody(t *testing.T) { func TestBadSignatureLength(t *testing.T) { payload := "{}" - req, err := http.NewRequest("POST", "http://localhost:3009/webhooks", bytes.NewBuffer([]byte(payload))) + req, err := http.NewRequest("POST", "http://127.0.0.1:3009/webhooks", bytes.NewBuffer([]byte(payload))) req.Header.Set("Content-Type", "application/json") req.Header.Set("X-Github-Event", "commit_comment") req.Header.Set("X-Hub-Signature", "") @@ -127,7 +129,7 @@ func TestBadSignatureLength(t *testing.T) { func TestBadSignatureMatch(t *testing.T) { payload := "{}" - req, err := http.NewRequest("POST", "http://localhost:3009/webhooks", bytes.NewBuffer([]byte(payload))) + req, err := http.NewRequest("POST", "http://127.0.0.1:3009/webhooks", bytes.NewBuffer([]byte(payload))) req.Header.Set("Content-Type", "application/json") req.Header.Set("X-Github-Event", "commit_comment") req.Header.Set("X-Hub-Signature", "sha1=111") @@ -287,7 +289,7 @@ func TestCommitCommentEvent(t *testing.T) { } ` - req, err := http.NewRequest("POST", "http://localhost:3009/webhooks", bytes.NewBuffer([]byte(payload))) + req, err := http.NewRequest("POST", "http://127.0.0.1:3009/webhooks", bytes.NewBuffer([]byte(payload))) req.Header.Set("Content-Type", "application/json") req.Header.Set("X-Github-Event", "commit_comment") req.Header.Set("X-Hub-Signature", "sha1=156404ad5f721c53151147f3d3d302329f95a3ab") @@ -420,7 +422,7 @@ func TestCreateEvent(t *testing.T) { } ` - req, err := http.NewRequest("POST", "http://localhost:3009/webhooks", bytes.NewBuffer([]byte(payload))) + req, err := http.NewRequest("POST", "http://127.0.0.1:3009/webhooks", bytes.NewBuffer([]byte(payload))) req.Header.Set("Content-Type", "application/json") req.Header.Set("X-Github-Event", "create") req.Header.Set("X-Hub-Signature", "sha1=77ff16ca116034bbeed77ebfce83b36572a9cbaf") @@ -551,7 +553,7 @@ func TestDeleteEvent(t *testing.T) { } ` - req, err := http.NewRequest("POST", "http://localhost:3009/webhooks", bytes.NewBuffer([]byte(payload))) + req, err := http.NewRequest("POST", "http://127.0.0.1:3009/webhooks", bytes.NewBuffer([]byte(payload))) req.Header.Set("Content-Type", "application/json") req.Header.Set("X-Github-Event", "delete") req.Header.Set("X-Hub-Signature", "sha1=4ddef04fd05b504c7041e294fca3ad1804bc7be1") @@ -713,7 +715,7 @@ func TestDeploymentEvent(t *testing.T) { } ` - req, err := http.NewRequest("POST", "http://localhost:3009/webhooks", bytes.NewBuffer([]byte(payload))) + req, err := http.NewRequest("POST", "http://127.0.0.1:3009/webhooks", bytes.NewBuffer([]byte(payload))) req.Header.Set("Content-Type", "application/json") req.Header.Set("X-Github-Event", "deployment") req.Header.Set("X-Hub-Signature", "sha1=bb47dc63ceb764a6b1f14fe123e299e5b814c67c") @@ -905,7 +907,7 @@ func TestDeploymentStatusEvent(t *testing.T) { } ` - req, err := http.NewRequest("POST", "http://localhost:3009/webhooks", bytes.NewBuffer([]byte(payload))) + req, err := http.NewRequest("POST", "http://127.0.0.1:3009/webhooks", bytes.NewBuffer([]byte(payload))) req.Header.Set("Content-Type", "application/json") req.Header.Set("X-Github-Event", "deployment_status") req.Header.Set("X-Hub-Signature", "sha1=8dc0bd0be97440e282e1b4c9ec8445a8d095dc28") @@ -1121,7 +1123,7 @@ func TestForkEvent(t *testing.T) { } ` - req, err := http.NewRequest("POST", "http://localhost:3009/webhooks", bytes.NewBuffer([]byte(payload))) + req, err := http.NewRequest("POST", "http://127.0.0.1:3009/webhooks", bytes.NewBuffer([]byte(payload))) req.Header.Set("Content-Type", "application/json") req.Header.Set("X-Github-Event", "fork") req.Header.Set("X-Hub-Signature", "sha1=cec5f8fb7c383514c622d3eb9e121891dfcca848") @@ -1259,7 +1261,7 @@ func TestGollumEvent(t *testing.T) { } ` - req, err := http.NewRequest("POST", "http://localhost:3009/webhooks", bytes.NewBuffer([]byte(payload))) + req, err := http.NewRequest("POST", "http://127.0.0.1:3009/webhooks", bytes.NewBuffer([]byte(payload))) req.Header.Set("Content-Type", "application/json") req.Header.Set("X-Github-Event", "gollum") req.Header.Set("X-Hub-Signature", "sha1=a375a6dc8ceac7231ee022211f8eb85e2a84a5b9") @@ -1461,7 +1463,7 @@ func TestIssueCommentEvent(t *testing.T) { } ` - req, err := http.NewRequest("POST", "http://localhost:3009/webhooks", bytes.NewBuffer([]byte(payload))) + req, err := http.NewRequest("POST", "http://127.0.0.1:3009/webhooks", bytes.NewBuffer([]byte(payload))) req.Header.Set("Content-Type", "application/json") req.Header.Set("X-Github-Event", "issue_comment") req.Header.Set("X-Hub-Signature", "sha1=e724c9f811fcf5f511aac32e4251b08ab1a0fd87") @@ -1635,7 +1637,7 @@ func TestIssuesEvent(t *testing.T) { } ` - req, err := http.NewRequest("POST", "http://localhost:3009/webhooks", bytes.NewBuffer([]byte(payload))) + req, err := http.NewRequest("POST", "http://127.0.0.1:3009/webhooks", bytes.NewBuffer([]byte(payload))) req.Header.Set("Content-Type", "application/json") req.Header.Set("X-Github-Event", "issues") req.Header.Set("X-Hub-Signature", "sha1=266736f9446195ffefd3d0cfcd1e096ab129ccad") @@ -1783,7 +1785,7 @@ func TestMemberEvent(t *testing.T) { } ` - req, err := http.NewRequest("POST", "http://localhost:3009/webhooks", bytes.NewBuffer([]byte(payload))) + req, err := http.NewRequest("POST", "http://127.0.0.1:3009/webhooks", bytes.NewBuffer([]byte(payload))) req.Header.Set("Content-Type", "application/json") req.Header.Set("X-Github-Event", "member") req.Header.Set("X-Hub-Signature", "sha1=597e7d6627a6636d4c3283e36631983fbd57bdd0") @@ -1864,7 +1866,7 @@ func TestMembershipEvent(t *testing.T) { } ` - req, err := http.NewRequest("POST", "http://localhost:3009/webhooks", bytes.NewBuffer([]byte(payload))) + req, err := http.NewRequest("POST", "http://127.0.0.1:3009/webhooks", bytes.NewBuffer([]byte(payload))) req.Header.Set("Content-Type", "application/json") req.Header.Set("X-Github-Event", "membership") req.Header.Set("X-Hub-Signature", "sha1=16928c947b3707b0efcf8ceb074a5d5dedc9c76e") @@ -2023,7 +2025,7 @@ func TestPageBuildEvent(t *testing.T) { } ` - req, err := http.NewRequest("POST", "http://localhost:3009/webhooks", bytes.NewBuffer([]byte(payload))) + req, err := http.NewRequest("POST", "http://127.0.0.1:3009/webhooks", bytes.NewBuffer([]byte(payload))) req.Header.Set("Content-Type", "application/json") req.Header.Set("X-Github-Event", "page_build") req.Header.Set("X-Hub-Signature", "sha1=b3abad8f9c1b3fc0b01c4eb107447800bb5000f9") @@ -2151,7 +2153,7 @@ func TestPublicEvent(t *testing.T) { } ` - req, err := http.NewRequest("POST", "http://localhost:3009/webhooks", bytes.NewBuffer([]byte(payload))) + req, err := http.NewRequest("POST", "http://127.0.0.1:3009/webhooks", bytes.NewBuffer([]byte(payload))) req.Header.Set("Content-Type", "application/json") req.Header.Set("X-Github-Event", "public") req.Header.Set("X-Hub-Signature", "sha1=73edb2a8c69c1ac35efb797ede3dc2cde618c10c") @@ -2617,7 +2619,7 @@ func TestPullRequestReviewCommentEvent(t *testing.T) { } ` - req, err := http.NewRequest("POST", "http://localhost:3009/webhooks", bytes.NewBuffer([]byte(payload))) + req, err := http.NewRequest("POST", "http://127.0.0.1:3009/webhooks", bytes.NewBuffer([]byte(payload))) req.Header.Set("Content-Type", "application/json") req.Header.Set("X-Github-Event", "pull_request_review_comment") req.Header.Set("X-Hub-Signature", "sha1=a9ece15dbcbb85fa5f00a0bf409494af2cbc5b60") @@ -3049,7 +3051,7 @@ func TestPullRequestEvent(t *testing.T) { } ` - req, err := http.NewRequest("POST", "http://localhost:3009/webhooks", bytes.NewBuffer([]byte(payload))) + req, err := http.NewRequest("POST", "http://127.0.0.1:3009/webhooks", bytes.NewBuffer([]byte(payload))) req.Header.Set("Content-Type", "application/json") req.Header.Set("X-Github-Event", "pull_request") req.Header.Set("X-Hub-Signature", "sha1=5b342f365078abd366111158b17a8edf5b41ef2a") @@ -3230,7 +3232,7 @@ func TestPushEvent(t *testing.T) { } ` - req, err := http.NewRequest("POST", "http://localhost:3009/webhooks", bytes.NewBuffer([]byte(payload))) + req, err := http.NewRequest("POST", "http://127.0.0.1:3009/webhooks", bytes.NewBuffer([]byte(payload))) req.Header.Set("Content-Type", "application/json") req.Header.Set("X-Github-Event", "push") req.Header.Set("X-Hub-Signature", "sha1=d683a72295b08a42a55bf6fbf2598dc7603e0b98") @@ -3369,7 +3371,7 @@ func TestRepositoryEvent(t *testing.T) { } ` - req, err := http.NewRequest("POST", "http://localhost:3009/webhooks", bytes.NewBuffer([]byte(payload))) + req, err := http.NewRequest("POST", "http://127.0.0.1:3009/webhooks", bytes.NewBuffer([]byte(payload))) req.Header.Set("Content-Type", "application/json") req.Header.Set("X-Github-Event", "repository") req.Header.Set("X-Hub-Signature", "sha1=df442a8af41edd2d42ccdd997938d1d111b0f94e") @@ -3537,7 +3539,7 @@ func TestReleaseEvent(t *testing.T) { } ` - req, err := http.NewRequest("POST", "http://localhost:3009/webhooks", bytes.NewBuffer([]byte(payload))) + req, err := http.NewRequest("POST", "http://127.0.0.1:3009/webhooks", bytes.NewBuffer([]byte(payload))) req.Header.Set("Content-Type", "application/json") req.Header.Set("X-Github-Event", "release") req.Header.Set("X-Hub-Signature", "sha1=e62bb4c51bc7dde195b9525971c2e3aecb394390") @@ -3763,7 +3765,7 @@ func TestStatusEvent(t *testing.T) { } ` - req, err := http.NewRequest("POST", "http://localhost:3009/webhooks", bytes.NewBuffer([]byte(payload))) + req, err := http.NewRequest("POST", "http://127.0.0.1:3009/webhooks", bytes.NewBuffer([]byte(payload))) req.Header.Set("Content-Type", "application/json") req.Header.Set("X-Github-Event", "status") req.Header.Set("X-Hub-Signature", "sha1=3caa5f062a2deb7cce1482314bb9b4c99bf0ab45") @@ -3912,7 +3914,7 @@ func TestTeamAddEvent(t *testing.T) { } ` - req, err := http.NewRequest("POST", "http://localhost:3009/webhooks", bytes.NewBuffer([]byte(payload))) + req, err := http.NewRequest("POST", "http://127.0.0.1:3009/webhooks", bytes.NewBuffer([]byte(payload))) req.Header.Set("Content-Type", "application/json") req.Header.Set("X-Github-Event", "team_add") req.Header.Set("X-Hub-Signature", "sha1=5f3953476e270b79cc6763780346110da880609a") @@ -4041,7 +4043,7 @@ func TestWatchEvent(t *testing.T) { } ` - req, err := http.NewRequest("POST", "http://localhost:3009/webhooks", bytes.NewBuffer([]byte(payload))) + req, err := http.NewRequest("POST", "http://127.0.0.1:3009/webhooks", bytes.NewBuffer([]byte(payload))) req.Header.Set("Content-Type", "application/json") req.Header.Set("X-Github-Event", "watch") req.Header.Set("X-Hub-Signature", "sha1=a317bcfe69ccb8bece74c20c7378e5413c4772f1") diff --git a/webhooks_test.go b/webhooks_test.go index 0b3f0c2..049a345 100644 --- a/webhooks_test.go +++ b/webhooks_test.go @@ -6,6 +6,7 @@ import ( "net/http" "os" "testing" + "time" . "gopkg.in/go-playground/assert.v1" ) @@ -48,11 +49,12 @@ func TestMain(m *testing.M) { func TestRun(t *testing.T) { - go Run(fakeHook, ":3006", "/webhooks") + go Run(fakeHook, "127.0.0.1:3006", "/webhooks") + time.Sleep(5000) payload := "{}" - req, err := http.NewRequest("POST", "http://localhost:3006/webhooks", bytes.NewBuffer([]byte(payload))) + req, err := http.NewRequest("POST", "http://127.0.0.1:3006/webhooks", bytes.NewBuffer([]byte(payload))) req.Header.Set("Content-Type", "application/json") Equal(t, err, nil) @@ -68,7 +70,7 @@ func TestRun(t *testing.T) { // While HTTP Server is running test some bad input // Test BAD URL - req, err = http.NewRequest("POST", "http://localhost:3006", bytes.NewBuffer([]byte(payload))) + req, err = http.NewRequest("POST", "http://127.0.0.1:3006", bytes.NewBuffer([]byte(payload))) req.Header.Set("Content-Type", "application/json") Equal(t, err, nil) @@ -81,7 +83,7 @@ func TestRun(t *testing.T) { Equal(t, resp.StatusCode, http.StatusNotFound) // Test BAD METHOD - req, err = http.NewRequest("GET", "http://localhost:3006/webhooks", bytes.NewBuffer([]byte(payload))) + req, err = http.NewRequest("GET", "http://127.0.0.1:3006/webhooks", bytes.NewBuffer([]byte(payload))) req.Header.Set("Content-Type", "application/json") Equal(t, err, nil) @@ -96,12 +98,13 @@ func TestRun(t *testing.T) { func TestRunServer(t *testing.T) { - server := &http.Server{Addr: ":3007", Handler: nil} + server := &http.Server{Addr: "127.0.0.1:3007", Handler: nil} go RunServer(server, fakeHook, "/webhooks") + time.Sleep(5000) payload := "{}" - req, err := http.NewRequest("POST", "http://localhost:3007/webhooks", bytes.NewBuffer([]byte(payload))) + req, err := http.NewRequest("POST", "http://127.0.0.1:3007/webhooks", bytes.NewBuffer([]byte(payload))) req.Header.Set("Content-Type", "application/json") Equal(t, err, nil) @@ -117,7 +120,7 @@ func TestRunServer(t *testing.T) { // While HTTP Server is running test some bad input // Test BAD URL - req, err = http.NewRequest("POST", "http://localhost:3007", bytes.NewBuffer([]byte(payload))) + req, err = http.NewRequest("POST", "http://127.0.0.1:3007", bytes.NewBuffer([]byte(payload))) req.Header.Set("Content-Type", "application/json") Equal(t, err, nil) @@ -130,7 +133,7 @@ func TestRunServer(t *testing.T) { Equal(t, resp.StatusCode, http.StatusNotFound) // Test BAD METHOD - req, err = http.NewRequest("GET", "http://localhost:3007/webhooks", bytes.NewBuffer([]byte(payload))) + req, err = http.NewRequest("GET", "http://127.0.0.1:3007/webhooks", bytes.NewBuffer([]byte(payload))) req.Header.Set("Content-Type", "application/json") Equal(t, err, nil) @@ -172,17 +175,18 @@ D2lWusoe2/nEqfDVVWGWlyJ7yOmqaVm/iNUN9B2N2g== -----END RSA PRIVATE KEY----- ` - server := &http.Server{Addr: ":3008", Handler: nil, TLSConfig: &tls.Config{}} + server := &http.Server{Addr: "127.0.0.1:3008", Handler: nil, TLSConfig: &tls.Config{}} server.TLSConfig.Certificates = make([]tls.Certificate, 1) server.TLSConfig.Certificates[0], err = tls.X509KeyPair([]byte(cert), []byte(key)) Equal(t, err, nil) go RunTLSServer(server, fakeHook, "/webhooks") + time.Sleep(5000) payload := "{}" - req, err := http.NewRequest("POST", "https://localhost:3008/webhooks", bytes.NewBuffer([]byte(payload))) + req, err := http.NewRequest("POST", "https://127.0.0.1:3008/webhooks", bytes.NewBuffer([]byte(payload))) req.Header.Set("Content-Type", "application/json") Equal(t, err, nil) @@ -202,7 +206,7 @@ D2lWusoe2/nEqfDVVWGWlyJ7yOmqaVm/iNUN9B2N2g== // While HTTP Server is running test some bad input // Test BAD URL - req, err = http.NewRequest("POST", "https://localhost:3008", bytes.NewBuffer([]byte(payload))) + req, err = http.NewRequest("POST", "https://127.0.0.1:3008", bytes.NewBuffer([]byte(payload))) req.Header.Set("Content-Type", "application/json") Equal(t, err, nil) @@ -215,7 +219,7 @@ D2lWusoe2/nEqfDVVWGWlyJ7yOmqaVm/iNUN9B2N2g== Equal(t, resp.StatusCode, http.StatusNotFound) // Test BAD METHOD - req, err = http.NewRequest("GET", "https://localhost:3008/webhooks", bytes.NewBuffer([]byte(payload))) + req, err = http.NewRequest("GET", "https://127.0.0.1:3008/webhooks", bytes.NewBuffer([]byte(payload))) req.Header.Set("Content-Type", "application/json") Equal(t, err, nil)