From 4a754524611a19d8b9b68d05c103c1cd2110d2e2 Mon Sep 17 00:00:00 2001 From: Reuben Thomas-Davis Date: Mon, 16 Mar 2020 19:49:59 +0000 Subject: [PATCH] avoid accessing Request in webhook.go until possible error due to invalid url has been handled --- cmd/git-sync/webhook.go | 2 +- cmd/git-sync/webhook_test.go | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/cmd/git-sync/webhook.go b/cmd/git-sync/webhook.go index 498b6fa..267996d 100644 --- a/cmd/git-sync/webhook.go +++ b/cmd/git-sync/webhook.go @@ -72,10 +72,10 @@ func (w *Webhook) Send(hash string) { func (w *Webhook) Do(hash string) error { req, err := http.NewRequest(w.Method, w.URL, nil) - req.Header.Set("Gitsync-Hash", hash) if err != nil { return err } + req.Header.Set("Gitsync-Hash", hash) ctx, cancel := context.WithTimeout(context.Background(), w.Timeout) defer cancel() diff --git a/cmd/git-sync/webhook_test.go b/cmd/git-sync/webhook_test.go index 04f27bc..ab70358 100644 --- a/cmd/git-sync/webhook_test.go +++ b/cmd/git-sync/webhook_test.go @@ -3,6 +3,7 @@ package main import ( "fmt" "testing" + "time" ) const ( @@ -62,3 +63,20 @@ func TestWebhookData(t *testing.T) { } }) } + +func TestDo(t *testing.T) { + t.Run("test invalid urls are handled", func(t *testing.T) { + wh := Webhook{ + URL: ":http://localhost:601426/hooks/webhook", + Method: "POST", + Success: 200, + Timeout: time.Second, + Backoff: time.Second * 3, + Data: NewWebhookData(), + } + err := wh.Do("hash") + if err == nil { + t.Fatalf("expected error for invalid url but got none") + } + }) +}