From 6cd57441a896857d2012845e2cdfafb2c8f181df Mon Sep 17 00:00:00 2001 From: Mitsuhiro Koga Date: Wed, 11 May 2016 00:03:30 +0900 Subject: [PATCH] post and put value should be URL-encoded refer to the following example. http://developer.chatwork.com/ja/endpoint_rooms.html --- src/chatwork.coffee | 26 +++++++++++++------------- test/chatwork.test.coffee | 34 +++++++++++++++++----------------- 2 files changed, 30 insertions(+), 30 deletions(-) diff --git a/src/chatwork.coffee b/src/chatwork.coffee index 685937e..5c6dba6 100644 --- a/src/chatwork.coffee +++ b/src/chatwork.coffee @@ -82,12 +82,12 @@ class ChatworkStreaming extends EventEmitter create: (name, adminIds, opts, callback) => params = [] - params.push "name=#{name}" - params.push "members_admin_ids=#{adminIds.join ','}" - params.push "description=#{opts.desc}" if opts.desc? + params.push "name=#{encodeURIComponent name}" + params.push "members_admin_ids=#{encodeURIComponent adminIds.join ','}" + params.push "description=#{encodeURIComponent opts.desc}" if opts.desc? params.push "icon_preset=#{opts.icon}" if opts.icon? - params.push "members_member_ids=#{opts.memberIds.join ','}" if opts.memberIds? - params.push "members_readonly_ids=#{opts.roIds.join ','}" if opts.roIds? + params.push "members_member_ids=#{encodeURIComponent opts.memberIds.join ','}" if opts.memberIds? + params.push "members_readonly_ids=#{encodeURIComponent opts.roIds.join ','}" if opts.roIds? body = params.join '&' @post "/rooms", body, callback @@ -99,9 +99,9 @@ class ChatworkStreaming extends EventEmitter update: (opts, callback) => params = [] - params.push "description=#{opts.desc}" if opts.desc? + params.push "description=#{encodeURIComponent opts.desc}" if opts.desc? params.push "icon_preset=#{opts.icon}" if opts.icon? - params.push "name=#{opts.name}" if opts.name? + params.push "name=#{encodeURIComponent opts.name}" if opts.name? body = params.join '&' @put "#{baseUrl}", body, callback @@ -119,9 +119,9 @@ class ChatworkStreaming extends EventEmitter update: (adminIds, opts, callback) => params = [] - params.push "members_admin_ids=#{adminIds.join ','}" - params.push "members_member_ids=#{opts.memberIds.join ','}" if opts.memberIds? - params.push "members_readonly_ids=#{opts.roIds.join ','}" if opts.roIds? + params.push "members_admin_ids=#{encodeURIComponent adminIds.join ','}" + params.push "members_member_ids=#{encodeURIComponent opts.memberIds.join ','}" if opts.memberIds? + params.push "members_readonly_ids=#{encodeURIComponent opts.roIds.join ','}" if opts.roIds? body = params.join '&' @put "#{baseUrl}/members", body, callback @@ -130,7 +130,7 @@ class ChatworkStreaming extends EventEmitter @get "#{baseUrl}/messages", "", callback create: (text, callback) => - body = "body=#{text}" + body = "body=#{encodeURIComponent text}" @post "#{baseUrl}/messages", body, callback listen: => @@ -162,8 +162,8 @@ class ChatworkStreaming extends EventEmitter create: (text, toIds, opts, callback) => params = [] - params.push "body=#{text}" - params.push "to_ids=#{toIds.join ','}" + params.push "body=#{encodeURIComponent text}" + params.push "to_ids=#{encodeURIComponent toIds.join ','}" params.push "limit=#{opts.limit}" if opts.limit? body = params.join '&' @post "#{baseUrl}/tasks", body, callback diff --git a/test/chatwork.test.coffee b/test/chatwork.test.coffee index 5cbf251..7bae0e7 100644 --- a/test/chatwork.test.coffee +++ b/test/chatwork.test.coffee @@ -171,8 +171,8 @@ describe 'ChatworkStreaming', -> name = 'Website renewal project' adminIds = [123, 542, 1001] api.post('/v1/rooms').reply 200, (url, body) -> - body.should.be.equal "name=#{name}" \ - + "&members_admin_ids=#{adminIds.join ','}" + body.should.be.equal "name=#{encodeURIComponent name}" \ + + "&members_admin_ids=#{encodeURIComponent adminIds.join ','}" done() bot.Rooms().create name, adminIds, {}, null @@ -187,12 +187,12 @@ describe 'ChatworkStreaming', -> api.post('/v1/rooms').reply 200, (url, body) -> params = Helper.parseBody body params.should.be.deep.equal - description: opts.desc + description: encodeURIComponent opts.desc icon_preset: opts.icon - members_admin_ids: adminIds.join ',' - members_member_ids: opts.memberIds.join ',' - members_readonly_ids: opts.roIds.join ',' - name: name + members_admin_ids: encodeURIComponent adminIds.join ',' + members_member_ids: encodeURIComponent opts.memberIds.join ',' + members_readonly_ids: encodeURIComponent opts.roIds.join ',' + name: encodeURIComponent name done() bot.Rooms().create name, adminIds, opts, null @@ -232,9 +232,9 @@ describe 'ChatworkStreaming', -> api.put(baseUrl).reply 200, (url, body) -> params = Helper.parseBody body params.should.be.deep.equal - description: opts.desc + description: encodeURIComponent opts.desc icon_preset: opts.icon - name: opts.name + name: encodeURIComponent opts.name done() room.update opts, null @@ -277,7 +277,7 @@ describe 'ChatworkStreaming', -> it 'should update members when no opts', (done) -> adminIds = [123, 542, 1001] api.put("#{baseUrl}/members").reply 200, (url, body) -> - body.should.be.equal "members_admin_ids=#{adminIds.join ','}" + body.should.be.equal "members_admin_ids=#{encodeURIComponent adminIds.join ','}" done() room.Members().update adminIds, {}, null @@ -290,9 +290,9 @@ describe 'ChatworkStreaming', -> api.put("#{baseUrl}/members").reply 200, (url, body) -> params = Helper.parseBody body params.should.be.deep.equal - members_admin_ids: adminIds.join ',' - members_member_ids: opts.memberIds.join ',' - members_readonly_ids: opts.roIds.join ',' + members_admin_ids: encodeURIComponent adminIds.join ',' + members_member_ids: encodeURIComponent opts.memberIds.join ',' + members_readonly_ids: encodeURIComponent opts.roIds.join ',' done() room.Members().update adminIds, opts, null @@ -308,7 +308,7 @@ describe 'ChatworkStreaming', -> it 'should create a message', (done) -> message = 'This is a test message' api.post("#{baseUrl}/messages").reply 200, (url, body) -> - body.should.be.equal "body=#{message}" + body.should.be.equal "body=#{encodeURIComponent message}" fixtures.room.messages.post room.Messages().create message, (err, data) -> @@ -371,7 +371,7 @@ describe 'ChatworkStreaming', -> text = "Buy milk" toIds = [1, 3, 6] api.post("#{baseUrl}/tasks").reply 200, (url, body) -> - body.should.be.equal "body=#{text}&to_ids=#{toIds.join ','}" + body.should.be.equal "body=#{encodeURIComponent text}&to_ids=#{encodeURIComponent toIds.join ','}" done() room.Tasks().create text, toIds, {}, null @@ -383,9 +383,9 @@ describe 'ChatworkStreaming', -> api.post("#{baseUrl}/tasks").reply 200, (url, body) -> params = Helper.parseBody body params.should.be.deep.equal - body: text + body: encodeURIComponent text limit: "#{opts.limit}" - to_ids: toIds.join ',' + to_ids: encodeURIComponent toIds.join ',' done() room.Tasks().create text, toIds, opts, null