Skip to content

Commit 03d1e64

Browse files
committed
Basic tests for tunneling of methods via x-http-method-override.
1 parent ae80c92 commit 03d1e64

File tree

1 file changed

+63
-1
lines changed

1 file changed

+63
-1
lines changed

test/rest_common.py

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1241,7 +1241,6 @@ def testDispatch(self):
12411241
json_dict = json.loads(b2s(results))
12421242
print(results)
12431243
status=json_dict['data']['status']
1244-
status=json_dict['data']['status']
12451244
self.assertEqual(status, 'ok')
12461245

12471246
# TEST #9
@@ -1369,6 +1368,69 @@ def testDispatch(self):
13691368

13701369
del(self.headers)
13711370

1371+
def testMethodOverride(self):
1372+
# TEST #1
1373+
# Use GET, PUT, PATCH to tunnel DELETE expect error
1374+
1375+
body=b'{ "order": 5 }'
1376+
env = { "CONTENT_TYPE": "application/json",
1377+
"CONTENT_LENGTH": len(body),
1378+
"REQUEST_METHOD": "POST"
1379+
}
1380+
body_file=BytesIO(body) # FieldStorage needs a file
1381+
self.server.client.request.headers.get=self.get_header
1382+
for method in ( "GET", "PUT", "PATCH" ):
1383+
headers={"accept": "application/json; version=1",
1384+
"content-type": env['CONTENT_TYPE'],
1385+
"content-length": len(body),
1386+
"x-http-method-override": "DElETE",
1387+
}
1388+
self.headers=headers
1389+
form = client.BinaryFieldStorage(body_file,
1390+
headers=headers,
1391+
environ=env)
1392+
self.db.setCurrentUser('admin') # must be admin to create status
1393+
results = self.server.dispatch(method,
1394+
"/rest/data/status",
1395+
form)
1396+
1397+
self.assertEqual(self.server.client.response_code, 400)
1398+
json_dict = json.loads(b2s(results))
1399+
status=json_dict['error']['status']
1400+
msg=json_dict['error']['msg']
1401+
self.assertEqual(status, 400)
1402+
self.assertEqual(msg, "X-HTTP-Method-Override: DElETE must be "
1403+
"used with POST method not %s."%method)
1404+
1405+
# TEST #2
1406+
# DELETE: delete issue 1 via post tunnel
1407+
self.assertFalse(self.db.status.is_retired("1"))
1408+
etag = calculate_etag(self.db.status.getnode("1"),
1409+
self.db.config['WEB_SECRET_KEY'])
1410+
etagb = etag.strip ('"')
1411+
headers={"accept": "application/json; q=0.75, application/xml; q=1",
1412+
"if-match": '"%s"'%etagb,
1413+
"content-length": 0,
1414+
"x-http-method-override": "DElETE"
1415+
}
1416+
self.headers=headers
1417+
body_file=BytesIO(b'') # FieldStorage needs a file
1418+
form = client.BinaryFieldStorage(body_file,
1419+
headers=headers,
1420+
environ=env)
1421+
self.server.client.request.headers.get=self.get_header
1422+
self.db.setCurrentUser('admin') # must be admin to delete issue
1423+
results = self.server.dispatch('POST',
1424+
"/rest/data/status/1",
1425+
form)
1426+
print(results)
1427+
self.assertEqual(self.server.client.response_code, 200)
1428+
json_dict = json.loads(b2s(results))
1429+
status=json_dict['data']['status']
1430+
self.assertEqual(status, 'ok')
1431+
self.assertTrue(self.db.status.is_retired("1"))
1432+
1433+
13721434
def testPostPOE(self):
13731435
''' test post once exactly: get POE url, create issue
13741436
using POE url. Use dispatch entry point.

0 commit comments

Comments
 (0)