Skip to content

Commit

Permalink
Pass Content-Type Header to Interceptor
Browse files Browse the repository at this point in the history
`Content-Type` should be passed to interceptor while making POST Request. Some
framework in different languages like java require this. So interceptor request
fails with 415 in those.
  • Loading branch information
khrm committed Apr 4, 2024
1 parent 4f2a4c0 commit fb3320c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
4 changes: 4 additions & 0 deletions pkg/interceptors/interceptors.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (

const (
CoreInterceptorsHost = "tekton-triggers-core-interceptors"
ContentType = "application/json"
)

// Interceptor is the interface that all interceptors implement.
Expand Down Expand Up @@ -124,6 +125,9 @@ func Execute(ctx context.Context, client *http.Client, req *triggersv1beta1.Inte
if err != nil {
return nil, err
}

r.Header.Set("Content-Type", ContentType)

res, err := client.Do(r)
if err != nil {
return nil, err
Expand Down
25 changes: 23 additions & 2 deletions pkg/interceptors/interceptors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,21 @@ func TestResolveToURL(t *testing.T) {
})
}

type localT struct {
T *testing.T
}

func (t localT) testHeader(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
contentType := r.Header.Get("Content-Type")
if contentType != interceptors.ContentType {
t.T.Fatalf("Header expected to be: %s but got :%s",
interceptors.ContentType, contentType)
}
next.ServeHTTP(w, r)
})
}

// testServer creates a httptest server with the passed in handler and returns a http.Client that
// can be used to talk to these interceptors
func testServer(t testing.TB, handler http.Handler, d ...*http.Transport) *http.Client {
Expand Down Expand Up @@ -427,7 +442,13 @@ func TestExecute(t *testing.T) {
if err != nil {
t.Fatalf("failed to initialize core interceptors: %v", err)
}
httpClient := testServer(t, coreInterceptors)

lT := localT{
T: t,
}

handler := lT.testHeader(coreInterceptors)
httpClient := testServer(t, handler)
got, err := interceptors.Execute(context.Background(), httpClient, tc.req, tc.url)
if err != nil {
t.Fatalf("Execute() unexpected error: %s", err)
Expand All @@ -446,7 +467,7 @@ func TestExecute_Error(t *testing.T) {
"Content-Type": {"application/json"},
}),
Context: &triggersv1.TriggerContext{
EventURL: "http://someurl.com",
EventURL: "http://somurel.com",
EventID: "abcde",
TriggerID: "namespaces/default/triggers/test-trigger",
},
Expand Down

0 comments on commit fb3320c

Please sign in to comment.