Skip to content

Commit

Permalink
file send cmt
Browse files Browse the repository at this point in the history
  • Loading branch information
Xavrax committed Dec 10, 2024
1 parent 68ad7f0 commit 6cd8d00
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 39 deletions.
41 changes: 32 additions & 9 deletions files_send_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,18 @@ func (b *sendFileBuilder) QueryParam(queryParam map[string]string) *sendFileBuil
// Transport sets the Transport for the sendFile request.
func (b *sendFileBuilder) Transport(tr http.RoundTripper) *sendFileBuilder {
b.opts.Transport = tr

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 *sendFileBuilder) CustomMessageType(messageType string) *sendFileBuilder {
b.opts.CustomMessageType = messageType

return b
}

// Execute runs the sendFile request.
func (b *sendFileBuilder) Execute() (*PNSendFileResponse, StatusResponse, error) {
rawJSON, status, err := executeRequest(b.opts)
Expand All @@ -110,19 +119,24 @@ func (b *sendFileBuilder) Execute() (*PNSendFileResponse, StatusResponse, error)
type sendFileOpts struct {
endpointOpts

Channel string
Name string
Message string
File *os.File
CipherKey string
TTL int
Meta interface{}
ShouldStore bool
QueryParam map[string]string
Channel string
Name string
Message string
File *os.File
CipherKey string
TTL int
Meta interface{}
ShouldStore bool
QueryParam map[string]string
CustomMessageType string

Transport http.RoundTripper
}

func (o *sendFileOpts) isCustomMessageTypeCorrect() bool {
return isCustomMessageTypeValid(o.CustomMessageType)
}

func (o *sendFileOpts) validate() error {
if o.config().SubscribeKey == "" {
return newValidationError(o, StrMissingSubKey)
Expand All @@ -135,6 +149,11 @@ func (o *sendFileOpts) validate() error {
if o.Name == "" {
return newValidationError(o, StrMissingFileName)
}

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

return nil
}

Expand All @@ -149,6 +168,10 @@ func (o *sendFileOpts) 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
14 changes: 14 additions & 0 deletions files_send_file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ func AssertSendFile(t *testing.T, checkQueryParam, testContext bool) {
channel := "chan"
o.Channel(channel)
o.QueryParam(queryParam)
o.CustomMessageType("custom")

path, err := o.opts.buildPath()
assert.Nil(err)
Expand All @@ -45,6 +46,7 @@ func AssertSendFile(t *testing.T, checkQueryParam, testContext bool) {
u, _ := o.opts.buildQuery()
assert.Equal("v1", u.Get("q1"))
assert.Equal("v2", u.Get("q2"))
assert.Equal("custom", u.Get("custom_message_type"))
}

}
Expand All @@ -66,3 +68,15 @@ func TestSendFileResponseValueError(t *testing.T) {
_, _, err := newPNSendFileResponse(jsonBytes, opts, StatusResponse{})
assert.Equal("pubnub/parsing: Error unmarshalling response: {s}", err.Error())
}

func TestSendFileCustomMessageTypeValidation(t *testing.T) {
assert := assert.New(t)
pn := NewPubNub(NewDemoConfig())
opts := newSendFileOpts(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())
}
16 changes: 1 addition & 15 deletions publish_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,21 +189,7 @@ func (b *publishBuilder) Execute() (*PublishResponse, StatusResponse, error) {
}

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
return isCustomMessageTypeValid(o.CustomMessageType)
}

func (o *publishOpts) validate() error {
Expand Down
16 changes: 1 addition & 15 deletions signal_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,21 +98,7 @@ type signalOpts struct {
}

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
return isCustomMessageTypeValid(o.CustomMessageType)
}

func (o *signalOpts) validate() error {
Expand Down
18 changes: 18 additions & 0 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,21 @@ func decryptString(cryptoModule crypto.CryptoModule, message string) (retVal int
val, e := cryptoModule.Decrypt(value)
return fmt.Sprintf("%s", string(val)), e
}

func isCustomMessageTypeValid(customMessageType string) bool {
if len(customMessageType) == 0 {
return true
}

if len(customMessageType) < 3 || len(customMessageType) > 50 {
return false
}

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

return true
}

0 comments on commit 6cd8d00

Please sign in to comment.