Skip to content

Commit

Permalink
validation of message type
Browse files Browse the repository at this point in the history
  • Loading branch information
Xavrax committed Dec 9, 2024
1 parent f8dba99 commit 9cf2949
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 1 deletion.
25 changes: 24 additions & 1 deletion publish_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,8 @@ func (b *publishBuilder) QueryParam(queryParam map[string]string) *publishBuilde
return b
}

// CustomMessageType sets the Custom Message Type for the Publish request.
// CustomMessageType sets the User-specified message type string - limited by 3-50 case-sensitive alphanumeric characters
// with only `-` and `_` special characters allowed.
func (b *publishBuilder) CustomMessageType(messageType string) *publishBuilder {
b.opts.CustomMessageType = messageType

Expand All @@ -187,6 +188,24 @@ func (b *publishBuilder) Execute() (*PublishResponse, StatusResponse, error) {
return newPublishResponse(rawJSON, status)
}

func (o *publishOpts) isCustomMessageTypeCorrect() bool {
if len(o.CustomMessageType) == 0 {
return true
}

if len(o.CustomMessageType) < 3 || len(o.CustomMessageType) > 50 {
return false
}

for _, c := range o.CustomMessageType {
if !('a' <= c && 'z' >= c) && !('A' <= c && 'Z' >= c) && c != '-' && c != '_' {
return false
}
}

return true
}

func (o *publishOpts) validate() error {
if o.config().PublishKey == "" {
return newValidationError(o, StrMissingPubKey)
Expand All @@ -204,6 +223,10 @@ func (o *publishOpts) validate() error {
return newValidationError(o, StrMissingMessage)
}

if !o.isCustomMessageTypeCorrect() {
return newValidationError(o, StrInvalidCustomMessageType)
}

return nil
}

Expand Down
14 changes: 14 additions & 0 deletions publish_request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ func AssertSuccessPublishGet2(t *testing.T, expectedString string, message inter
o.TTL(10)
o.ShouldStore(false)
o.DoNotReplicate(true)
o.CustomMessageType("custom")

path, err := o.opts.buildPath()
assert.Nil(err)
Expand All @@ -99,6 +100,7 @@ func AssertSuccessPublishGet2(t *testing.T, expectedString string, message inter
expected.Set("pnsdk", Version)
expected.Set("norep", "true")
expected.Set("store", "0")
expected.Set("custom_message_type", "custom")

h.AssertQueriesEqual(t, expected, query,
[]string{"seqn", "pnsdk", "uuid", "store"}, []string{})
Expand Down Expand Up @@ -532,3 +534,15 @@ func TestPublishValidateSubscribeKey(t *testing.T) {

assert.Equal("pubnub/validation: pubnub: Publish: Missing Subscribe Key", opts.validate().Error())
}

func TestPublishValidateCustomMessageType(t *testing.T) {
assert := assert.New(t)
pn := NewPubNub(NewDemoConfig())
opts := newPublishOpts(pn, pn.ctx)
opts.CustomMessageType = "custom-message_type"
assert.True(opts.isCustomMessageTypeCorrect())
opts.CustomMessageType = "a"
assert.False(opts.isCustomMessageTypeCorrect())
opts.CustomMessageType = "!@#$%^&*("
assert.False(opts.isCustomMessageTypeCorrect())
}
2 changes: 2 additions & 0 deletions pubnub.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ const (
StrMissingFileName = "Missing File Name"
// StrMissingToken shows `Missing PAMv3 token` message
StrMissingToken = "Missing PAMv3 token"
// StrInvalidCustomMessageType shows `Invalid CustomMessageType` message
StrInvalidCustomMessageType = "Invalid CustomMessageType: size different than 3-50 or contains invalid characters"
)

// PubNub No server connection will be established when you create a new PubNub object.
Expand Down

0 comments on commit 9cf2949

Please sign in to comment.