Skip to content

Commit

Permalink
signal cmt
Browse files Browse the repository at this point in the history
  • Loading branch information
Xavrax committed Dec 10, 2024
1 parent 9cf2949 commit 69fcff6
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
35 changes: 35 additions & 0 deletions signal_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,14 @@ func (b *signalBuilder) QueryParam(queryParam map[string]string) *signalBuilder
return b
}

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

return b
}

// Execute runs the Signal request.
func (b *signalBuilder) Execute() (*SignalResponse, StatusResponse, error) {
rawJSON, status, err := executeRequest(b.opts)
Expand All @@ -86,6 +94,25 @@ type signalOpts struct {
UsePost bool
QueryParam map[string]string
Transport http.RoundTripper
CustomMessageType string
}

func (o *signalOpts) 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 *signalOpts) validate() error {
Expand All @@ -97,6 +124,10 @@ func (o *signalOpts) validate() error {
return newValidationError(o, StrMissingPubKey)
}

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

return nil
}

Expand Down Expand Up @@ -130,6 +161,10 @@ func (o *signalOpts) buildQuery() (*url.Values, error) {

SetQueryParam(q, o.QueryParam)

if len(o.CustomMessageType) > 0 {
q.Set("custom_message_type", o.CustomMessageType)
}

return q, nil
}

Expand Down
15 changes: 14 additions & 1 deletion signal_request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ func AssertSuccessSignalGet(t *testing.T, channel string, checkQueryParam bool)
opts.Channel = channel
opts.Message = msgMap
opts.QueryParam = queryParam
opts.CustomMessageType = "custom"

path, err := opts.buildPath()
assert.Nil(err)
Expand All @@ -43,8 +44,8 @@ func AssertSuccessSignalGet(t *testing.T, channel string, checkQueryParam bool)
u, _ := opts.buildQuery()
assert.Equal("v1", u.Get("q1"))
assert.Equal("v2", u.Get("q2"))
assert.Equal("custom", u.Get("custom_message_type"))
}

}

func TestSignalPath(t *testing.T) {
Expand Down Expand Up @@ -144,3 +145,15 @@ func TestSignalResponseValuePass(t *testing.T) {
_, _, err := newSignalResponse(jsonBytes, opts, StatusResponse{})
assert.Nil(err)
}

func TestSignalCustomMessageTypeValidation(t *testing.T) {
assert := assert.New(t)
pn := NewPubNub(NewDemoConfig())
opts := newSignalOpts(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())
}

0 comments on commit 69fcff6

Please sign in to comment.