Skip to content

Commit

Permalink
Ensure Interceptor request uses HTTP POST.
Browse files Browse the repository at this point in the history
Fixes #437
  • Loading branch information
wlynch authored and tekton-robot committed Feb 25, 2020
1 parent 7345759 commit 9a7c1c0
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 20 deletions.
3 changes: 2 additions & 1 deletion docs/eventlisteners.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ contains the default values defined in
To access logs for the EventListener sink, you can query for pods with the
`eventlistener` label set to the name of your EventListener resource:

```shell script
```shell
kubectl get pods --selector eventlistener=my-eventlistener
```

Expand Down Expand Up @@ -186,6 +186,7 @@ To be an Event Interceptor, a Kubernetes object should:

- Be fronted by a regular Kubernetes v1 Service over port 80
- Accept JSON payloads over HTTP
- Accept HTTP POST requests with JSON payloads.
- Return a HTTP 200 OK Status if the EventListener should continue processing
the event
- Return a JSON body back. This will be used by the EventListener as the event
Expand Down
2 changes: 2 additions & 0 deletions pkg/sink/sink.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ func (r Sink) executeInterceptors(t *triggersv1.EventListenerTrigger, in *http.R

// The request body to the first interceptor in the chain should be the received event body.
request := &http.Request{
Method: http.MethodPost,
Header: in.Header,
Body: ioutil.NopCloser(bytes.NewBuffer(event)),
}
Expand Down Expand Up @@ -190,6 +191,7 @@ func (r Sink) executeInterceptors(t *triggersv1.EventListenerTrigger, in *http.R
// Set the next request to be the output of the last response to enable
// request chaining.
request = &http.Request{
Method: http.MethodPost,
Header: resp.Header,
Body: ioutil.NopCloser(resp.Body),
}
Expand Down
56 changes: 37 additions & 19 deletions pkg/sink/sink_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -643,6 +643,10 @@ type sequentialInterceptor struct {

func (f *sequentialInterceptor) ServeHTTP(w http.ResponseWriter, r *http.Request) {
f.called = true
if r.Method != http.MethodPost {
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
var data map[string]int
if err := json.NewDecoder(r.Body).Decode(&data); err != nil {
w.WriteHeader(http.StatusInternalServerError)
Expand Down Expand Up @@ -697,25 +701,39 @@ func TestExecuteInterceptor(t *testing.T) {
Interceptors: []*triggersv1.EventInterceptor{a, a},
}

req, err := http.NewRequest(http.MethodPost, "/", nil)
if err != nil {
t.Fatalf("http.NewRequest: %v", err)
}
resp, header, err := r.executeInterceptors(trigger, req, []byte(`{}`), "", logger)
if err != nil {
t.Fatalf("executeInterceptors: %v", err)
}

var got map[string]int
if err := json.Unmarshal(resp, &got); err != nil {
t.Fatalf("json.Unmarshal: %v", err)
}
want := map[string]int{"i": 2}
if diff := cmp.Diff(want, got); diff != "" {
t.Errorf("Body: -want +got: %s", diff)
}
if diff := cmp.Diff([]string{"1", "2"}, header["Foo"]); diff != "" {
t.Errorf("Header: -want +got: %s", diff)
for _, method := range []string{
http.MethodGet,
http.MethodHead,
http.MethodPost,
http.MethodPut,
http.MethodPatch,
http.MethodDelete,
http.MethodConnect,
http.MethodOptions,
http.MethodTrace,
} {
t.Run(method, func(t *testing.T) {
req, err := http.NewRequest(method, "/", nil)
if err != nil {
t.Fatalf("http.NewRequest: %v", err)
}
resp, header, err := r.executeInterceptors(trigger, req, []byte(`{}`), "", logger)
if err != nil {
t.Fatalf("executeInterceptors: %v", err)
}

var got map[string]int
if err := json.Unmarshal(resp, &got); err != nil {
t.Fatalf("json.Unmarshal: %v", err)
}
want := map[string]int{"i": 2}
if diff := cmp.Diff(want, got); diff != "" {
t.Errorf("Body: -want +got: %s", diff)
}
if diff := cmp.Diff([]string{"1", "2"}, header["Foo"]); diff != "" {
t.Errorf("Header: -want +got: %s", diff)
}
})
}
}

Expand Down

0 comments on commit 9a7c1c0

Please sign in to comment.