def test_http_test_pass_retry(): assert is_enabled() msg = MsgPrintDep(text='abcd') get_responses = [ Response(body="first response", status=500, content_type="application/vnd.swiftnav.broker.v1+sbp2"), Response(body='second and last response', status=200, content_type="application/vnd.swiftnav.broker.v1+sbp2") ] post_responses = [ Response(body="", status=500, content_type="application/vnd.swiftnav.broker.v1+sbp2"), Response(body='', status=200, content_type="application/vnd.swiftnav.broker.v1+sbp2") ] register_uri(GET, BASE_STATION_URI, get_responses) register_uri(PUT, BASE_STATION_URI, post_responses) with HTTPDriver(device_uid="Swift22", url=BASE_STATION_URI) as driver: with pytest.raises(ValueError): driver.read(size=255) assert driver.connect_read() assert driver.read(size=255)
def test_structure_presence_flag_is_checked(self): """ Before attempting to unpack TargetInfo structure presence flag must be checked. Originally this issue: https://code.google.com/p/python-ntlm/issues/detail?id=28 """ HTTPretty.register_uri( HTTPretty.GET, FAKE_URL, responses=[ Response(status=401, body="", forcing_headers=INITIAL_REJECTION_HEADERS), MockRawServerResponse(status="401", raw_response=AUTH_TOO_SHORT_RESPONSE), Response(status=200, body=SUCCESSFUL_CONNECTION_BODY, forcing_headers=SUCCESSFUL_CONNECTION_HEADERS), ]) passman = urllib.request.HTTPPasswordMgrWithDefaultRealm() passman.add_password(None, FAKE_URL, FAKE_USER, FAKE_PASSWORD) auth_NTLM = HTTPNtlmAuthHandler.HTTPNtlmAuthHandler(passman, debuglevel=0) opener = urllib.request.build_opener(auth_NTLM) f = opener.open(FAKE_URL) response = f.read() assert response == SUCCESSFUL_CONNECTION_BODY
def mock_event_create_response(): values = [Response(status=200, body=json.dumps(body)), Response(status=400, body='')] httpretty.register_uri(httpretty.POST, API_URL + '/events/', responses=values) put_values = [Response(status=200, body=json.dumps({'title': 'loaded from JSON', 'ignored': 'ignored'}))] httpretty.register_uri(httpretty.PUT, API_URL + '/events/cv4ei7syx10uvsxbs21ccsezf', responses=put_values)
def test_wait_for_completion_works_poll_once(self): url = '{0}/{1}/{2}'.format(self.connection.host, self.cmd._type, self.cmd.id) resps = [ Response(body=json.dumps(_mock_status_working())), Response(body=json.dumps(_mock_status_done())) ] HTTPretty.register_uri(HTTPretty.GET, url, responses=resps) with Timer() as timed: self.cmd.wait_for_completion(poll_interval=.005) self.assertGreater(timed.elapsed, .003) self.assertLess(timed.elapsed, .020)
def test_no_filter(api_client): httpretty.enable() # httpretty kind of sucks and strips & parameters from the URL values = [Response(status=200, body=json.dumps(body)), Response(status=200, body=json.dumps(body2))] httpretty.register_uri(httpretty.GET, API_URL + '/events', responses=values) events = api_client.events.all() assert len(events) == 72 assert events[0].id == 'cv4ei7syx10uvsxbs21ccsezf' httpretty.disable()
def test_file_upload(api_client): httpretty.enable() body = [{ "content_type": "text/plain", "filename": "a.txt", "id": "3qfe4k3siosfjtjpfdnon8zbn", "account_id": "6aakaxzi4j5gn6f7kbb9e0fxs", "object": "file", "size": 762878 }] values = [Response(status=200, body=json.dumps(body))] httpretty.register_uri(httpretty.POST, API_URL + '/files/', responses=values) httpretty.register_uri(httpretty.GET, API_URL + '/files/3qfe4k3siosfjtjpfdnon8zbn/download', body='test body') myfile = api_client.files.create() myfile.filename = 'test.txt' myfile.data = "Hello World." myfile.save() assert myfile.filename == 'a.txt' assert myfile.size == 762878 data = myfile.download().decode() assert data == 'test body'
def test_two_filters(api_client): httpretty.enable() values2 = [Response(status=200, body='[]')] httpretty.register_uri(httpretty.GET, API_URL + '/events?param1=a¶m2=b', responses=values2) events = api_client.events.where(param1='a', param2='b').all() assert len(events) == 0 qs = httpretty.last_request().querystring assert qs['param1'][0] == 'a' assert qs['param2'][0] == 'b' httpretty.disable()
def test_that_if_ntlm_is_not_in_the_header_we_do_shitty_behavior(self): """ This test is here to remind me ff NTLM isn't in the header we basically hang forever :/ """ HTTPretty.register_uri(HTTPretty.GET, FAKE_URL, responses=[ Response( status=401, body="", forcing_headers=BASIC_AUTH_HEADERS), ])
def test_zero_offset(api_client): httpretty.enable() values = [Response(status=200, body='[]')] httpretty.register_uri(httpretty.GET, API_URL + '/events?in=Nylas&offset=0', responses=values) events = api_client.events.where({'in': 'Nylas', 'offset': 0}).items() for event in events: pass qs = httpretty.last_request().querystring assert qs['in'][0] == 'Nylas' assert qs['offset'][0] == '0' httpretty.disable()
def test_duplicate_authenticate_headers(self): """ Handle HTTP responses with more than one WWW-Authenticate header. Some servers send two WWW-Authenticate headers: one with the NTLM challenge and another with the 'Negotiate' phrase. Make sure we operate on the right header. Originally this issue: https://code.google.com/p/python-ntlm/issues/detail?id=27 """ HTTPretty.register_uri( HTTPretty.GET, FAKE_URL, responses=[ Response(status=401, body="", forcing_headers=INITIAL_REJECTION_HEADERS), MockRawServerResponse(status="401", raw_response=DUPLICATE_HEADERS), Response(status=200, body=SUCCESSFUL_CONNECTION_BODY, forcing_headers=SUCCESSFUL_CONNECTION_HEADERS), ]) passman = urllib.request.HTTPPasswordMgrWithDefaultRealm() passman.add_password(None, FAKE_URL, FAKE_USER, FAKE_PASSWORD) auth_NTLM = HTTPNtlmAuthHandler.HTTPNtlmAuthHandler(passman, debuglevel=0) opener = urllib.request.build_opener(auth_NTLM) f = opener.open(FAKE_URL) response = f.read() assert response == SUCCESSFUL_CONNECTION_BODY