diff --git a/v2/alias.go b/v2/alias.go index 2fbfaa9a7..0f484b33b 100644 --- a/v2/alias.go +++ b/v2/alias.go @@ -173,6 +173,7 @@ var ( WithTarget = http.WithTarget WithHeader = http.WithHeader + WithHost = http.WithHost WithShutdownTimeout = http.WithShutdownTimeout //WithEncoding = http.WithEncoding //WithStructuredEncoding = http.WithStructuredEncoding // TODO: expose new way diff --git a/v2/protocol/http/options.go b/v2/protocol/http/options.go index 359095004..91a45ce36 100644 --- a/v2/protocol/http/options.go +++ b/v2/protocol/http/options.go @@ -72,6 +72,26 @@ func WithHeader(key, value string) Option { } } +// WithHost sets the outbound host header for all cloud events when using an HTTP request +func WithHost(value string) Option { + return func(p *Protocol) error { + if p == nil { + return fmt.Errorf("http host option can not set nil protocol") + } + value = strings.TrimSpace(value) + if value != "" { + if p.RequestTemplate == nil { + p.RequestTemplate = &nethttp.Request{ + Method: nethttp.MethodPost, + } + } + p.RequestTemplate.Host = value + return nil + } + return fmt.Errorf("http host option was empty string") + } +} + // WithShutdownTimeout sets the shutdown timeout when the http server is being shutdown. func WithShutdownTimeout(timeout time.Duration) Option { return func(p *Protocol) error { diff --git a/v2/protocol/http/options_test.go b/v2/protocol/http/options_test.go index 21cb841d2..58d12d91a 100644 --- a/v2/protocol/http/options_test.go +++ b/v2/protocol/http/options_test.go @@ -271,6 +271,77 @@ func TestWithHeader(t *testing.T) { } } +func TestWithHost(t *testing.T) { + testCases := map[string]struct { + t *Protocol + value string + want *Protocol + wantErr string + }{ + "valid host": { + t: &Protocol{ + RequestTemplate: &http.Request{}, + }, + value: "test", + want: &Protocol{ + RequestTemplate: &http.Request{ + Host: "test", + }, + }, + }, + "valid host, unset req": { + t: &Protocol{}, + value: "test", + want: &Protocol{ + RequestTemplate: &http.Request{ + Method: http.MethodPost, + Host: "test", + }, + }, + }, + "empty host value": { + t: &Protocol{ + RequestTemplate: &http.Request{}, + }, + wantErr: `http host option was empty string`, + }, + "whitespace key": { + t: &Protocol{ + RequestTemplate: &http.Request{}, + }, + value: " \t\n", + wantErr: `http host option was empty string`, + }, + "nil protocol": { + wantErr: `http host option can not set nil protocol`, + }, + } + for n, tc := range testCases { + t.Run(n, func(t *testing.T) { + + err := tc.t.applyOptions(WithHost(tc.value)) + + if tc.wantErr != "" || err != nil { + var gotErr string + if err != nil { + gotErr = err.Error() + } + if diff := cmp.Diff(tc.wantErr, gotErr); diff != "" { + t.Errorf("unexpected error (-want, +got) = %v", diff) + } + return + } + + got := tc.t + + if diff := cmp.Diff(tc.want, got, + cmpopts.IgnoreUnexported(Protocol{}), cmpopts.IgnoreUnexported(http.Request{})); diff != "" { + t.Errorf("unexpected (-want, +got) = %v", diff) + } + }) + } +} + func TestWithShutdownTimeout(t *testing.T) { testCases := map[string]struct { t *Protocol