Commit 18a26c12 authored by Łukasz Nowak's avatar Łukasz Nowak

Follow spec in tests.

parent e3b11012
...@@ -41,15 +41,14 @@ class NCHandler(BaseHTTPServer.BaseHTTPRequestHandler): ...@@ -41,15 +41,14 @@ class NCHandler(BaseHTTPServer.BaseHTTPRequestHandler):
self.end_headers() self.end_headers()
self.wfile.write(out) self.wfile.write(out)
def do_POST(self): def do_PUT(self):
path = os.path.abspath(os.path.join(self.tree, *self.path.split('/'))) path = os.path.abspath(os.path.join(self.tree, *self.path.split('/')))
if not os.path.exists(path): if not os.path.exists(os.path.dirname(path)):
os.makedirs(path) os.makedirs(os.path.dirname(path))
data = self.rfile.read(int(self.headers.getheader('content-length'))) data = self.rfile.read(int(self.headers.getheader('content-length')))
cksum = hashlib.sha512(data).hexdigest() cksum = hashlib.sha512(data).hexdigest()
if 'shadir' in path: if 'shadir' in path:
d = json.loads(data) d = json.loads(data)
path = os.path.join(path, d[0]['urlmd5'])
data = json.dumps([d]) data = json.dumps([d])
if os.path.exists(path): if os.path.exists(path):
f = open(path, 'r') f = open(path, 'r')
...@@ -61,6 +60,25 @@ class NCHandler(BaseHTTPServer.BaseHTTPRequestHandler): ...@@ -61,6 +60,25 @@ class NCHandler(BaseHTTPServer.BaseHTTPRequestHandler):
json_data_list = json.loads(file_data) json_data_list = json.loads(file_data)
json_data_list.append(d) json_data_list.append(d)
data = json.dumps(json_data_list) data = json.dumps(json_data_list)
else:
raise ValueError('shacache shall use POST')
open(path, 'wb').write(data)
self.send_response(201)
self.send_header('Content-Length', str(len(cksum)))
self.send_header('Content-Type', 'text/html')
self.end_headers()
self.wfile.write(cksum)
return
def do_POST(self):
path = os.path.abspath(os.path.join(self.tree, *self.path.split('/')))
if not os.path.exists(path):
os.makedirs(path)
data = self.rfile.read(int(self.headers.getheader('content-length')))
cksum = hashlib.sha512(data).hexdigest()
if 'shadir' in path:
raise ValueError('shadir shall use PUT')
else: else:
path = os.path.join(path, cksum) path = os.path.join(path, cksum)
...@@ -182,15 +200,17 @@ class OnlineTest(OnlineMixin, unittest.TestCase): ...@@ -182,15 +200,17 @@ class OnlineTest(OnlineMixin, unittest.TestCase):
"""Check scenario with shadir used""" """Check scenario with shadir used"""
nc = NetworkcacheClient(self.shacache, self.shadir) nc = NetworkcacheClient(self.shacache, self.shadir)
urlmd5 = str(random.random()) urlmd5 = str(random.random())
nc.upload(self.test_data, urlmd5, file_name='my file') key = 'somekey' + str(random.random())
result = nc.select(urlmd5) nc.upload(self.test_data, urlmd5, file_name='my file', key=key)
result = nc.select(key)
self.assertEqual(result.read(), self.test_string) self.assertEqual(result.read(), self.test_string)
def test_upload_shadir_no_filename(self): def test_upload_shadir_no_filename(self):
"""Check scenario with shadir used, but not filename passed""" """Check scenario with shadir used, but not filename passed"""
nc = NetworkcacheClient(self.shacache, self.shadir) nc = NetworkcacheClient(self.shacache, self.shadir)
urlmd5 = str(random.random()) urlmd5 = str(random.random())
self.assertRaises(ValueError, nc.upload, self.test_data, urlmd5) self.assertRaises(ValueError, nc.upload, self.test_data, urlmd5,
key='somekey')
def test_upload_twice_same(self): def test_upload_twice_same(self):
nc = NetworkcacheClient(self.shacache, self.shadir) nc = NetworkcacheClient(self.shacache, self.shadir)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment