Skip to content

Commit

Permalink
Add IsHTTPBatch
Browse files Browse the repository at this point in the history
IsHTTPBatch is a convenience function such that an end user could
determine if a request or response is batch or not, and process it as
such.

Signed-off-by: Mark Mandel <markmandel@google.com>
  • Loading branch information
markmandel committed Jan 10, 2023
1 parent f5d82ff commit c55cd83
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 0 deletions.
3 changes: 3 additions & 0 deletions v2/alias.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,11 +135,14 @@ var (
ToMessage = binding.ToMessage

// Event Creation

NewEventFromHTTPRequest = http.NewEventFromHTTPRequest
NewEventFromHTTPResponse = http.NewEventFromHTTPResponse
NewEventsFromHTTPRequest = http.NewEventsFromHTTPRequest
NewEventsFromHTTPResponse = http.NewEventsFromHTTPResponse
NewHTTPRequestFromEvents = http.NewHTTPRequestFromEvents
NewHTTPRequestFromEvent = http.NewHTTPRequestFromEvent
IsHTTPBatch = http.IsHTTPBatch

// HTTP Messages

Expand Down
6 changes: 6 additions & 0 deletions v2/protocol/http/utility.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,9 @@ func NewHTTPRequestFromEvents(ctx context.Context, url string, events []event.Ev

return request, nil
}

// IsHTTPBatch returns of the current http.Request or http.Response is a batch event operation, by checking the
// header `Content-Type` value.
func IsHTTPBatch(header nethttp.Header) bool {
return header.Get(ContentType) == event.ApplicationCloudEventsBatchJSON
}
12 changes: 12 additions & 0 deletions v2/protocol/http/utility_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/cloudevents/sdk-go/v2/event"
"github.com/cloudevents/sdk-go/v2/test"
"github.com/google/uuid"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -235,3 +236,14 @@ func TestNewHTTPRequestFromEvents(t *testing.T) {
require.NoError(t, err)
require.Equal(t, events, result)
}

func TestIsHTTPBatch(t *testing.T) {
header := http.Header{}
assert.False(t, IsHTTPBatch(header))

header.Set(ContentType, event.ApplicationJSON)
assert.False(t, IsHTTPBatch(header))

header.Set(ContentType, event.ApplicationCloudEventsBatchJSON)
assert.True(t, IsHTTPBatch(header))
}

0 comments on commit c55cd83

Please sign in to comment.