Skip to content

Commit

Permalink
Add WitHost Option for HTTP Protocols
Browse files Browse the repository at this point in the history
Signed-off-by: Jaxton Winder <[email protected]>

Improve error message for nil protocol

Signed-off-by: Jaxton Winder <[email protected]>

Add unit test for WithHost

Signed-off-by: Jaxton Winder <[email protected]>
Signed-off-by: James Lewis <[email protected]>
  • Loading branch information
jaxtonw authored and JamesBLewis committed Jul 5, 2024
1 parent 8f2c924 commit 0fcf1fb
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 0 deletions.
1 change: 1 addition & 0 deletions v2/alias.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 20 additions & 0 deletions v2/protocol/http/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
71 changes: 71 additions & 0 deletions v2/protocol/http/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 0fcf1fb

Please sign in to comment.