Skip to content

Commit

Permalink
Merge pull request #4 from Konboi/feature/support-form-urleccoded
Browse files Browse the repository at this point in the history
Support content-type:application/x-www-form-urlencoded #3
  • Loading branch information
Konboi committed Nov 14, 2014
2 parents 88a0240 + 35967c7 commit a36e147
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
21 changes: 18 additions & 3 deletions ghooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

const (
VERSION = 0.1
VERSION = 0.2
)

type Server struct {
Expand Down Expand Up @@ -69,13 +69,28 @@ func Reciver(w http.ResponseWriter, req *http.Request) {
defer req.Body.Close()

var payload interface{}
decoder := json.NewDecoder(req.Body)
var decoder *json.Decoder

if strings.Contains(req.Header.Get("Content-Type"), "application/json") {

decoder = json.NewDecoder(req.Body)

} else if strings.Contains(req.Header.Get("Content-Type"), "application/x-www-form-urlencoded") {

err := req.ParseForm()
if err != nil {
http.Error(w, "Bad Request", http.StatusBadRequest)
return
}
p := req.FormValue("payload")
decoder = json.NewDecoder(strings.NewReader(p))
}

err := decoder.Decode(&payload)
if err != nil {
http.Error(w, "Bad Request", http.StatusBadRequest)
return
}

Emmit(event, payload)
w.WriteHeader(http.StatusOK)
}
13 changes: 13 additions & 0 deletions ghooks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ func TestReciver(t *testing.T) {
json_string := `{"fuga": "hoge", "foo": { "bar": "boo" }}`
req, _ = http.NewRequest("POST", "/", strings.NewReader(json_string))
req.Header.Set("X-GitHub-Event", "hoge")
req.Header.Set("Content-Type", "application/json")
w = httptest.NewRecorder()
Reciver(w, req)
if w.Code != 200 {
Expand All @@ -93,9 +94,21 @@ func TestReciver(t *testing.T) {
json_string = `{"fuga": "hoge", "foo": { "bar", "boo" }}`
req, _ = http.NewRequest("POST", "/", strings.NewReader(json_string))
req.Header.Set("X-GitHub-Event", "hoge")
req.Header.Set("Content-Type", "application/json")
w = httptest.NewRecorder()
Reciver(w, req)
if w.Code == 200 {
t.Fatalf("Should not be 200; received %d", w.Code)
}

json_string = `{"fuga": "hoge", "foo": { "bar": "boo" }}`
req, _ = http.NewRequest("POST", "/", strings.NewReader("payload="+json_string))
req.Header.Set("X-GitHub-Event", "hoge")
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
w = httptest.NewRecorder()
Reciver(w, req)
if w.Code != 200 {
t.Fatalf("Not return 200; received %d", w.Code)
}

}

0 comments on commit a36e147

Please sign in to comment.