Skip to content

Commit c93876b

Browse files
author
Dean Karn
authored
Merge pull request #10 from go-playground/add-handler-support
Add handler support
2 parents fccbba5 + 0926003 commit c93876b

File tree

4 files changed

+105
-13
lines changed

4 files changed

+105
-13
lines changed

.travis.yml

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
language: go
2+
go:
3+
- 1.7.5
4+
- 1.8.1
5+
- tip
6+
matrix:
7+
allow_failures:
8+
- go: tip
9+
10+
notifications:
11+
email:
12+
recipients: [email protected]
13+
on_success: change
14+
on_failure: always
15+
16+
before_install:
17+
- go get -u github.com/go-playground/overalls
18+
- go get -u github.com/mattn/goveralls
19+
- go get -u golang.org/x/tools/cmd/cover
20+
- go get -u github.com/golang/lint/golint
21+
- go get -u github.com/gordonklaus/ineffassign
22+
- mkdir -p $GOPATH/src/gopkg.in
23+
- ln -s $GOPATH/src/github.com/$TRAVIS_REPO_SLUG $GOPATH/src/gopkg.in/webhooks.v2
24+
- ln -s $GOPATH/src/github.com/$TRAVIS_REPO_SLUG $GOPATH/src/gopkg.in/webhooks.v3
25+
26+
before_script:
27+
- go vet ./...
28+
29+
script:
30+
- gofmt -d -s .
31+
- golint ./...
32+
- ineffassign ./
33+
- go test -v ./...
34+
- go test -race
35+
36+
after_success: |
37+
[ $TRAVIS_GO_VERSION = 1.8.1 ] &&
38+
overalls -project="github.com/go-playground/webhooks" -covermode=count -ignore=.git,examples -debug &&
39+
goveralls -coverprofile=overalls.coverprofile -service travis-ci -repotoken $COVERALLS_TOKEN

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Library webhooks
22
================
3-
<img align="right" src="https://raw.githubusercontent.com/go-playground/webhooks/v3/logo.png">![Project status](https://img.shields.io/badge/version-3.0.0-green.svg)
4-
[![Build Status](https://semaphoreci.com/api/v1/joeybloggs/webhooks/branches/v3/badge.svg)](https://semaphoreci.com/joeybloggs/webhooks)
3+
<img align="right" src="https://raw.githubusercontent.com/go-playground/webhooks/v3/logo.png">![Project status](https://img.shields.io/badge/version-3.1.0-green.svg)
4+
[![Build Status](https://travis-ci.org/go-playground/webhooks.svg?branch=v3)](https://travis-ci.org/go-playground/webhooks)
55
[![Coverage Status](https://coveralls.io/repos/go-playground/webhooks/badge.svg?branch=v3&service=github)](https://coveralls.io/github/go-playground/webhooks?branch=v3)
66
[![Go Report Card](https://goreportcard.com/badge/go-playground/webhooks)](https://goreportcard.com/report/go-playground/webhooks)
77
[![GoDoc](https://godoc.org/gopkg.in/go-playground/webhooks.v3?status.svg)](https://godoc.org/gopkg.in/go-playground/webhooks.v3)

webhooks.go

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,18 +35,27 @@ type Webhook interface {
3535
}
3636

3737
type server struct {
38-
hook Webhook
39-
path string
38+
hook Webhook
39+
path string
40+
includePathCheck bool
4041
}
4142

4243
// ProcessPayloadFunc is a common function for payload return values
4344
type ProcessPayloadFunc func(payload interface{}, header Header)
4445

46+
// Handler returns the webhook http.Handler for use in your own Mux implementation
47+
func Handler(hook Webhook) http.Handler {
48+
return &server{
49+
hook: hook,
50+
}
51+
}
52+
4553
// Run runs a server
4654
func Run(hook Webhook, addr string, path string) error {
4755
srv := &server{
48-
hook: hook,
49-
path: path,
56+
hook: hook,
57+
path: path,
58+
includePathCheck: true,
5059
}
5160

5261
s := &http.Server{Addr: addr, Handler: srv}
@@ -58,8 +67,9 @@ func Run(hook Webhook, addr string, path string) error {
5867
func RunServer(s *http.Server, hook Webhook, path string) error {
5968

6069
srv := &server{
61-
hook: hook,
62-
path: path,
70+
hook: hook,
71+
path: path,
72+
includePathCheck: true,
6373
}
6474

6575
s.Handler = srv
@@ -74,8 +84,9 @@ func RunServer(s *http.Server, hook Webhook, path string) error {
7484
func RunTLSServer(s *http.Server, hook Webhook, path string) error {
7585

7686
srv := &server{
77-
hook: hook,
78-
path: path,
87+
hook: hook,
88+
path: path,
89+
includePathCheck: true,
7990
}
8091

8192
s.Handler = srv
@@ -91,9 +102,12 @@ func (s *server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
91102
http.Error(w, "405 Method not allowed", http.StatusMethodNotAllowed)
92103
return
93104
}
94-
if r.URL.Path != s.path {
95-
http.Error(w, "404 Not found", http.StatusNotFound)
96-
return
105+
106+
if s.includePathCheck {
107+
if r.URL.Path != s.path {
108+
http.Error(w, "404 Not found", http.StatusNotFound)
109+
return
110+
}
97111
}
98112

99113
s.hook.ParsePayload(w, r)

webhooks_test.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import (
88
"testing"
99
"time"
1010

11+
"net/http/httptest"
12+
1113
. "gopkg.in/go-playground/assert.v1"
1214
)
1315

@@ -47,6 +49,43 @@ func TestMain(m *testing.M) {
4749
// teardown
4850
}
4951

52+
func TestHandler(t *testing.T) {
53+
54+
mux := http.NewServeMux()
55+
mux.Handle("/webhooks", Handler(fakeHook))
56+
57+
s := httptest.NewServer(Handler(fakeHook))
58+
defer s.Close()
59+
60+
payload := "{}"
61+
62+
req, err := http.NewRequest("POST", s.URL+"/webhooks", bytes.NewBuffer([]byte(payload)))
63+
req.Header.Set("Content-Type", "application/json")
64+
65+
Equal(t, err, nil)
66+
67+
client := &http.Client{}
68+
resp, err := client.Do(req)
69+
Equal(t, err, nil)
70+
71+
defer resp.Body.Close()
72+
73+
Equal(t, resp.StatusCode, http.StatusOK)
74+
75+
// Test BAD METHOD
76+
req, err = http.NewRequest("GET", s.URL+"/webhooks", bytes.NewBuffer([]byte(payload)))
77+
req.Header.Set("Content-Type", "application/json")
78+
79+
Equal(t, err, nil)
80+
81+
resp, err = client.Do(req)
82+
Equal(t, err, nil)
83+
84+
defer resp.Body.Close()
85+
86+
Equal(t, resp.StatusCode, http.StatusMethodNotAllowed)
87+
}
88+
5089
func TestRun(t *testing.T) {
5190

5291
go Run(fakeHook, "127.0.0.1:3006", "/webhooks")

0 commit comments

Comments
 (0)