def test_get_response_headers_tuple(self): """Test we get expected headers with a tuple argument.""" proxy_response = (1, 1, {'lol': 'wat', 'etag': '1'}) with webcompat.app.app_context(): new_headers = get_response_headers(proxy_response) assert new_headers.get('content-type') == 'application/json' assert new_headers.get('etag') == '1' assert new_headers.get('lol') is None new_headers2 = get_response_headers(proxy_response, mime_type='text/html') assert new_headers2.get('content-type') == 'text/html'
def proxy_comments(number): """XHR endpoint for GitHub issue comments. * GET an issue comments * POST a comment on an issue (only as an authorized GitHub user) """ params = request.args.copy() path = 'repos/{0}/{1}/comments'.format(ISSUES_PATH, number) if request.method == 'POST' and g.user: new_comment = api_request('post', path, params=params, data=get_comment_data(request.data), mime_type=JSON_MIME_HTML) return get_html_comments(new_comment) else: # TODO: handle the (rare) case for more than 1 page of comments # for now, we just get the first 100 and rely on the client to # fetch more params.update({'per_page': 100}) comments_data = api_request('get', path, params=params, mime_type=JSON_MIME_HTML) comments_status = comments_data[1:2] if comments_status != 304: return get_html_comments(comments_data) else: # in the case of a 304, the browser cache will handle it. return '', 304, get_response_headers(comments_data, HTML_MIME)
def modify_labels(number): """XHR endpoint to modify issue labels. Sending in an empty array removes them all as well. This method is always proxied because non-repo collabs can't normally edit labels for an issue. """ if g.user: path = 'repos/{0}/{1}/labels'.format(ISSUES_PATH, number) labels = proxy_request('put', path, data=request.data) return (labels.content, labels.status_code, get_response_headers(labels)) else: abort(403)
def get_html_comments(response): """Helper method to create a response from our comment template. The template expects a list of dict objects, so create a list in the instance where we have a single object. """ comment_json, comment_status = response[:2] # In the case of a 304, GitHub won't send us back a body, but # the browser cache will correctly load the data. try: comment_json = json.loads(comment_json) except ValueError: pass if isinstance(comment_json, dict): comment_json = [comment_json] comment_html = render_template('issue/issue-comment-list.html', comments=comment_json) return make_response(comment_html, comment_status, get_response_headers(response, HTML_MIME))