PageRenderTime 29ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/node_modules/nodemailer/node_modules/aws-sdk/test/protocol/rest_xml.spec.coffee

https://gitlab.com/jonnyforney/beerbowerpainting
CoffeeScript | 329 lines | 302 code | 27 blank | 0 comment | 2 complexity | 16e93b2c3e9ffce679786ec2e28c2968 MD5 | raw file
  1. helpers = require('../helpers')
  2. AWS = helpers.AWS
  3. Buffer = AWS.util.Buffer
  4. svc = AWS.Protocol.RestXml
  5. describe 'AWS.Protocol.RestXml', ->
  6. MockRESTXMLService = AWS.util.inherit AWS.Service,
  7. endpointPrefix: 'mockservice'
  8. xmlns = 'http://mockservice.com/xmlns'
  9. request = null
  10. response = null
  11. service = null
  12. beforeEach ->
  13. MockRESTXMLService.prototype.api = new AWS.Model.Api
  14. metadata:
  15. xmlNamespace: xmlns
  16. operations:
  17. SampleOperation:
  18. http:
  19. method: 'POST'
  20. requestUri: '/'
  21. AWS.Service.defineMethods(MockRESTXMLService)
  22. service = new MockRESTXMLService(region: 'region')
  23. request = new AWS.Request(service, 'sampleOperation')
  24. response = request.response
  25. defop = (op) ->
  26. AWS.util.property(service.api.operations, 'sampleOperation',
  27. new AWS.Model.Operation('sampleOperation', op, api: service.api))
  28. describe 'buildRequest', ->
  29. build = -> svc.buildRequest(request); request
  30. describe 'empty bodies', ->
  31. it 'defaults body to empty string when there are no inputs', ->
  32. defop input: type: 'structure', members: {}
  33. expect(build().httpRequest.body).toEqual('')
  34. it 'defaults body to empty string when no body params are present', ->
  35. request.params = Bucket: 'abc', ACL: 'canned-acl'
  36. defop
  37. http: requestUri: '/{Bucket}'
  38. input:
  39. type: 'structure'
  40. members:
  41. Bucket:
  42. location: 'uri'
  43. ACL:
  44. locationName: 'x-amz-acl'
  45. location: 'header'
  46. build()
  47. expect(request.httpRequest.body).toEqual('')
  48. expect(request.httpRequest.path).toEqual('/abc')
  49. expect(request.httpRequest.headers['x-amz-acl']).toEqual('canned-acl')
  50. describe 'string bodies', ->
  51. it 'populates the body with string types directly', ->
  52. request.params = Bucket: 'bucket-name', Data: 'abc'
  53. defop
  54. http: requestUri: '/{Bucket}'
  55. input:
  56. payload: 'Data'
  57. members:
  58. Bucket:
  59. location: 'uri'
  60. Data:
  61. type: 'string'
  62. expect(build().httpRequest.body).toEqual('abc')
  63. describe 'xml bodies', ->
  64. it 'populates the body with XML from the params', ->
  65. request.params =
  66. ACL: 'canned-acl'
  67. Config:
  68. Abc: 'abc'
  69. Locations: ['a', 'b', 'c']
  70. Data: [
  71. { Foo:'foo1', Bar:'bar1' },
  72. { Foo:'foo2', Bar:'bar2' },
  73. ]
  74. Bucket: 'bucket-name'
  75. Marker: 'marker'
  76. Limit: 123
  77. Metadata:
  78. abc: 'xyz'
  79. mno: 'hjk'
  80. defop
  81. http: requestUri: '/{Bucket}'
  82. input:
  83. payload: 'Config'
  84. members:
  85. Bucket: # uri path param
  86. type: 'string'
  87. location: 'uri'
  88. Marker: # uri querystring param
  89. type: 'string'
  90. location: 'querystring'
  91. locationName: 'next-marker'
  92. Limit: # uri querystring integer param
  93. type: 'integer'
  94. location: 'querystring'
  95. locationName: 'limit'
  96. ACL: # header string param
  97. type: 'string'
  98. location: 'header'
  99. locationName: 'x-amz-acl'
  100. Metadata: # header map param
  101. type: 'map'
  102. location: 'headers'
  103. locationName: 'x-amz-meta-'
  104. Config: # structure of mixed tpyes
  105. type: 'structure'
  106. members:
  107. Abc: type: 'string'
  108. Locations: # array of strings
  109. type: 'list'
  110. member:
  111. type: 'string'
  112. locationName: 'Location'
  113. Data: # array of structures
  114. type: 'list'
  115. member:
  116. type: 'structure'
  117. members:
  118. Foo: type: 'string'
  119. Bar: type: 'string'
  120. xml = """
  121. <Config xmlns="http://mockservice.com/xmlns">
  122. <Abc>abc</Abc>
  123. <Locations>
  124. <Location>a</Location>
  125. <Location>b</Location>
  126. <Location>c</Location>
  127. </Locations>
  128. <Data>
  129. <member>
  130. <Foo>foo1</Foo>
  131. <Bar>bar1</Bar>
  132. </member>
  133. <member>
  134. <Foo>foo2</Foo>
  135. <Bar>bar2</Bar>
  136. </member>
  137. </Data>
  138. </Config>
  139. """
  140. build()
  141. expect(request.httpRequest.method).toEqual('POST')
  142. expect(request.httpRequest.path).
  143. toEqual('/bucket-name?limit=123&next-marker=marker')
  144. expect(request.httpRequest.headers['x-amz-acl']).toEqual('canned-acl')
  145. expect(request.httpRequest.headers['x-amz-meta-abc']).toEqual('xyz')
  146. expect(request.httpRequest.headers['x-amz-meta-mno']).toEqual('hjk')
  147. helpers.matchXML(request.httpRequest.body, xml)
  148. it 'omits the body xml when body params are not present', ->
  149. request.params = Bucket:'abc' # omitting Config purposefully
  150. defop
  151. http: requestUri: '/{Bucket}'
  152. input:
  153. members:
  154. Bucket:
  155. location: 'uri'
  156. Config: {}
  157. build()
  158. expect(request.httpRequest.body).toEqual('')
  159. expect(request.httpRequest.path).toEqual('/abc')
  160. it 'uses payload member name for payloads', ->
  161. request.params =
  162. Data:
  163. Member1: 'member1'
  164. Member2: 'member2'
  165. defop
  166. input:
  167. payload: 'Data'
  168. members:
  169. Data:
  170. type: 'structure'
  171. locationName: 'RootElement'
  172. members:
  173. Member1: type: 'string'
  174. Member2: type: 'string'
  175. helpers.matchXML build().httpRequest.body, """
  176. <RootElement xmlns="http://mockservice.com/xmlns">
  177. <Member1>member1</Member1>
  178. <Member2>member2</Member2>
  179. </RootElement>
  180. """
  181. describe 'extractError', ->
  182. extractError = (body) ->
  183. if body == undefined
  184. body = """
  185. <Error>
  186. <Code>InvalidArgument</Code>
  187. <Message>Provided param is bad</Message>
  188. </Error>
  189. """
  190. response.httpResponse.statusCode = 400
  191. response.httpResponse.body = new Buffer(body)
  192. svc.extractError(response)
  193. it 'extracts the error code and message', ->
  194. extractError()
  195. expect(response.error instanceof Error).toBeTruthy()
  196. expect(response.error.code).toEqual('InvalidArgument')
  197. expect(response.error.message).toEqual('Provided param is bad')
  198. expect(response.data).toEqual(null)
  199. it 'returns an empty error when the body is blank', ->
  200. extractError ''
  201. expect(response.error instanceof Error).toBeTruthy()
  202. expect(response.error.code).toEqual(400)
  203. expect(response.error.message).toEqual(null)
  204. expect(response.data).toEqual(null)
  205. it 'extracts error when inside <Errors>', ->
  206. extractError """
  207. <SomeResponse>
  208. <Errors>
  209. <Error>
  210. <Code>code</Code><Message>msg</Message>
  211. </Error>
  212. </Errors>
  213. </SomeResponse>"""
  214. expect(response.error.code).toEqual('code')
  215. expect(response.error.message).toEqual('msg')
  216. it 'extracts error when <Error> is nested', ->
  217. extractError """
  218. <SomeResponse>
  219. <Error>
  220. <Code>code</Code><Message>msg</Message>
  221. </Error>
  222. </SomeResponse>"""
  223. expect(response.error.code).toEqual('code')
  224. expect(response.error.message).toEqual('msg')
  225. describe 'extractData', ->
  226. extractData = (body) ->
  227. response.httpResponse.statusCode = 200
  228. response.httpResponse.body = new Buffer(body)
  229. svc.extractData(response)
  230. it 'parses the xml body', ->
  231. defop output:
  232. type: 'structure'
  233. members:
  234. Foo: {}
  235. Bar:
  236. type: 'list'
  237. member:
  238. locationName: 'Item'
  239. extractData """
  240. <xml>
  241. <Foo>foo</Foo>
  242. <Bar>
  243. <Item>a</Item>
  244. <Item>b</Item>
  245. <Item>c</Item>
  246. </Bar>
  247. </xml>
  248. """
  249. expect(response.data).toEqual({Foo:'foo', Bar:['a', 'b', 'c']})
  250. it 'sets payload element to a Buffer object when it streams', ->
  251. defop output:
  252. type: 'structure'
  253. payload: 'Body'
  254. members:
  255. Body:
  256. streaming: true
  257. extractData 'Buffer data'
  258. expect(Buffer.isBuffer(response.data.Body)).toBeTruthy()
  259. expect(response.data.Body.toString()).toEqual('Buffer data')
  260. it 'sets payload element to String when it does not stream', ->
  261. defop output:
  262. type: 'structure'
  263. payload: 'Body'
  264. members:
  265. Body: type: 'string'
  266. extractData 'Buffer data'
  267. expect(typeof response.data.Body).toEqual('string')
  268. expect(response.data.Body).toEqual('Buffer data')
  269. it 'sets payload element along with other outputs', ->
  270. response.httpResponse.headers['x-amz-foo'] = 'foo'
  271. response.httpResponse.headers['x-amz-bar'] = 'bar'
  272. defop output:
  273. type: 'structure'
  274. payload: 'Baz'
  275. members:
  276. Foo:
  277. location: 'header'
  278. locationName: 'x-amz-foo'
  279. Bar:
  280. location: 'header'
  281. locationName: 'x-amz-bar'
  282. Baz: {}
  283. extractData 'Buffer data'
  284. expect(response.data.Foo).toEqual('foo')
  285. expect(response.data.Bar).toEqual('bar')
  286. expect(response.data.Baz).toEqual('Buffer data')
  287. it 'parses headers when a payload is provided', ->
  288. response.httpResponse.headers['x-amz-foo'] = 'foo'
  289. defop output:
  290. type: 'structure'
  291. payload: 'Bar'
  292. members:
  293. Foo:
  294. location: 'header'
  295. locationName: 'x-amz-foo'
  296. Bar:
  297. type: 'structure'
  298. members:
  299. Baz: type: 'string'
  300. extractData '<Bar><Baz>Buffer data</Baz></Bar>'
  301. expect(response.data.Foo).toEqual('foo')
  302. expect(response.data.Baz).toEqual('Buffer data')