def test_simple_post(): with patch.object(requests.Session, 'request') as request_mock, \ patch.object(RequestLog, 'log') as log_mock: simple_post( domain=TEST_DOMAIN, url=TEST_API_URL, data='<payload id="abc123"><parrot status="dead" /></payload>', headers={'Content-Type': 'text/xml+parrot'}, auth=(TEST_API_USERNAME, TEST_API_PASSWORD), verify=True, payload_id=TEST_PAYLOAD_ID, ) request_mock.assert_called_with( 'POST', TEST_API_URL, auth=(TEST_API_USERNAME, TEST_API_PASSWORD), data='<payload id="abc123"><parrot status="dead" /></payload>', headers={ 'Content-Type': 'text/xml+parrot', 'content-length': '55', }, timeout=REQUEST_TIMEOUT, ) ((__, (level, log_entry), ___), ) = log_mock.mock_calls assert_equal(level, logging.INFO) assert_equal(log_entry.payload_id, TEST_PAYLOAD_ID)
def test_repeater(request, domain): url = request.POST["url"] repeater_type = request.POST['repeater_type'] format = request.POST.get('format', None) repeater_class = get_all_repeater_types()[repeater_type] form = GenericRepeaterForm({ "url": url, "format": format }, domain=domain, repeater_class=repeater_class) if form.is_valid(): url = form.cleaned_data["url"] format = format or RegisterGenerator.default_format_by_repeater( repeater_class) generator_class = RegisterGenerator.generator_class_by_repeater_format( repeater_class, format) repeater = repeater_class( username=request.POST.get('username'), password=request.POST.get('password'), auth_type=request.POST.get('auth_type'), ) generator = generator_class(repeater) fake_post = generator.get_test_payload(domain) headers = generator.get_headers() verify = not request.POST.get('skip_cert_verify') == 'true' try: resp = simple_post( domain, url, fake_post, headers=headers, auth_manager=repeater.get_auth_manager(), verify=verify, ) if 200 <= resp.status_code < 300: return HttpResponse( json.dumps({ "success": True, "response": resp.text, "status": resp.status_code })) else: return HttpResponse( json.dumps({ "success": False, "response": resp.text, "status": resp.status_code })) except Exception as e: errors = str(e) return HttpResponse(json.dumps({"success": False, "response": errors})) else: return HttpResponse( json.dumps({ "success": False, "response": "Please enter a valid url." }))
def test_simple_post(): with patch.object(requests.Session, 'request') as request_mock, \ patch.object(RequestLog, 'log') as log_mock: request_mock.return_value = Response() auth_manager = BasicAuthManager(TEST_API_USERNAME, TEST_API_PASSWORD) response = simple_post( domain=TEST_DOMAIN, url=TEST_API_URL, data='<payload id="abc123"><parrot status="déad" /></payload>', headers={'Content-Type': 'text/xml+parrot'}, auth_manager=auth_manager, verify=True, payload_id=TEST_PAYLOAD_ID, ) request_mock.assert_called_with( 'POST', TEST_API_URL, data=b'<payload id="abc123"><parrot status="d\xc3\xa9ad" /></payload>', headers={ 'Content-Type': 'text/xml+parrot', 'content-length': '56', }, json=None, timeout=REQUEST_TIMEOUT, ) ((__, (level, log_entry), ___),) = log_mock.mock_calls assert_equal(level, logging.INFO) assert_equal(log_entry.payload_id, TEST_PAYLOAD_ID) assert_equal(response.status_code, 200)
def send_request(self, repeat_record, payload): url = self.get_url(repeat_record) return simple_post( self.domain, url, payload, headers=self.get_headers(repeat_record), auth_manager=self.connection_settings.get_auth_manager(), verify=self.verify, notify_addresses=self.connection_settings.notify_addresses, payload_id=repeat_record.payload_id, )
def send_request(self, repeat_record, payload): headers = self.get_headers(repeat_record) auth = self.get_auth() url = self.get_url(repeat_record) return simple_post( self.domain, url, payload, headers=headers, auth=auth, verify=self.verify, notify_addresses=self.notify_addresses, payload_id=repeat_record.payload_id, )
def test_simple_post_400(): with patch.object(requests.Session, 'request') as request_mock, \ patch.object(Requests, 'notify_error') as notify_mock, \ patch.object(RequestLog, 'log'): response = Response() response.status_code = 400 response.content = b'Bad request' request_mock.return_value = response auth_manager = BasicAuthManager(TEST_API_USERNAME, TEST_API_PASSWORD) response = simple_post( domain=TEST_DOMAIN, url=TEST_API_URL, data='<payload id="abc123"><parrot status="déad" /></payload>', headers={'Content-Type': 'text/xml+parrot'}, auth_manager=auth_manager, verify=True, payload_id=TEST_PAYLOAD_ID, ) notify_mock.assert_called_with('HTTP status code 400: Bad request') assert_equal(response.status_code, 400)