diff --git a/docs/eventlisteners.md b/docs/eventlisteners.md index cdfd230f7..bcd9aafad 100644 --- a/docs/eventlisteners.md +++ b/docs/eventlisteners.md @@ -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 ``` @@ -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 diff --git a/pkg/sink/sink.go b/pkg/sink/sink.go index d5a0ec803..7631550e9 100644 --- a/pkg/sink/sink.go +++ b/pkg/sink/sink.go @@ -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)), } @@ -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), } diff --git a/pkg/sink/sink_test.go b/pkg/sink/sink_test.go index 8bda23e71..815d5c8c1 100644 --- a/pkg/sink/sink_test.go +++ b/pkg/sink/sink_test.go @@ -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) @@ -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) + } + }) } }