mirror of
https://github.com/caddyserver/caddy.git
synced 2025-10-22 09:13:18 +00:00
Added webhook functionality to the git middleware.
The webhook providers reside behind a small interface which determines if a provider should run. If a provider should run it delegates responsibility of the request to the provider. ghdeploy initial commit Added webhook functionality to the git middleware. The webhook providers reside behind a small interface which determines if a provider should run. If a provider should run it delegates responsibility of the request to the provider. Add tests Remove old implementation Fix inconsistency with git interval pulling. Remove '\n' from logging statements and put the initial pull into a startup function
This commit is contained in:
parent
4852f0580b
commit
b4780a41d3
5 changed files with 303 additions and 6 deletions
63
middleware/git/webhook/github_hook_test.go
Normal file
63
middleware/git/webhook/github_hook_test.go
Normal file
|
@ -0,0 +1,63 @@
|
|||
package webhook
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"github.com/mholt/caddy/middleware/git"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestGithubDeployPush(t *testing.T) {
|
||||
repo := &git.Repo{Branch: "master", HookUrl: "/github_deploy", HookSecret: "supersecret"}
|
||||
ghHook := GithubHook{}
|
||||
|
||||
for i, test := range []struct {
|
||||
body string
|
||||
event string
|
||||
responseBody string
|
||||
code int
|
||||
}{
|
||||
{"", "", "", 400},
|
||||
{"", "push", "", 400},
|
||||
{pushBodyOther, "push", "", 200},
|
||||
{pushBodyPartial, "push", "", 400},
|
||||
{"", "release", "", 400},
|
||||
{"", "ping", "pong", 200},
|
||||
} {
|
||||
|
||||
req, err := http.NewRequest("POST", "/github_deploy", bytes.NewBuffer([]byte(test.body)))
|
||||
if err != nil {
|
||||
t.Fatalf("Test %v: Could not create HTTP request: %v", i, err)
|
||||
}
|
||||
|
||||
if test.event != "" {
|
||||
req.Header.Add("X-Github-Event", test.event)
|
||||
}
|
||||
|
||||
rec := httptest.NewRecorder()
|
||||
|
||||
code, err := ghHook.Handle(rec, req, repo)
|
||||
|
||||
if code != test.code {
|
||||
t.Errorf("Test %d: Expected response code to be %d but was %d", i, test.code, code)
|
||||
}
|
||||
|
||||
if rec.Body.String() != test.responseBody {
|
||||
t.Errorf("Test %d: Expected response body to be '%v' but was '%v'", i, test.responseBody, rec.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
var pushBodyPartial = `
|
||||
{
|
||||
"ref": ""
|
||||
}
|
||||
`
|
||||
|
||||
var pushBodyOther = `
|
||||
{
|
||||
"ref": "refs/heads/some-other-branch"
|
||||
}
|
||||
`
|
Loading…
Add table
Add a link
Reference in a new issue