def test_POST_FILES_WITH_CJK_PARAMS(self): """ Test good unicode support for parameters in a POST request. """ with open(__file__) as f: url = self.coapbin('post') post1 = coap.post(url, data={'some': '中文'}, files={'some': f}) post2 = coap.post(url, data={'some': '日本語'}, files=[('some', f)]) post3 = coap.post(url, data=[('some', '한국의')], files=[('some', f)]) self.assertEqual(post1.status_code, 200) self.assertEqual(post2.status_code, 200) self.assertEqual(post3.status_code, 200)
def test_post_fields_with_multiple_values_and_files(self): """ Test that it is possible to POST using the files argument and a list for a value in the data argument. """ data = {'field': ['a', 'b']} files = {'field': 'Garbled data'} r = coap.post(self.coapbin('post'), data=data, files=files) t = json.loads(r.text) self.assertEqual(t.get('form'), {'field': ['a', 'b']}) self.assertEqual(t.get('files'), files) coap.post(self.coapbin('post'), data=data, files=files.items()) self.assertEqual(t.get('files'), files)
def test_POST_FILES_WITH_PARAMS(self): """ Test POST of files with parameters """ with open(__file__) as f: url = self.coapbin('post') post1 = coap.post(url, data={'some': 'data'}, files={'some': f}) post2 = coap.post(url, data={'some': 'data'}, files=[('some', f)]) post3 = coap.post(url, data=[('some', 'data')], files=[('some', f)]) self.assertEqual(post1.status_code, 200) self.assertEqual(post2.status_code, 200) self.assertEqual(post3.status_code, 200)
def test_str_data_content_type(self): """ todo """ data = 'test string data' r = coap.post(self.coapbin('post'), data=data) t = json.loads(r.text) self.assertEqual(t.get('headers').get('Content-Type'), '')
def test_max_content_length(self): """ Test of the limitation of posting data on an endpoint :return: """ self.server.config['MAX_CONTENT_LENGTH'] = 64 rv = coap.post('/accept', data={'my_file': 'foo' * 100}) self.assertEqual(rv.data, '42')
def test_POST_FILES_WITH_HEADERS(self): """ Test POST with headers """ url = self.coapbin('post') with open(__file__) as f: post2 = coap.post(url, files={'some': f}) self.assertEqual(post2.status_code, 200)
def test_url_encoded_post_querystring_multivalued(self): """ todo """ r = coap.post(self.coapbin('post'), params={"test": ['foo', 'baz']}) self.assertEqual(r.status_code, 200) self.assertEqual(r.headers['content-type'], 'application/json') self.assertEqual(r.url, self.coapbin('post?test=foo&test=baz')) raw_body = json.loads(r.text) self.assertEqual(raw_body.get('form'), {}) # No form supplied self.assertEqual(raw_body.get('data'), '')
def test_url_encoded_post_query(self): """ Testing query and data in url """ r = coap.post(self.coapbin('post'), params={"test": 'foo'}) self.assertEqual(r.status_code, 200) self.assertEqual(r.headers['content-type'], 'application/json') self.assertEqual(r.url, self.coapbin('post?test=foo')) r = coap.get(self.coapbin('get'), params={"test": ['foo', 'baz']}) self.assertEqual(r.status_code, 200) self.assertEqual(r.url, self.coapbin('get?test=foo&test=baz'))
def test_request_dispatching(self): """ Simple request dispatching """ def index(request): """ Simple mirror view :param request: Incoming request """ return request.method self.server.route("/", index) self.server.route("/more", index, methods=['GET', 'POST']) self.assertEqual(coap.get(self.server.url + '/').data, 'GET') self.assertEqual(coap.post(self.server.url + '/').status_code, 405) self.assertEqual(coap.get(self.server.url + '/').status_code, 200) self.assertEqual(coap.post(self.server.url + '/more').data, 'POST') self.assertEqual(coap.get(self.server.url + '/more').data, 'GET') self.assertEqual( coap.delete(self.server.url + '/more').status_code, 405)
def test_multivalued_data_encoding(self): """ Make sure data encoding works on a value that is an iterable but not a list """ r = coap.post(self.coapbin('post'), data={"test": ('foo', "non_ascii")}) self.assertEqual(r.status_code, 200) self.assertEqual(r.headers['content-type'], 'application/json') raw_body = json.loads(r.text) self.assertEqual(raw_body.get('form'), {"test": ['foo', "non_ascii"]})
def test_url_encoded_post_data(self): """ Test post data """ content = "foobar" r = coap.post(self.coapbin('post'), data={"test": content}) self.assertEqual(r.status_code, 200) self.assertEqual(r.headers['content-type'], 'application/json') self.assertEqual(r.url, self.coapbin('post')) raw_body = json.loads(r.text) self.assertEqual(raw_body.get('data'), '')
def test_none_values_in_data_are_deleted(self): """ Test that keys with None as the value are removed instead of being posted. """ r = coap.post(self.coapbin('post'), data={ 'key1': 'value1', 'key2': None }) values = r.json['form'] self.assertEqual(values['key1'], 'value1') # The 'key2' key should not have been sent. self.assertTrue(values.get('key2') is None)
def test_url_encoded_post_querystring(self): """ Testing query in URL """ content = "foobar" r = coap.post(self.coapbin('post'), params={"test": content}) self.assertEqual(r.status_code, 200) self.assertEqual(r.headers['content-type'], 'application/json') self.assertEqual(r.url, self.coapbin('post?test=' + content)) raw_body = json.loads(r.text) self.assertEqual(raw_body.get('form'), {}) # No form supplied self.assertEqual(raw_body.get('data'), '')
def test_url_mapping(self): """ Simple URL mapping """ method = lambda request: request.method self.server.add_url_rule('/', 'index', method) self.server.add_url_rule('/more', 'more', method, methods=['GET', 'POST']) self.assertEqual(coap.get(self.server.url + '/').data, 'GET') rv = coap.post(self.server.url + '/') self.assertEqual(rv.status_code, 405) rv = coap.delete(self.server.url + '/more') self.assertEqual(rv.status_code, 405)
def test_url_encoded_post_query_multivalued_and_data(self): """ todo """ r = coap.post(self.coapbin('post'), params={"test": ['foo', 'baz']}, data={ "test2": "foobar", "test3": ['foo', 'baz'] }) self.assertEqual(r.status_code, 200) self.assertEqual(r.headers['content-type'], 'application/json') self.assertEqual(r.url, self.coapbin('post?test=foo&test=baz')) raw_body = json.loads(r.text) self.assertEqual(raw_body.get('form'), { "test2": 'foobar', "test3": ['foo', 'baz'] }) self.assertEqual(raw_body.get('data'), '')
def test_file_post_data(self): """ Posting data """ file_content = b"foobar" testfile = tempfile.NamedTemporaryFile(delete=False) testfile.write(file_content) testfile.flush() testfile.close() data = open(testfile.name, "rb") r = coap.post(self.coapbin('post'), data=data, headers={"content-type": "application/octet-stream"}) data.close() self.assertEqual(r.status_code, 200) self.assertEqual(r.headers['content-type'], 'application/json') self.assertEqual(r.url, self.coapbin('post')) raw_body = json.loads(r.text) self.assertTrue(raw_body.get('form') in (None, {})) self.assertEqual(raw_body.get('data'), file_content.decode('ascii')) os.remove(testfile.name)
def test_POST_FILES(self): """ Test POST of text, files... """ url = self.coapbin('post') post1 = coap.post(url, data={'some': 'data'}) self.assertEqual(post1.status_code, 200) with open(__file__) as f: post2 = coap.post(url, files={'some': f}) post3 = coap.post(url, files=[('some', f)]) self.assertEqual(post2.status_code, 200) self.assertEqual(post3.status_code, 200) post4 = coap.post(url, data=json.dumps({"some": "json"})) self.assertEqual(post4.status_code, 200) try: coap.post(url, files=['bad file data']) except ValueError: pass
def test_bytes_files(self): """ Test that `bytes` can be used as the values of `files`. """ coap.post(self.coapbin('post'), files={'test': b'test'})