Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass Content-Type Header to Interceptor #1615

Merged
merged 1 commit into from
May 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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