bindings: support rabbitmq send any content types (#1147)

Signed-off-by: Long <long0dai@foxmail.com>
This commit is contained in:
Long Dai 2021-09-18 01:21:41 +08:00 committed by GitHub
parent e1b52376e9
commit 189d2d6717
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 46 additions and 1 deletions

View File

@ -99,6 +99,12 @@ func (r *RabbitMQ) Invoke(req *bindings.InvokeRequest) (*bindings.InvokeResponse
Body: req.Data,
}
contentType, ok := contrib_metadata.TryGetContentType(req.Metadata)
if ok {
pub.ContentType = contentType
}
ttl, ok, err := contrib_metadata.TryGetTTL(req.Metadata)
if err != nil {
return nil, err

View File

@ -42,7 +42,7 @@ func (r *InvokeRequest) GetMetadataAsBool(key string) (bool, error) {
return false, nil
}
// GetMetadataAsBool parses metadata as int64
// GetMetadataAsInt64 parses metadata as int64
func (r *InvokeRequest) GetMetadataAsInt64(key string, bitSize int) (int64, error) {
if val, ok := r.Metadata[key]; ok {
intVal, err := strconv.ParseInt(val, 10, bitSize)

View File

@ -23,6 +23,9 @@ const (
// PriorityMetadataKey defines the metadata key for setting a priority
PriorityMetadataKey = "priority"
// ContentType defines the metadata key for the content type
ContentType = "contentType"
)
// TryGetTTL tries to get the ttl as a time.Duration value for pubsub, binding and any other building block.
@ -83,3 +86,11 @@ func IsRawPayload(props map[string]string) (bool, error) {
return false, nil
}
func TryGetContentType(props map[string]string) (string, bool) {
if val, ok := props[ContentType]; ok && val != "" {
return val, true
}
return "", false
}

View File

@ -55,3 +55,31 @@ func TestIsRawPayload(t *testing.T) {
assert.Nil(t, err)
})
}
func TestTryGetContentType(t *testing.T) {
t.Run("Metadata without content type", func(t *testing.T) {
val, ok := TryGetContentType(map[string]string{})
assert.Equal(t, "", val)
assert.Equal(t, false, ok)
})
t.Run("Metadata with empty content type", func(t *testing.T) {
val, ok := TryGetContentType(map[string]string{
"contentType": "",
})
assert.Equal(t, "", val)
assert.Equal(t, false, ok)
})
t.Run("Metadata with corrent content type", func(t *testing.T) {
const contentType = "application/cloudevent+json"
val, ok := TryGetContentType(map[string]string{
"contentType": contentType,
})
assert.Equal(t, contentType, val)
assert.Equal(t, true, ok)
})
}