Coverage for tests\request\test_request_converter.py: 100%

1 import unittest

2 import textwrap

3

4 from zapy.requests.models import ZapyRequest, KeyValueItem

5 from zapy.requests.converter import RequestConverter

6 from zapy.requests.requester import Requester, ZapyRequestContext

7

8 from zapy.store import use_store

9 from zapy.store.manager import Store

10

11

12 class TestCaseReq(unittest.TestCase):

13

14 def test_trailing_spaces(self):

15 zapy_request = ZapyRequest(

16 endpoint="http://test",

17 method="GET",

18 variables=[

19 KeyValueItem(key='var1', value='Hello ', active=True),

20 ],

21 params=[

22 KeyValueItem(key='param1 ', value='{{var1 + "World"}}', active=True),

23 ],

24 )

25 ctx = ZapyRequestContext(store=use_store(), logger=print)

26 requester = RequestConverter(zapy_request, ctx)

27 request_context = requester.build_httpx_args()

28

29 self.assertDictEqual({'param1': ['Hello World']}, request_context['params'])

30

31 def test_python_variables(self):

32 zapy_request = ZapyRequest(

33 endpoint="http://test/",

34 method="GET",

35 variables=[

36 KeyValueItem(key='var1', value='{{ Decimal("0.1") }}', active=True),

37 ],

38 headers=[

39 KeyValueItem(key='X-test', value='{{ Decimal("5") }}', active=True),

40 KeyValueItem(key='Content-TYPE', value='{{ ctx.auto() }}', active=True),

41 ],

42 params=[

43 KeyValueItem(key='param1', value='{{var1 + Decimal("0.2")}}', active=True),

44 ],

45 script=[

46 'from decimal import Decimal',

47 ],

48 body_type='text/plain',

49 body=[

50 '{{ var1 }}',

51 ],

52 )

53 ctx = ZapyRequestContext(store=use_store(), logger=print)

54 requester = RequestConverter(zapy_request, ctx)

55 request_context = requester.build_httpx_args()

56

57 self.assertDictEqual({'X-test': '5', 'Content-TYPE': 'text/plain'}, request_context['headers'])

58 self.assertEqual({'param1': ['0.3']}, request_context['params'])

59 self.assertEqual("0.1", request_context['content'])

60

61 def test_jinja_render(self):

62 zapy_request = ZapyRequest(

63 endpoint="http://test/{{var1}}/{{var2 | upper}}",

64 method="GET",

65 variables=[

66 KeyValueItem(key='var1', value='{#jinja#}{{ "val1" | upper }}', active=True),

67 KeyValueItem(key='var2', value='val2', active=True),

68 KeyValueItem(key='var3', value='{{[1, 2, 0.3]}}', active=True),

69 ],

70 params=[

71 KeyValueItem(key='param1', value='val: {{var1}}', active=True),

72 KeyValueItem(key='param2', value='{{var2 | upper}}', active=True),

73 KeyValueItem(key='param3', value='{{ var1 }} {{ var2 }}', active=True),

74 ],

75 headers=[

76 KeyValueItem(key='X-test', value='{#jinja#}{{ "val1" | upper }}', active=True),

77 KeyValueItem(key='Content-TYPE', value='{{ ctx.auto() }}', active=True),

78 ],

79 body_type='text/plain',

80 body=[

81 '{% for element in var3 -%}',

82 '{{element + 4}}',

83 '{% endfor %}',

84 ],

85 )

86 ctx = ZapyRequestContext(store=use_store(), logger=print)

87 requester = RequestConverter(zapy_request, ctx)

88 request_context = requester.build_httpx_args()

89

90 self.assertEqual("http://test/VAL1/VAL2", request_context['url'])

91 self.assertDictEqual({

92 'param1': ['val: VAL1'],

93 'param2': ['VAL2'],

94 'param3': ['VAL1 val2'],

95 }, request_context['params'])

96 self.assertDictEqual({'X-test': 'VAL1', 'Content-TYPE': 'text/plain'}, request_context['headers'])

97 self.assertEqual(textwrap.dedent("""\

98 5

99 6

100 4.3

101 """), request_context['content'])

102

103 def test_url(self):

104 zapy_request = ZapyRequest(

105 endpoint="http://test/{{var1}}/{{var2}}",

106 method="GET",

107 variables=[

108 KeyValueItem(key='var1', value='{{ 1 + 2 }}', active=True),

109 KeyValueItem(key=' var2 ', value=' between spaces ', active=True),

110 ],

111 )

112 ctx = ZapyRequestContext(store=use_store(), logger=print)

113 requester = RequestConverter(zapy_request, ctx)

114 request_context = requester.build_httpx_args()

115

116 self.assertEqual("http://test/3/ between spaces ", request_context['url'])

117

118 def test_body_form(self):

119 zapy_request = ZapyRequest(

120 endpoint="http://test/",

121 method="POST",

122 variables=[

123 KeyValueItem(key='var1', value='{#jinja#}{{ "val1" | upper }}', active=True),

124 KeyValueItem(key='var2', value='val2', active=True),

125 KeyValueItem(key='var3', value='{{[1, 2, 0.3]}}', active=True),

126 ],

127 headers=[

128 KeyValueItem(key='Content-TYPE', value='{{ ctx.auto() }}', active=True),

129 ],

130 body_type='application/x-www-form-urlencoded',

131 body=[

132 KeyValueItem(key='param1', value='val: {{var1}}', active=True),

133 KeyValueItem(key='param2', value='{#jinja#}{{var2 | upper}}', active=True),

134 KeyValueItem(key='param3', value='{{ var1 }} {{ var2 }}', active=True),

135 ],

136 )

137 ctx = ZapyRequestContext(store=use_store(), logger=print)

138 requester = RequestConverter(zapy_request, ctx)

139 request_context = requester.build_httpx_args()

140

141 self.assertEqual({'Content-TYPE': 'application/x-www-form-urlencoded'}, request_context['headers'])

142 self.assertEqual({

143 'param1': ['val: VAL1'],

144 'param2': ['VAL2'],

145 'param3': ['VAL1 val2'],

146 }, request_context['data'])

147

148 def test_body_none(self):

149 zapy_request = ZapyRequest(

150 endpoint="http://test/",

151 method="GET",

152 headers=[

153 KeyValueItem(key='Content-TYPE', value='{{ ctx.auto() }}', active=True),

154 ],

155 body_type='None',

156 body=[

157 'any',

158 ],

159 )

160 ctx = ZapyRequestContext(store=use_store(), logger=print)

161 requester = RequestConverter(zapy_request, ctx)

162 request_context = requester.build_httpx_args()

163

164 self.assertEqual({}, request_context['headers'])

165 self.assertIsNone(request_context.get('data'))

166

167 def test_multipart_form_with_files(self):

168 zapy_request = ZapyRequest(

169 endpoint="http://test/",

170 method="POST",

171 variables=[

172 KeyValueItem(key='var1', value='{#jinja#}{{ "val1" | upper }}', active=True),

173 KeyValueItem(key='var3', value='tests/assets/request1.zapy', active=True),

174 ],

175 body_type='multipart/form-data',

176 body=[

177 KeyValueItem(key='param1', value='val: {{var1}}', active=True),

178 KeyValueItem(key='param4', value='{{ ctx.load_file(var3) }}', active=True),

179 ],

180 )

181 ctx = ZapyRequestContext(store=use_store(), logger=print)

182 requester = RequestConverter(zapy_request, ctx)

183 request_context = requester.build_httpx_args()

184

185 self.assertEqual({}, request_context['headers'])

186 self.assertEqual({

187 'param1': ['val: VAL1'],

188 }, request_context['data'])

189

190 name, file_data = request_context['files'][0]

191 self.assertEqual(2, len(file_data))

192

193 file_name, file = file_data

194 self.assertEqual('param4', name)

195 self.assertEqual('request1.zapy', file_name)

196 self.assertEqual(list(open('tests/assets/request1.zapy', mode='rb')), list(file))

197

198 def test_header_auto(self):

199 zapy_request = ZapyRequest(

200 endpoint="http://test/",

201 method="GET",

202 headers=[

203 KeyValueItem(key='Content-TYPE', value='{{ ctx.auto() }}', active=True),

204 KeyValueItem(key=' X-stripped', value=' no_stripped ', active=True),

205 ],

206 body_type='application/json',

207 body=[

208 '{ "hello": "world" }',

209 ],

210 )

211 ctx = ZapyRequestContext(store=use_store(), logger=print)

212 requester = RequestConverter(zapy_request, ctx)

213 request_context = requester.build_httpx_args()

214

215 self.assertDictEqual({'Content-TYPE': 'application/json', 'X-stripped': ' no_stripped '}, request_context['headers'])

216 self.assertEqual('{ "hello": "world" }', request_context['content'])

217

218 def test_header_override(self):

219 zapy_request = ZapyRequest(

220 endpoint="http://test/",

221 method="GET",

222 variables=[

223 KeyValueItem(key='content_type', value='text/plain', active=True),

224 ],

225 headers=[

226 KeyValueItem(key='content-Type', value='{{ content_type }}', active=True),

227 ],

228 body_type='application/json',

229 body=[

230 '{{ content_type | upper }}',

231 ],

232 )

233 ctx = ZapyRequestContext(store=use_store(), logger=print)

234 requester = RequestConverter(zapy_request, ctx)

235 request_context = requester.build_httpx_args()

236

237 self.assertDictEqual({

238 'content-Type': 'text/plain',

239 }, request_context['headers'])

240 self.assertEqual("TEXT/PLAIN", request_context['content'])

241

242 def test_header_missing(self):

243 zapy_request = ZapyRequest(

244 endpoint="http://test/",

245 method="GET",

246 body_type='text/plain',

247 body=[

248 'any',

249 ],

250 )

251 ctx = ZapyRequestContext(store=use_store(), logger=print)

252 requester = RequestConverter(zapy_request, ctx)

253 request_context = requester.build_httpx_args()

254

255 self.assertDictEqual({}, request_context['headers'])

256 self.assertEqual("any", request_context['content'])

257

258 def test_store(self):

259 from decimal import Decimal

260 zapy_request = ZapyRequest(

261 endpoint="http://test/{{ctx.store.path1}}",

262 method="GET",

263 params=[

264 KeyValueItem(key='param1', value='{{ctx.store.var1 + ctx.store.var2}}', active=True),

265 ],

266 script=[

267 'from decimal import Decimal',

268 'ctx.store.var2 = Decimal("0.2")',

269 ],

270 )

271 store = Store()

272 store.path1 = 'abc'

273 store['var1'] = Decimal('0.1')

274

275 ctx = ZapyRequestContext(store=store, logger=print)

276 requester = RequestConverter(zapy_request, ctx)

277 request_context = requester.build_httpx_args()

278

279 self.assertEqual("http://test/abc", request_context['url'])

280 self.assertEqual({'param1': ['0.3']}, request_context['params'])

281 self.assertEqual(Decimal("0.2"), store.var2)