def create(self): """Add a new user to the store """ user_data = self._get_user_data() user = self._create_user(user_data) if not self._validate(user): return Response('Data validation failed.', '422 Unprocessable Entity') if self._persist(user): response = Response(user.__dict__, '201 CREATED') response.add_header('Location', "/user/{0}".format(user.id)) return response
def download(self): rsp = Response(Request(self.downloadurl)) content_disposition = rsp.headers['Content-Disposition'] filename = '' if content_disposition: match = re.search(r'filename="(.+?)"', content_disposition) if match: filename = match.group(1) if not filename: filename = self.name + '.zip' print '\nDownloading %s as %s ...' % (self, filename), rsp.save_to_path(filename)
def get_json(request): body = '[]' headers = [ ('Content-Type', f'application/json'), ('Content-Length', len(body)), ] return Response(200, 'OK', headers, body.encode())
def post_json(request): request.body_file.seek(0) body = request.body_file.read() headers = [ ('Content-Type', f'application/json'), ('Content-Length', len(body)), ] return Response(200, 'OK', headers, body)
def test_response_file(self, test_dir): request = Request() file = os.path.join(test_dir, 'test.html') with open(file, 'wb') as f: f.write(b'012345') response = Response.response_file(request, file, 'text/html') assert response.status == 200 assert response.message == 'OK' assert response.headers.get('Content-Length') == 6 assert response.body == b'012345'
def test_response_file_range_withoud_end(self, test_dir): request = Request() request.headers['Range'] = 'bytes=2-' file = os.path.join(test_dir, 'test.html') with open(file, 'wb') as f: f.write(b'012345') response = Response.response_file(request, file, 'text/html') assert response.status == 206 assert response.message == 'Partial Content' assert response.headers.get('Content-Length') == 4 assert response.body == b'2345'
def test(name): name += request.path_info u = session.get('user') login = session.get('login') # if not session.get('user'): # raise Redirect('http://www.localhost') # raise Redirect('/hi/go/baby') res = Response("test on session user %s,logined:%s" % (u, login)) # res.set_cookie('hello', 'hahaha') # res.add_header('Content-Type', 'text/plain') # print request.cookie # print request.environ # print request.form.get('a') # print request.get_cookie('helloo') session.set('user', 'aka') session.set('login', True) return res
def test_response_dir(self, test_dir): request = Request() path = os.path.join(test_dir) response = Response.response_dir(request, path) curpath = path[path.rfind('/'):] act_body = (b"<!DOCTYPE html><meta charset='utf-8'>" b"<html>\n<head>\n<title>") act_body += f'Listing for: None'.encode('utf-8') act_body += b'</title>\n</head>\n</head>\n<body><h1>' act_body += f'Listing for: None'.encode('utf-8') act_body += f'</h1><hr>\n<ul><li><a href="/" '.encode('utf-8') act_body += f'None>/</a></li>\n</'.encode('utf-8') act_body += b'ul>\n</body>\n</html>\n' assert response.status == 200 assert response.message == 'OK' assert response.body == act_body assert response.headers.get('Content-Length') == len(act_body) assert response.body == act_body
def index(request, filename): path = os.path.join(os.path.dirname(__file__), '{}.html'.format(filename)) if not os.path.exists(path): return Response("File not found: {}".format(path), status_code=404) return send_file(path)
def test_get_headers(self): response = Response(200, "OK", headers={"Content-Length": 0}, body='') expected = 'Content-Length: 0\r\nContent-Type: text/html\r\n' actual = response.get_headers() assert expected == actual
def get_wunderlist_today(): return flask.jsonify(Response(data=wunder_web.get_today_tasks()))
def get_day_info(): return flask.jsonify(Response(data=day_info.get_day_info()))
def get_humidex_info(): data = humidex.get_humidex_info() if not data: return flask.jsonify(Response(ok=False, errors=['No data found'])) return flask.jsonify(Response(data=data.to_json()))
def send_html(template, **kwargs): return Response(template.format(**kwargs), headers={'Content-Type': 'text/html'})
def query(request): body = request.query return Response(200, 'OK', body=str(body).encode())
def test_status_code(self): response = Response(200, "OK", body='') expected = f"HTTP/1.1 {response.status} {response.message}" actual = response.status_code() assert expected == actual
def pattern(request, p): return Response(200, 'OK', body=p.encode())
def get(request): return Response(200, 'OK', [], b'')