def test_response_body_enabled(self): url = self.mockurl('show-image') resp = self.request_lua( """ treat = require('treat') function main(splash) splash.response_body_enabled = true assert(splash:go(splash.args.url)) local har1 = splash:har{reset=true} splash.response_body_enabled = false assert(splash:go(splash.args.url)) local har2 = splash:har() return { har = treat.as_array({har1, har2}), enabled2 = splash.response_body_enabled, } end """, {'url': url}) self.assertStatusCode(resp, 200) data = resp.json() assert data['enabled2'] is False img_gif = requests.get(self.mockurl("slow.gif?n=0")).content resp = requests.get(self.mockurl('show-image')).content entries = self.assertHarEntriesLength(data['har'][0], 2) body = get_response_body_bytes(entries[0]['response']) assert body[:50] == resp[:50] # there is some randomness in the end self.assertBase64Content(entries[1], img_gif) entries = self.assertHarEntriesLength(data['har'][1], 2) self.assertNoContent(entries[0]) self.assertNoContent(entries[1])
def test_response_body_enabled(self): url = self.mockurl("show-image") resp = self.request_lua( """ treat = require('treat') function main(splash) splash.response_body_enabled = true assert(splash:go(splash.args.url)) local har1 = splash:har{reset=true} splash.response_body_enabled = false assert(splash:go(splash.args.url)) local har2 = splash:har() return { har = treat.as_array({har1, har2}), enabled2 = splash.response_body_enabled, } end """, {"url": url}, ) self.assertStatusCode(resp, 200) data = resp.json() assert data["enabled2"] is False img_gif = requests.get(self.mockurl("slow.gif?n=0")).content resp = requests.get(self.mockurl("show-image")).content entries = self.assertHarEntriesLength(data["har"][0], 2) body = get_response_body_bytes(entries[0]["response"]) assert body[:50] == resp[:50] # there is some randomness in the end self.assertBase64Content(entries[1], img_gif) entries = self.assertHarEntriesLength(data["har"][1], 2) self.assertNoContent(entries[0]) self.assertNoContent(entries[1])
def assertBase64Content(self, entry, body): assert entry['response']['content']['encoding'] == 'base64' assert get_response_body_bytes(entry['response']) == body
def test_body_bytes_bad_encoding(): har_response = get_har_response("hello", "i-am-unknown") with pytest.raises(ValueError): get_response_body_bytes(har_response)
def test_get_body_bytes(text, encoding, result): har_response = get_har_response(text, encoding) assert get_response_body_bytes(har_response) == result
def get_body(self): if self._body_binary is None: body = get_response_body_bytes(self._info) content_type = self._info['content']['mimeType'] self._body_binary = BinaryCapsule(body, content_type) return self._body_binary
def assertBase64Content(self, entry, body): assert entry["response"]["content"]["encoding"] == "base64" assert get_response_body_bytes(entry["response"]) == body